【Google Apps Script】claspを使ってローカル環境で開発する

Google Apps Script(GAS)のエディタで直接スクリプトを書くことももちろんできますが、インデントや補完がうまくできないので辛いです。。
そこで、ローカル環境で開発を行う方法について調べてみました!

Node.jsのインストール

Node.js が必要になります。インストールはこちらから↓

nodejs.org

claspのインストール

Google公式のCLIツールである「clasp」をインストールします。

$ sudo npm install @google/clasp -g

一般ユーザーで実行するとエラーになるので注意!

npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

Googleアカウントへのアクセス許可

$ clasp login

コマンドを実行すると、ブラウザで認証確認画面が表示されるので「許可」してください。

ログインに成功すると、.clasprc.jsonというファイルが生成されます。

Authorization successful.

Default credentials saved to: ~/.clasprc.json (/xxxx/.clasprc.json).

ちなみにログアウトを行うと上記ファイルが削除されます。

$ clasp logout

Google Apps Script APIを許可する

G Suite Developer Hub(https://script.google.com/home/usersettings)の「設定」メニューからGoogle Apps Script APIを「オン」にします。

f:id:aym413:20190404233417p:plain:w600

オンにしないとpushした時にエラーになるので注意!

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の作成

今回はGASを作成するときの3つのパターンについて記載しています。

  1. スプレッドシートから新規で作成する場合
  2. 既にあるGASファイルを使用する場合
  3. スプレッドシートは用意していて、GASファイルをこれから作成する場合

スプレッドシートから新規で作成する場合

プロジェクト名を指定して、スプレッドシートとGASファイルを作成します。

ここで指定する<プロジェクト名>はスプレッドシートGASのプロジェクト名が同一になります。

$ cd <任意の作業ディレクトリ>
$ clasp create <プロジェクト名> --type sheets
Created new Google Sheet: https://drive.google.com/open?id=xxxxxx → スプレッドシートのURL
Created new Google Sheets Add-on script: https://script.google.com/d/xxxxx/edit → GASのURL
Cloned 1 file.
└─ appsscript.json

既にあるGASファイルを使用する場合

スクリプトエディタメニューの「ファイル」>「プロジェクトのプロパティ」>「情報」タブ内に記載されているスクリプトIDをコピーします。
f:id:aym413:20190404232626p:plain:w500

スクリプトIDを指定してスクリプトファイルをクローンしてきます。

$ cd <任意の作業ディレクトリ>
$ clasp clone <スクリプトID>
Cloned 2 files.
└─ appsscript.json
└─ test.js

スプレッドシートは用意していて、GASファイルをこれから作成する場合

スプレッドシート開いて、URL内のIDをコピーします。

https://docs.google.com/spreadsheets/d/<スプレッドシートのID>/edit#gid=0


スプレッドシートを指定して、GASファイルを作成します。
clasp create を実行したら Clone which script? と聞かれるので standalone を選択して Enterキー を押下します。
ここで sheets を選択してしまうと、新規でスプレッドシートが作成されてしまうので注意!

$ cd <任意の作業ディレクトリ>
$ clasp create --parentId <スプレッドシートのID> --title <プロジェクト名>
? Clone which script? (Use arrow keys)
❯ standalone 
  docs 
  sheets 
  slides 
  forms 
  webapp 
  api 

--titleは指定しなくても実行はできますが、指定しなかった場合は、作業ディレクトリがプロジェクト名になります。


上記のclasp createclasp clone を実行すると、.clasp.jsonというファイルが生成されるのですが、
もし、下記のエラーが発生した場合は.clasp.jsonがホームディレクトリ内で既に作成されてしまっていますので削除してください。(私自身これにハマりました・・)

Nested clasp projects are not supported.

GASファイルをpushする

最後にローカルで開発したGASファイルをpushします。

$ cd <任意の作業ディレクトリ>
$ clasp push
└─ appsscript.json
└─ test.js
Pushed 2 files.

claspは git コマンドのような感じで使えるのでとっても扱いやすいです!
今回はスプレッドシートでのGASの使い方をご紹介しましたが、このほかにもGoogleドキュメントやプレゼンテーションでも利用できるようなので、幅広い使い方ができそうです!