clasp run できないとき。2018-09-25


概要

GAS のGoogle謹製CLIツール clasp を参考に、claspを導入しました。
push/pullは問題なく使えて非常に便利です。

claspのhelpを見ると run というオプションがあり、ターミナルからGASの関数を実行できるようです。
試してみたところいろいろエラーで実行できなかったので、実行できるようにします。

その前に

2018-09-25現在でinstallされる以下バージョンだとエラーが出て動きません。
GitHubの最新のコードで試しましょう。

% clasp -v
1.5.3

しばらく待てば修正版がリリースされると思います。
追記: 1.6以上がリリースされました。それを使えばエラーが出ないのでふつうにnpm installで入れれば問題ないです。

初回(.clasp.jsonprojectId がない状態)の run

初回(.clasp.jsonprojectId がない状態)で run をすると必要な準備の手順が示されます。その通り設定していきます。

以下の通り、実施すべきことが出力され、.clasp.jsonprojectIdが追加されます。
英語ではありますが書いてある通りに設定していくのみです。

% clasp run FUNCTION_NAME

BASIC SCRIPT EXECUTION API SETUP

Open this link:  https://script.google.com/d/<<<SCRIPT_ID>>>/edit
Go to *Resource > Cloud Platform Project...* and copy your projectId
(including "project-id-")

? What is your GCP projectId? <<<PROJECT_ID>>> # ここにprojectIdを入力します。
1. Enable the Script & Logging APIs for the project:
  a. Open this link: https://console.cloud.google.com/apis/library/script.googleapis.com/?project=<<< PROJECT_ID >>>
      Click ENABLE.
  b. Open this link: https://console.cloud.google.com/apis/library/logging.googleapis.com?project=<<< PROJECT_ID >>>
      Click ENABLE.

2. Create a client ID and secret:
    Open this link: https://console.developers.google.com/apis/credentials?project=<<< PROJECT_ID >>>
    Click Create credentials, then select OAuth client ID.
    Select Other.
    Give the client a name.
    Click Create.
    Click Download JSON for the new client ID: name (right-hand side).

3. Authenticate clasp with your credentials json file:
    clasp login --creds <client_credentials.json>

最後にダウンロードした <client_credentials.json> を指定してログインし直します。
ログインが終わると、プロジェクト配下に .clasprc.json が作成されます。 (--creds オプション無しの場合は $HOME 以下に .clasprc.json が作成されました)

さらに追加で設定

「実行可能APIとして導入」のONにする

今の状態で run しても以下のようなエラーが出るので「実行可能APIとして導入」からポチポチ実行可能なAPIとして導入します。

Script API executable not published/deployed.

clasp から 「実行可能APIとして導入」 する方法があれば教えていただけると嬉しいです

scopeの設定

SpreadsheetApp などを使っている場合は、scopeを追加する必要があります。
scopeを設定しないと以下のようなエラーが出ます。

Error: Local client credentials unauthenticated. Check scopes/authorization.

scopeの設定方法

clone したときについてくる appsscript.json に設定します。
以下のような設定を追加します。
UrlFetchApp を利用している場合は https://www.googleapis.com/auth/script.external_request を追加する必要があります

  "oauthScopes": [
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request"
  ]

参考: https://github.com/google/clasp/issues/11#issuecomment-358111971

全体のJSON例:

appsscript.json
{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request"
  ]
}

socpeの設定後、初回の run

以下のように質問され、追加のscopeありの状態で承認します。(いつも通り承認用にブラウザが立ち上がるので承認します。)

% clasp run FUNCTION_NAME
New authoization scopes detected in manifest:
 [ 'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/spreadsheets',
  'https://www.googleapis.com/auth/script.external_request' ]
? Authorize new scopes? Yes
? Use localhost? Yes

run する

ここまで終わればあとは run できます。

実行中

% clasp run FUNCTION_NAME
\ Using local credentials: ...(省略)

実行中はくるくるしてるので待ちます。

実行結果例

returnのないfunctionの場合は undefined と表示されます。

% clasp run FUNCTION_NAME
Result: undefined

return していればその内容が表示されます。

% clasp run FUNCTION_NAME
Result: <returnした値>

Logger.logの内容は表示されないようです
追記: ログを確認したい場合は clasp logs を使います

まとめ

clasp run できました