1
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?

More than 1 year has passed since last update.

WSL2(Ubuntu20.04LTS)とclaspを用いてGASのローカル環境を構築する

Last updated at Posted at 2023-03-20

はじめに

こんにちは! @Keichan_15 です!

つい最近Zennデビューを果たしましたのでちょろっと宣伝がてらのQiita執筆になります(笑)

Qiitaでも一応備忘録を兼ねて残しておくって感じ。内容自体は特段Zennと変わらないので、どっち見て頂いても大丈夫です(笑)

事前準備

今回は、 WSL2が導入済であることを前提 として進めていきます。WSLの導入方法については自身が過去に執筆した記事をご覧ください!

記事内で導入する項目のうち、行って頂きたい項目は以下の2つになります。

  • WSL2のインストール
  • Node.jsのインストール

インストールが完了したら早速claspを導入していきましょう!

導入

実行環境の構築

まず、任意のディレクトリを作成します。今回は clasp_demo というディレクトリを作成します。作成が完了したら同時にディレクトリも移動しておきましょう。

Ubuntu
$ mkdir clasp_demo
$ cd clasp_demo

ついでに、以降はVSCodeのターミナルを使用して導入を行うため、追加で下記のコマンドを入力します。

Ubuntu
$ code .

このコードを入力することでVSCodeの拡張機能をインストールすることが可能です。

次に、念のためNode.jsとnpmが導入されているか確認します。

Ubuntu
$ node --version
v16.17.0

$ npm --version
8.15.0

若干表示されるバージョンの違いはあるかと思いますが、このように表示されていれば問題ありません。

Google Apps Script APIの有効化

claspを使用するためにGoogle Apps Script APIを有効化にする必要があります。
下記のページから有効化しましょう。

パッケージの導入

次に必要なパッケージをインストールしていきます。

Ubuntu
$ npm init -y
$ npm install @google/clasp -g
$ npm install @types/google-apps-script

ちなみに僕はここでコケました。2つ目のコマンドである clasp のインストールで下記のエラーにぶち当たります。

Ubuntu
npm notice
npm notice New major version of npm available! 8.15.0 -> 9.6.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.2
npm notice Run npm install -g npm@9.6.2 to update!
npm notice
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/@google/clasp
npm ERR! dest /usr/local/lib/node_modules/@google/.clasp-L3XGxwFD
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/@google/clasp' -> '/usr/local/lib/node_modules/@google/.clasp-L3XGxwFD'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/@google/clasp' -> '/usr/local/lib/node_modules/@google/.clasp-L3XGxwFD'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/@google/clasp',
npm ERR!   dest: '/usr/local/lib/node_modules/@google/.clasp-L3XGxwFD'
npm ERR! }
npm ERR!
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/keichan/.npm/_logs/2023-03-16T12_19_13_632Z-debug-0.log

あれ~と思ったのですが、よくよくログを見てみると、

Ubuntu
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/@google/clasp' -> '/usr/local/lib/node_modules/@google/.clasp-L3XGxwFD'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/@google/clasp' -> '/usr/local/lib/node_modules/@google/.clasp-L3XGxwFD'] 

permission denied の文字があるではないか!ということで、 sudo で実行すると問題無く通りました。

Ubuntu
$ npm init -y
$ sudo npm install @google/clasp -g
$ sudo npm install @types/google-apps-script

余談
npmのupdate通知が入っていましたので一緒にやっておきました。別にこれに関してはやらなくても良いと思うけど一応ね。

Ubuntu
npm notice New major version of npm available! 8.15.0 -> 9.6.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.2
npm notice Run npm install -g npm@9.6.2 to update!
Ubuntu
$ sudo npm install -g npm@9.6.2
$ npm --version
9.6.2

Googleアカウント連携

パッケージの導入が完了したらGoogleアカウントと連携します。下記のコマンドを入力してください。

Ubuntu
$ clasp login
Warning: You seem to already be logged in *globally*. You have a ~/.clasprc.json
Logging in globally…
🔑 Authorize clasp by visiting this url:
https://accounts.google.com/o/oauth2/v2/auth?access_type=xxxxxoxxxox

おそらく自動でGoogle Chromeが開くと思います。画面の説明に沿ってアカウントを選択して連携します。

見切れていますが、下にスクロールすると 許可 のボタンが表示されると思います。

許可が通り下記のような画面が表示されるとOKです!

ターミナルにも下記のようなメッセージが表示されていると思います。

Ubuntu
Authorization successful.

作成

準備が完了したのでGASアプリケーションを新規作成していきましょう!

Ubuntu
$ clasp create
? Create which script? (Use arrow keys)
❯ standalone 
  docs 
  sheets 
  slides 
  forms 
  webapp 
  api 

clasp create を入力した後、どの形式でGASを作成するか選択できます。今回は standalone を選択します。

Ubuntu
$ clasp create
? Create which script? standalone
Created new standalone script: https://script.google.com/d/1B-xxxoxxxoxxxx/edit
Warning: files in subfolder are not accounted for unless you set a '/home/keichan/clasp_demo/.claspignore' file.
Cloned 1 file.
└─ /home/keichan/clasp_demo/appsscript.json

Googleドライブのマイドライブ直下の位置にアプリケーションが作成されたことが確認できると思います。

これでスクリプトの準備ができました。
次に今回はテストで main.js を作成し、Hello World をログ出力するコードを実装していきます。

まず、 main.js を作成します。

Ubuntu
$ touch main.js

中身はお決まりの console.log での出力。

main.js
function test() {
  console.log('Hello World!');
}

実装が完了したら、GASにソースファイルをpushしましょう。下記のコマンドを入力します。

Ubuntu
$ clasp push

次にpushした先のGoogle環境にあるアプリケーションをWebエディター(従来のGASエディター)を開きます。下記のコマンドを入力します。

Ubuntu
$ clasp open

以下のようにGoogle ChromeにてGASのエディターが開けばOKです!お疲れ様でした!

ちなみに今回は紹介しませんが、claspには他にも便利なコマンドが数多く存在しています。是非皆さんもclaspを使用してローカルでGAS触っちゃいましょう~~!

参考記事

おわりに

シンプルに眠いのと全く動きたくない病が絶賛発動中なのですが、これ改善方法ありますかね…?
ちなみに最近はジムサボってます。以上。
ばいばい:v:

1
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
1
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?