- 公開日:
スプレッドシートでGASの実行ボタンを作る方法
Google スプレッドシートではGoogle Apps Script(GAS)で作成したスクリプトを、クリック一つで実行することができるボタンを作成することが出来ます。
この記事では、スプレッドシートでGASの実行ボタンを作る方法を分かりやすく紹介します。
スプレッドシートでGASの実行ボタンを作る方法
Google スプレッドシートでGASの実行ボタンを作る場合、まずはメニューバーの挿入にある「図形描画」を選択してボタンを作成してシートに表示させます。
その後、GASで作成したスクリプトをボタンの図形に割り当てて、GASの実行ボタンとして使用していきます。
![「図形描画」を順に選択](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet1.png)
①「挿入」②「図形描画」を順に選択します。
以下のショートカットキーでも「図形描画」を選択できます。
OS | ショートカットキー |
---|---|
Windows | Alt+I,W (Google Chrome) Alt+Shift+I,W (その他ブラウザ) |
Mac | Option+I,W (Google Chrome) Option+Shift+I,W (その他ブラウザ) |
![「長方形」を順に選択](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet2.png)
長方形の形でボタンを作ります。
①「図形マーク」②「図形」③「長方形」を順に選択します。
![青い正方形マークをドラッグ&ドロップ](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet3.png)
図形の周りある青い正方形マークをドラッグ&ドロップして好みのサイズにします。
![図形内でダブルクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet4.png)
図形の形状が変更されました。
図形内でダブルクリックします。
![図形に文字を入力](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet5.png)
図形に文字を入力します。
![「保存して閉じる」ボタンをクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet6.png)
「保存して閉じる」ボタンをクリックします。
![「Apps Script」を順に選択](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet7.png)
作成した図形がシート上に表示されました。
①「拡張機能」②「Apps Script」を順に選択します。
![「新しいプロジェクト」ボタンをクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet9.png)
「新しいプロジェクト」ボタンをクリックします。
![名前を変更をクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet10.png)
「名前を変更」をクリックします。
![プロジェクトタイトルを変更します](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet8.png)
①「プロジェクトタイトル(例:サンプルスクリプト)」を入力し、②「名前を変更」ボタンをクリックします。
![スクリプトをコピーして貼り付けます](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet11.png)
下記のスクリプトをコピーして貼り付けます。
A1セルに「あいうえお」と入力するコードです。
function writeTextToCell() {
// アクティブなスプレッドシートを取得
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// a1セルに「あいうえお」を書き込む
sheet.getRange('A1').setValue('あいうえお');
}
![プロジェクトを保存」ボタンをクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet12.png)
「プロジェクトを保存」ボタンをクリックします。
![function名をコピーする](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet13.png)
1行目の関数名(例:writeTextToCell)を選択し、コピーするためにCtrl+Cキーを押します。
![3点マークをクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet14.png)
現在のシートに戻り、図形を選択して右上の3点マークをクリックします。
![「スクリプトを割り当て」を選択](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet15.png)
「スクリプトを割り当て」を選択します。
![function名を貼り付けて確定する](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet16.png)
① Ctrl+Vキーを押して関数名をペーストし②「確定」ボタンをクリックします。
これで図形にスクリプトが割り当てられました。
![「スクリプトボタン」をクリック](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet17.png)
「スクリプトボタン」をクリックします。
![スクリプトが正常に動作する](https://g-tips.jp/wp-content/uploads/create-execution-button-for-gas-in-spreadsheet18.png)
スクリプトが正常に動作しました。
シートにGASの実行ボタンを作ることが出来ました。