GETリクエストを処理したい(doGet)
1function doGet(e) {
2
3 // https://script.google.com/macros/s/スクリプトID/exec?name=John へのアクセス
4
5 // クエリから値を取得(?name=John)
6 const name = e.parameter.name;
7
8 // コンテンツを作成
9 const message = "Hello, " + (name ? name : "world") + "!";
10
11 // レスポンスを返す
12 return ContentService.createTextOutput(message).setMimeType(ContentService.MimeType.TEXT);
13}
doGet関数は、GASでGETリクエストを処理するための関数です。
レスポンスはContentService.createTextOutputなどで生成し、ウェブアプリとしてデプロイできます。
上のサンプルでは、あるシートに紐づいたdoGet関数を定義しています。
GETリクエストは
https://script.google.com/macros/s/スクリプトID/exec?name=John
を想定しています。
クエリが?name=Johnとなっているので、
e.parameter.nameでJohnという値を取得できます。
またnameを使ってmessageの文字列を作成しています。
そしてContentService.createTextOutputを使ってレスポンスを作成しています。
今回はただのテキスト情報なので、MIMEタイプをTEXTにしています。
クエリを?name=Smithに変更すると、レスポンスも変わることが想像できると思います。
デプロイしたい
GASのエディターからデプロイできます。
[デプロイ]>[新しいデプロイ]種類の選択:
ウェブアプリ説明:
(アプリの説明)次のユーザーとして実行:
[自分]アクセスできるユーザー:
[全員]
テストしたい
1function testDoGetParameters() {
2 const e = {
3 "parameter": {
4 "name": "John"
5 }
6 };
7 const response = doGet(e);
8 const content = response.getContent();
9 Logger.log(`content: ${content}`);
10}
11
12function testDoGetURL() {
13 const baseUrl = ScriptApp.getService().getUrl();
14 Logger.log(`Base URL: ${baseUrl}`);
15 const query = "?name=John";
16 Logger.log(`Query: ${query}`);
17
18 const url = baseUrl + query;
19
20 const options = {
21 "method": "GET",
22 "followRedirects": true,
23 };
24 const response = UrlFetchApp.fetch(url, options);
25 Logger.log(`response: ${response}`);
26}
UrlFetchAppで、doGet関数の動作確認ができます。
シート名ごとに処理したい
1function doGet(e) {
2
3 // クエリからシート名を取得
4 const sheetName = e.parameter.sheetName;
5
6 // クエリが見つからない場合
7 if (!sheetName) {
8 const msg = "シート名が指定されていません";
9 return ContentService.createTextOutput(msg).setMimeType(ContentService.MimeType.TEXT);
10 }
11
12 // スプレッドシートを取得
13 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
14 const sheet = spreadsheet.getSheetByName(sheetName);
15
16 // シート名が見つからない場合
17 if (!sheet) {
18 const msg = `指定されたシートが存在しません: ${sheetName}`;
19 return ContentService.createTextOutput(msg).setMimeType(ContentService.MimeType.TEXT);
20 }
21
22 // シートの内容をJSON形式に変換する
23 const values = sheet.getDataRange().getValues();
24 const headers = values[0];
25 const rows = values.slice(1);
26 const records = rows.map(row =>
27 Object.fromEntries(headers.map((h, i) => [h, row[i]]))
28 );
29
30 return ContentService.createTextOutput(JSON.stringify(records))
31 .setMimeType(ContentService.MimeType.JSON);
32}
doGet関数の実用的なサンプルです。
ここでは、あるスプレッドシートに複数のシートがある場合を想定しています。
そして、シートごとに内容をJSON形式で公開し、外部からデータ処理できるようにしたいと考えています。
GASで公開したウェブアプリは
https://script.google.com/macros/s/スクリプトID/exec
でアクセスできるようになります。
クエリに?sheetName=シート名とすることで、該当するシートのコンテンツにアクセスできます。
複数の処理を切り替えたい
1const actionMap = {
2 "action1": doGetOne,
3 "action2": doGetTwo,
4 "action3": doGetThree,
5};
6
7function doGet(e) {
8 const action = e.parameter.action;
9 return actionMap[action](e);
10}
11
12function doGetOne(e) {
13 return ContentService.createTextOutput("action1です");
14}
15
16function doGetTwo(e) {
17 return ContentService.createTextOutput("action2です");
18}
19
20function doGetThree(e) {
21 return ContentService.createTextOutput("action3です");
22}
doGet関数は、ひとつのプロジェクトで、ひとつしか定義できない、特殊な関数です。
しかし、スタンドアロンなプロジェクトから、
複数のプロジェクトを操作したいこともあります。
その場合、クエリーを使って分岐させます。