LoginSignup
4
2

More than 3 years have passed since last update.

【GAS】clasp + TypeScript をV8ランタイムに対応させて最新のES構文を利用する

Last updated at Posted at 2020-03-16

概要

GAS でV8ランタイムが利用できるようになりました。
V8 Runtime Overview  |  Apps Script  |  Google Developers

これによりES2019で追加された関数(flatMap, trimStart/trimEnd など)が利用できます。
参考: JavaScriptの ES2019で追加された新機能まとめ - Qiita

clasp

Command Line Interface using clasp

clasp is an open-source tool, separate from the Apps Script platform, that lets you develop and manage Apps Script projects from your terminal rather than the Apps Script editor

ざっくりいうとGASをブラウザ上ではなくローカル開発するための公式ツール

TypeScript でGASを書く

https://github.com/google/clasp/blob/master/docs/typescript.md
こちらに従ってセットアップすれば TypeScript で開発できるようになります。

V8ランタイムに対応する

TypeScript で作成したプロジェクトで最新のES構文を利用できるようにします。
clasp のV8対応のプルリク を参考に変更します。

ターゲットを変更

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2019"
  }
}

GASランタイムをV8に変更

appsscript.json
{
  "runtimeVersion": "V8"
}

結果

ES2019の構文が利用できるようになる

main.ts
const threads = GmailApp.search("from:mail@example.com")
const messages: GoogleAppsScript.Gmail.GmailMessage[] = threads.flatMap(t => t.getMessages())
messages.forEach(m => {
  const body = m.getPlainBody()
  Logger.log(body.trimEnd())
})

参考

r57ty7/toho-schedule-creator: TOHOシネマズの購入完了メールから自動で Google Calendar に登録するGAS

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