在 Apps Script 和 JavaScript 中,執行階段或執行階段環境包含用來剖析及執行指令碼程式碼的 JavaScript 引擎。執行階段會針對記憶體的存取方式、程式如何與電腦作業系統互動,以及何種程式語法合法。每個網路瀏覽器都有一個 JavaScript 執行階段環境。
Apps Script 以往都是由 Mozilla 的 Rhino JavaScript 解譯器驅動。雖然 Rhino 提供了一種便利的方式,讓 Apps Script 執行開發人員指令碼,但 Rhino 也能將 Apps Script 連結至特定的 JavaScript 版本 (ES5)。Apps Script 開發人員無法在使用 Rhino 執行階段的指令碼中使用更新版 JavaScript 語法和功能。
為解決這個問題,採用 Chrome 和 Node.js 的 V8 執行階段現已支援 Apps Script。您可以將現有指令碼遷移至 V8,以便使用新型 JavaScript 語法和功能。
本頁說明 V8 啟用的新功能,以及如何在指令碼中啟用 V8。將指令碼遷移至 V8 一文說明瞭將現有指令碼遷移至 V8 執行階段的步驟。
V8 執行階段的功能
使用 V8 執行階段的指令碼可利用下列功能:
新版 ECMAScript 語法
您可以在採用 V8 執行階段的指令碼中使用新型 ECMAScript 語法。這個語法包含 let
、const
和許多其他熱門功能。
請參閱 V8 語法範例,瞭解您可以使用 V8 執行階段完成的熱門語法改善項目。
改善函式偵測
我們改善了使用 V8 指令碼的 Apps Script 函式偵測功能。新的執行階段可辨識以下函式定義格式:
function normalFunction() {} async function asyncFunction() {} function* generatorFunction() {} var varFunction = function() {} let letFunction = function() {} const constFunction = function() {} var namedVarFunction = function alternateNameVarFunction() {} let namedLetFunction = function alternateNameLetFunction() {} const namedConstFunction = function alternateNameConstFunction() {} var varAsyncFunction = async function() {} let letAsyncFunction = async function() {} const constAsyncFunction = async function() {} var namedVarAsyncFunction = async function alternateNameVarAsyncFunction() {} let namedLetAsyncFunction = async function alternateNameLetAsyncFunction() {} const namedConstAsyncFunction = async function alternateNameConstAsyncFunction() {} var varGeneratorFunction = function*() {} let letGeneratorFunction = function*() {} const constGeneratorFunction = function*() {} var namedVarGeneratorFunction = function* alternateNameVarGeneratorFunction() {} let namedLetGeneratorFunction = function* alternateNameLetGeneratorFunction() {} const namedConstGeneratorFunction = function* alternateNameConstGeneratorFunction() {} var varLambda = () => {} let letLambda = () => {} const constLambda = () => {} var varAsyncLambda = async () => {} let letAsyncLambda = async () => {} const constAsyncLambda = async () => {}
從觸發條件和回呼呼叫物件方法
使用 V8 的指令碼可以從原本可以呼叫程式庫方法的位置呼叫物件方法和類別靜態方法。這些位置包括:
- Google Workspace 外掛程式資訊清單觸發條件
- 可安裝觸發條件
- 編輯器中的選單項目 Google Workspace
- 使用者回呼函式,例如
ScriptApp.newStateToken()
程式碼範例中所述的函式。
以下 V8 範例說明如何使用物件方法,在 Google 試算表中建構選單項目:
function onOpen() {
var ui = SpreadsheetApp.getUi(); // Or DocumentApp, SlidesApp, or FormApp.
ui.createMenu('Custom Menu')
.addItem('First item', 'menu.item1')
.addSeparator()
.addSubMenu(ui.createMenu('Sub-menu')
.addItem('Second item', 'menu.item2'))
.addToUi();
}
var menu = {
item1: function() {
SpreadsheetApp.getUi().alert('You clicked: First item');
},
item2: function() {
SpreadsheetApp.getUi().alert('You clicked: Second item');
}
}
查看記錄檔
Apps Script 提供兩種記錄服務:Logger
服務和 console
類別。這兩種服務都會將記錄寫入相同的 Stackdriver Logging 服務。
如要顯示 Logger
和 console
記錄,請按一下指令碼編輯器頂端的「Execution log」(執行記錄)。
查看執行作業
如要查看指令碼的執行記錄,請開啟 Apps Script 專案,然後按一下左側的「Executions」圖示
。V8 語法範例
下列簡短清單列出了使用 V8 執行階段指令碼時,可以使用哪些熱門語法功能。
let
和const
let
和 const
關鍵字可讓您分別定義區塊範圍本機變數及區塊範圍常數。
// V8 runtime let s = "hello"; if (s === "hello") { let s = "world"; console.log(s); // Prints "world" } console.log(s); // Prints "hello" const N = 100; N = 5; // Results in TypeError |
箭頭函式
箭頭函式提供用於定義運算式內函式的精簡方法。
// Rhino runtime function square(x) { return x * x; } console.log(square(5)); // Outputs 25 |
// V8 runtime const square = x => x * x; console.log(square(5)); // Outputs 25 // Outputs [1, 4, 9] console.log([1, 2, 3].map(x => x * x)); |
類別
類別提供透過繼承在概念上整理程式碼的方法。V8 中的類別主要是 JavaScript 原型式繼承的語法糖。
// V8 runtime class Rectangle { constructor(width, height) { // class constructor this.width = width; this.height = height; } logToConsole() { // class method console.log(`Rectangle(width=${this.width}, height=${this.height})`); } } const r = new Rectangle(10, 20); r.logToConsole(); // Outputs Rectangle(width=10, height=20) |
解構作業
解構指派運算式是將陣列和物件內值拆分為相異變數的快速方法。
// Rhino runtime var data = {a: 12, b: false, c: 'blue'}; var a = data.a; var c = data.c; console.log(a, c); // Outputs 12 "blue" var array = [1, 2, 3]; var x = a[0]; var y = a[1]; var z = a[2]; console.log(x, y, z); // Outputs 1 2 3 |
// V8 runtime var data = {a: 12, b: false, c: 'blue'}; var {a, c} = data; console.log(a, c); // Outputs 12 "blue" var array = [1, 2, 3]; var [x, y, z] = array; console.log(x, y, z); // Outputs 1 2 3 |
範本常值
範本常值是允許內嵌運算式的字串常值。可避免更複雜的字串串連陳述式。
// Rhino runtime var name = 'Hi ' + first + ' ' + last + '.'; var url = 'http://localhost:3000/api/messages/' + id; |
// V8 runtime var name = `Hi ${first} ${last}.`; var url = `http://localhost:3000/api/messages/${id}`; |
預設參數
預設參數可讓您指定函式宣告中函式參數的預設值。這可以簡化函式主體中的程式碼,因為這樣就不必將預設值明確指派給遺漏的參數。
// Rhino runtime function hello(greeting, name) { greeting = greeting || "hello"; name = name || "world"; console.log( greeting + " " + name + "!"); } hello(); // Outputs "hello world!" |
// V8 runtime var hello = function(greeting="hello", name="world") { console.log( greeting + " " + name + "!"); } hello(); // Outputs "hello world!" |
多行字串
您可以使用與範本常值相同的語法定義多行字串。與範本文字相同,這個語法可避免字串串連,並簡化字串定義。
// Rhino runtime var multiline = "This string is sort of\n" + "like a multi-line string,\n" + "but it's not really one."; |
// V8 runtime var multiline = `This on the other hand, actually is a multi-line string, thanks to JavaScript ES6`; |
啟用 V8 執行階段
如果指令碼使用的是 Rhino 執行階段,您可以按照下列步驟將其切換至 V8:
- 開啟 Apps Script 專案。
- 按一下左側的「專案設定」圖示 。
- 勾選「啟用 Chrome V8 執行階段」核取方塊。
您也可以編輯指令碼資訊清單檔案,直接指定指令碼執行階段:
- 開啟 Apps Script 專案。
- 按一下左側的「專案設定」圖示 。
- 勾選「在編輯器中顯示「appsscript.json」資訊清單檔案」核取方塊。
- 按一下左側的「Editor」圖示
appsscript.json
」。
>「 - 在
appsscript.json
資訊清單檔案中,將runtimeVersion
欄位設為V8
值。 - 按一下頂端的「儲存專案」圖示 。
將指令碼遷移至 V8 一文說明還有哪些步驟能確保指令碼正常運作,使用 V8。
啟用 Rhino 執行階段
如果您的指令碼使用 V8,而且您必須將其切換為使用原始 Rhino 執行階段,請執行下列步驟:
- 開啟 Apps Script 專案。
- 按一下左側的「專案設定」圖示 。
- 取消勾選「啟用 Chrome V8 執行階段」核取方塊。
或者,編輯指令碼資訊清單:
- 開啟 Apps Script 專案。
- 按一下左側的「專案設定」圖示 。
- 勾選「在編輯器中顯示「appsscript.json」資訊清單檔案」核取方塊。
- 按一下左側的「Editor」圖示
appsscript.json
」。
>「 - 在
appsscript.json
資訊清單檔案中,將runtimeVersion
欄位設為DEPRECATED_ES5
值。 - 按一下頂端的「儲存專案」圖示 。
如何遷移現有的指令碼?
將指令碼遷移至 V8 指南說明遷移現有指令碼以使用 V8 時必須採取的步驟。這項作業會啟用 V8 執行階段,並檢查指令碼是否存在任何已知的不相容問題。
自動將指令碼遷移至 V8
自 2020 年 2 月 18 日起,Google 會開始逐步將通過自動相容性測試的現有指令碼遷移至 V8。遷移後,受影響的指令碼會繼續正常運作。
如要選擇不採用自動遷移指令碼,請將資訊清單中的 runtimeVersion
欄位設為 DEPRECATED_ES5
。之後您隨時都可以選擇手動將指令碼遷移至 V8。
如何回報錯誤?
支援指南說明瞭如何在 Stack Overflow 上取得程式設計說明、搜尋現有問題報告、回報新錯誤,以及提出新功能要求。