tec memo

メモです。

Google Apps Script(GAS)を管理するCLIツール Clasp

Clasp なるCLIツールがあることを教えてもらったので、インストールし使えるまでの作業を備忘録として残す。

なお、作業については以下を参照させていただきました。

qiita.com

事前準備

事前にnodejsを入れておきましょう。 私は、nodenvでインストールした。

$ nodenv install 15.6.0
$ nodenv global 15.6.0
$ node rehash

claspをインストール

npmで以下の通りインストールを試みる。

$ npm i @google/clasp -g
npm WARN ERESOLVE overriding peer dependency
npm WARN Found: inquirer@7.3.3
npm WARN node_modules/@google/clasp/node_modules/inquirer
npm WARN   inquirer@"^7.0.0" from @google/clasp@2.3.0
npm WARN   node_modules/@google/clasp
npm WARN     @google/clasp@"*" from the root project
npm WARN
npm WARN Could not resolve dependency:
npm WARN peer inquirer@"^5.0.0 || ^6.0.0" from inquirer-autocomplete-prompt@1.0.1
npm WARN node_modules/@google/clasp/node_modules/inquirer-autocomplete-prompt
npm WARN   inquirer-autocomplete-prompt@"1.0.1" from @google/clasp@2.3.0
npm WARN   node_modules/@google/clasp

changed 160 packages, and audited 161 packages in 10s

13 packages are looking for funding
  run `npm fund` for details

7 high severity vulnerabilities

To address all issues, run:
  npm audit fix

Run `npm audit` for details.

7つの重大な脆弱性が見つかったというメッセージがでた。 npm audit fix で自動で解決を試みた。

$ npm audit fix

up to date, audited 1 package in 602ms

$ npm audit
found 0 vulnerabilities

脆弱性がなくなったようなので、再度claspをインストール

npm i clasp

added 1 package, and audited 2 packages in 10s

found 0 vulnerabilities

googleに認証する

claspでログインをする。

$ clasp login

デフォルトブラウザが起動し、以下のような画面になるので許可をする。

f:id:atani:20210125223621p:plain

ターミナルに戻ると、以下のようなメッセージが出て、ログイン完了。

Authorization successful.

Default credentials saved to: ~/.clasprc.json (/Users/akira/.clasprc.json).
$ clasp create
? Create which script? standalone
User has not enabled the Apps Script API. Enable it by visiting https://script.google.com/home/usersettings then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

GASのAPIが無効になっているので有効にする必要がある。

https://script.google.com/home/usersettings にアクセス

以下のような画面になるので、有効にする。

設定変更前

f:id:atani:20210125223139p:plain:w400

設定変更後

f:id:atani:20210125223218p:plain:w400

再度 GASファイルを作ってみる

GASファイルを作ってみる。

$ clasp create
? Create which script? (Use arrow keys)
❯ standalone
  docs
  sheets
  slides
  forms
  webapp
  api

選択肢が出てくるので、今回は standalone を選択。 以下のようなメッセージが出れば、作成完了。

Created new standalone script: https://script.google.com/d/XXXXXX/edit
Warning: files in subfolder are not accounted for unless you set a '.claspignore' file.
Cloned 1 file.
└─ appsscript.json