0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoogleFormとGoogleSpreadSheetとGoogleAppsScriptで、フォームとDBとデータ取得APIを作成する

Posted at

背景

  • 迷子猫捜索アプリを作りたい
  • (その機能として)入力フォームとデータ取得APIを用意したい
  • 無料で手間をかけずに作りたい

GoogleFormを使い、SpreadSheetをDBに見立てて、GASでAPIを準備すれば楽なのでは?
→ 試してみた

実装

フォーム

GoogleFormを使って項目を作る
image.png

DB

Formの回答をSpreadSheetに書き出す
関連付けされたSheetは名前を変えてもOK
image.png

image.png

API

GoogleAppsScriptsを使ってSpreadSheetの情報を返すAPIを作る

gas.js
function doGet(e) {
  var table = getTableFromSheet()
  var payload = JSON.stringify(table)

  ContentService.createTextOutput()
  var output = ContentService.createTextOutput();
  output.setMimeType(ContentService.MimeType.JSON);
  output.setContent(payload);

  return output;
}

function getTableFromSheet() {
  const SHEET_URL = "https://docs.google.com/spreadsheets/d/your sheet id/";
  const SHEET_NAME = "your sheet name";

  var spreadSheet = SpreadsheetApp.openByUrl(SHEET_URL);
  var sheet = spreadSheet.getSheetByName(SHEET_NAME);
  var table = sheet.getDataRange().getValues();

  return table;
}

確認

デプロイ後にURLにGETアクセスすると、SpreadSheetの情報をJSONで返しました。

image.png

まとめ

実際にアプリに組み込む際にはアクセス権の調整などは必要そうですが、フォーム・DB・APIを無料でサッと作るには良いかと思います。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?