LoginSignup
0
0

More than 1 year has passed since last update.

Google Apps Script の内容を grep したいので clasp 使う

Last updated at Posted at 2022-10-25

はじめに

  • いわゆる gs ファイルを grep したかったのだが、2022年10月現在の script.google.com には、残念ながらその機能はなさそう。
  • というわけで、やむなく、clasp で local に pull して、grep する手順のメモをここに書く。
  • この記事は、MacOSX を想定している。
  • が、結論だけ書くと、元々やりたかったcontainer bound な script 一覧をまとめて clone するみたいなことはできなかった。悲しいが、敗北した。
  • 結論としては、GASも GitHub で管理しようぜ、である。
  • 以下は、個別の gs ファイルを clone するだけの手順。

clasp のインストール

clasp とは、Google Apps Script を Local 開発するためのツールである。

homebrew で nodebrew を install

$ brew install nodebrew 

nodebrew の PATH を通す(以下を ~/.zshrc にでも書いてください)

export PATH=$HOME/.nodebrew/current/bin:$PATH

書いたら、source して読み込み

$ source ~/.zshrc

source の download 用ディレクトリを作成して、latest を install

$ mkdir -p ~/.nodebrew/src
$ nodebrew install-binary latest

最新版が落ちてくるが、current が none なので、use して指定してあげてください。

$ nodebrew ls
v19.0.0

current: none
$ nodebrew use v19.0.0
use v19.0.0

ここまできて、やっと clasp install

$ npm install -g  @google/clasp
$ clasp -h
Usage: clasp <command> [options]

clasp - The Apps Script CLI

Options:
  -v, --version                               output the current version
  -A, --auth <file>                           path to an auth file or a folder with a '.clasprc.json' file.
  -I, --ignore <file>                         path to an ignore file or a folder with a '.claspignore' file.
  -P, --project <file>                        path to a project file or to a folder with a '.clasp.json' file.
  -h, --help                                  display help for command

Commands:
  login [options]                             Log in to script.google.com
  logout                                      Log out
  create [options]                            Create a script
  clone [options] [scriptId] [versionNumber]  Clone a project
  pull [options]                              Fetch a remote project
  push [options]                              Update the remote project
  status [options]                            Lists files that will be pushed by clasp
  open [options] [scriptId]                   Open a script
  deployments                                 List deployment ids of a script
  deploy [options]                            Deploy a project
  undeploy [options] [deploymentId]           Undeploy a deployment of a project
  version [description]                       Creates an immutable version of the script
  versions                                    List versions of a script
  list [options]                              List App Scripts projects
  logs [options]                              Shows the StackDriver logs
  run [options] [functionName]                Run a function in your Apps Scripts project
  apis [options]                              List, enable, or disable APIs
    list
    enable <api>
    disable <api>
  setting|settings [settingKey] [newValue]    Update <settingKey> in .clasp.json
  *                                           Any other command is not supported
  paths                                       List current config files path
  help [command]                              display help for command

clasp 設定

login しないと始まらないので、とりあえず login

$ clasp login

ブラウザに飛ばされて以下のような認可画面がでるので、許可する。

スクリーンショット 2022-10-26 1.12.11.png

そうすると、ターミナルのほうで

Authorization successful.

Default credentials saved to: /Users/foobar/.clasprc.json.

とでて ~/.clasprc.json に token が保存される。

Web の UI でつくったものを clone してみる

スプレッドシートに紐づくgas(container bound な script)がこんな感じであるとする。

スクリーンショット 2022-10-26 1.56.22.png

File -> Project properties -> Info -> Script ID で、script id を拾ってくる。

$ clasp clone ${script_id}

これで、local に コードが落ちてくる

$ tree
.
├── appsscript.json
└── コード.js

日本語が微妙だが...

 $ cat "コード.js"
function myFunction() {
  Browser.msgBox('hello123');
}

"コード.js" に期待のものが入っている。

$ grep "hello123" ./*
./コード.js:  Browser.msgBox('hello123');

これで grep できる。

だがしかし

このやり方は script id を拾ってくる部分が肝である(でないと、ポチポチブラウザから、script idを拾ってくる必要がある)。

現在のところどうも clasp では(clasp list)では、container bound な script 一覧を出力する機能がなさそう...。

これは、clasp がというより、元のREST API にそもそも container bound な script 一覧を出力する機能がなさそうな雰囲気...。

というわけで、冒頭に書いたように敗北した話でした...。

結論

GAS も GitHub で管理しよう。

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