僕の世界観を変えてみる

文系男子が趣味でプログラミングを勉強していくブログです。他にも日常で起きたどうでもいいことや愚痴を書いていきたいです。座右の銘は和を以て貴しとなすです。仲良くやろうよ。

claspでGASのバージョン管理をする【Mac】

nodebrew install

% brew install nodebrew

Error: The following directories are not writable by your use

error

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のバージョン管理ツール

f:id:htmllifehack:20200625221647p:plain
node.js

バージョンをターミナルで変更できる。

chownはディレクトリの所有権の変更を行うコマンド -Rchownのオプション、下層のディレクトリもまとめて所有権を変更する。 $(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

error Fetching: https://nodejs.org/dist/v14.4.0/node-v14.4.0-darwin-x64.tar.gz Warning: Failed to create the file Warning: /Users/***/.nodebrew/src/v14.4.0/node-v14.4.0-darwin-x64.tar.gz: Warning: No such file or directory 0.0% curl: (23) Failed writing body (0 != 980) download failed: https://nodejs.org/dist/v14.4.0/node-v14.4.0-darwin-x64.tar.gz

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サービスを作成する場合はいずれかを選択することができます。

error

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できるようになるはずです。