業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

0
0

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 3 years have passed since last update.

[GoogleAppsScript(GAS)] 関数を API として公開し、ターミナルから実行する方法

Last updated at Posted at 2020-08-29

概要

GAS で作成した関数を API として公開する。makefile で curl で API を呼び出し、ターミナルから実行する手順についてまとめる。

成果物

「name」「hp」「attack」列を用意しているスプレッドシートに、makefile で指定した値を追加する
スクリーンショット 2020-08-29 22.20.52.png
          ↓
スクリーンショット 2020-08-29 22.21.10.png

前提知識

GoogleAppsScript(GAS) で 「Hello World」する方法

全体像

スクリーンショット 2020-08-29 22.30.46.png

手順

GAS で関数を作成

スプレッドシートの [ツール] → [スクリプトエディタ] に以下のコードを記述
シート名は「monsters」とする

.gas
const doPost = (e) =>{
  const data = {
   name:e.parameter.name,
   hp:e.parameter.hp,
   attack:e.parameter.attack
 }

  appendMonster(data);
  return ContentService.createTextOutput("OK!")
}

const appendMonster = (data) => {
  const spreadSheet = SpreadsheetApp.getActive();
  const monstersSheet = spreadSheet.getSheetByName("monsters")
  monstersSheet.appendRow([data.name,data.hp,data.attack]);
}

通信する際に利用する関数名として、「doPost」と「doGet」がある。呼び出した値を利用する場合は「doPost」、呼び出した値を返す場合は「doGet」を利用する。ここでは、呼び出した値をスプレッドシートに書き込むため「doPost」を利用した。

API として公開

スクリーンショット 2020-08-29 22.07.06.png
[Project version:]で [New] を選択
※理由は不明だが、「New」を選択しないと URL が更新されない。
スクリーンショット 2020-08-29 22.13.20.png

Makefile を作成し、curl を利用して API を呼び出す記述を追加

作業フォルダーとして、「gas-client」 フォルダーを作成し、その中に Makefile を作成する
Makefile には、変数で API の URL を定義し、appenddata で curl コマンドを実行する

Makefile
API_URL = 'API 公開時に取得した URL'

appenddata:
	curl -L $(API_URL) \
	-F 'name=リベンジ' \
	-F 'hp=50' \
	-F 'attack=10'

ターミナルで実行

ターミナル
gas-client $ make appenddata
curl -L 'API 公開時に取得した URL' \
	-F 'name=リベンジ' \
	-F 'hp=50' \
	-F 'attack=10'
OK!gas-client $ 

参考

GoのためのMakefile入門

0
0
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?