2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

claspを使用してGAS開発

Posted at

claspとは

clasp(クラスポ) は「Command Line Apps Script Projects」の略で、
Google が公式で出してる Apps Script をローカルPCで管理するためのツール です。

claspを使うメリット

・ローカルPCに .gs ファイルや appsscript.json を保存できる
・VSCode など好きなエディタで編集できる
・GitHub に push して履歴管理できる
・clasp push でローカル → GAS にアップロード
・clasp pull で GAS → ローカルにダウンロード

clasp導入方法

  1. 事前準備
    Node.js を入れる
    Googleアカウント(GASと同じ)
  2. clasp を入れる & ログイン
npm i -g @google/clasp
clasp login

→ ブラウザで許可
2) 既存GASプロジェクトをローカルに落とす(おすすめ)
(すでにスプレッドシートに作ったGASを同期したい場合)

スクリプトエディタ(Apps Script)で プロジェクトのURL を開く
例: https://script.google.com/u/0/home/projects/[SCRIPT_ID]/edit

ローカルの空ディレクトリで:

clasp clone <SCRIPT_ID>

できあがるファイル:
Code.gs / そのほか *.gs
appsscript.json(マニフェスト)
.clasp.json(設定)

image.png

■以後
ローカル → GAS:clasp push
GAS → ローカル:clasp pull

<よく使うコマンド>

# 反映(ローカル → GAS)
clasp push
# 取得(GAS → ローカル)
clasp pull
# 新しいデプロイバージョン作成(履歴メモ付き)
clasp version "feat: add timestamp to PDF"
# バージョン一覧
clasp versions
# 開く(ブラウザでGASプロジェクト)
clasp open

<Git hub運用>

git init
git add .
git commit -m "initial commit"
git remote add origin git@github.com:you/your-repo.git
git push -u origin main

変更ごとに clasp push → 実環境へ反映
リリース時は clasp version "message" → CHANGELOG更新

<よくあるハマりどころ & 対処>
・権限不足/スコープ不足:appsscript.json の "oauthScopes" に必要スコープがあるか確認。初回はメニューの「権限付与」実行。
・rootDir の不一致:clasp push が「何もない」時は .clasp.json の "rootDir" と実ディレクトリが合ってるか確認。
・親紐づけできてない:.clasp.json の "parentId" にスプレッドシートIDを入れる。
・誰かの編集で競合:clasp pull で取り込んでから手元修正→clasp push。
・共有ドライブ保存:drive.file スコープで基本行けるが、組織ポリシーで制限がある場合は権限確認。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?