claspでGASのバージョン管理をする【Mac】
- nodebrew install
- Error: The following directories are not writable by your use
- node.js install
- node.jsを有効化
- clasp install
- GASをcloneする
- ローカルで編集したコードをGASに反映させる
- ローカルからプロジェクトを新規作成
nodebrew install
% brew install nodebrew
Error: The following directories are not writable by your use
Error: The following directories are not writable by your user:
/usr/local/share/man/man8
You should change the ownership of these directories to your user.
% sudo chown -R $(whoami) /usr/local/share/man/man8
And make sure that your user has write permission.
% chmod u+w /usr/local/share/man/man8
※nodebrewとはnode.jsのバージョン管理ツール
バージョンをターミナルで変更できる。
chown
はディレクトリの所有権の変更を行うコマンド
-R
はchown
のオプション、下層のディレクトリもまとめて所有権を変更する。
$(whoami)
、whoami
の現在のユーザー名を表示、を変数化?
% nodebrew -v >> 出力結果 nodebrew 1.0.1
node.js install
node.js のバージョン確認
% nodebrew ls-remote >> 出力結果 v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.0.5 v0.0.6 . . . v14.0.0 v14.1.0 v14.2.0 v14.3.0 v14.4.0
nodebrew ls-remote
でnode.jsのバージョン一覧を確認できる。
% nodebrew install-binary stable or % nodebrew install-binary latest
install-binary stable
で推奨版(現在は12.18.1)、install-binary latest
で最新版(14.4.0)がインストールできる。
Warning: No such file or directory
4行目のWarningがエラー内容です。
ファイルかディレクトリがありませんとなっています。
なんのファイル、ディレクトリがないのかは3行目のWarningの.nodebrew/src/
です。
nodebrewのソースファイル(.がついているので隠しファイル)がないのでこれを作成する必要があるみたいです。
% mkdir -p ~/.nodebrew/src
mkdir -p
でフォルダがなければ作成すると言う意味です。
その後もう一度
% nodebrew install-binary stable or % nodebrew install-binary latest
でインストールされるはずです。
% nodebrew install-binary latest >> 実行結果 Fetching: https://nodejs.org/dist/v14.4.0/node-v14.4.0-darwin-x64.tar.gz ############################################### 100.0% Installed successfully
node.jsを有効化
% nodebrew ls >> 出力結果 v14.4.0 current: none
use バージョンでnode.jsを有効化。
% nodebrew use v14.4.0 use v14.4.0
Pathを通す
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zprofile
参考にさせていただいたサイト qiita.com
clasp install
% npm install -g @google/clasp
https://script.google.com/home/usersettingsにアクセスし、APIをオンにする。
% clasp login
でclasp
にログインするとブラウザが立ち上がり、Googleアカウントでログインすると設定は完了。
GASをcloneする
すでにGASを作成済みだったので、いったんローカルファイルにcloneする。
% mkdir GAS % cd GAS % clasp clone GASのスクリプトID >> 実行結果 Warning: files in subfolder are not accounted for unless you set a '.claspignore' file. Cloned 3 files. └─ test.js └─ コード.js └─ appsscript.json Not ignored files: └─ appsscript.json └─ test.js └─ コード.js Ignored files: └─ .clasp.json
GASからファイル→プロジェクトのプロパティ→スクリプトIDの順で辿るとIDがわかります。
ローカルで編集したコードをGASに反映させる
% clasp push
Gitと同様pushであげることができます。
ただし、ファイルを指定することはできずフォルダ内の全てのファイルをアップする形になります。
ローカルからプロジェクトを新規作成
claspはcreate
でプロジェクトを新規作成できます。
% clasp create ファイル名 % clasp create test_code ? Create which script? (Use arrow keys) ❯ standalone docs sheets slides forms webapp api
GASを作る場合はstandalone
を選択します。
他はドキュメントやスプレッドシート、スライダーなどおなじみのGoogleサービスを作成する場合はいずれかを選択することができます。
Nested clasp projects are not supported.
僕は上記のようなエラーが発生しました。
ホームディレクトリ内に.claps.json
ファイルが存在していると出るエラーのようです。
▼【Google Apps Script】claspを使ってローカル環境で開発する - こぶろぐ。
create
を行う前にclone
をしていたため一つ上のフォルダに.clasp.json
がありました。
.clasp.json
は隠しファイルです。
% ls -a
-a
でオプションを追加してls
すると隠しファイルも表示されます。
finder上ではcommand + shift + .
で表示非表示を切り替えられます。
.clasp.json
の中身はGASのスクリプトIDが記載されていました。
.clasp.json
を削除すればcreate
できるようになるはずです。