可在 Apps Script 專案中執行函式。指令碼專案必須部署到 Apps Script API,且呼叫應用程式必須共用相同的 Cloud Platform 專案。
這種方法需要使用 OAuth 2.0 憑證來授權;該憑證包含至少一個「Authorization」(授權) 部分所列的範圍;不需要授權的指令碼專案無法透過此 API 執行。如要尋找要在驗證憑證中加入的正確範圍,請開啟指令碼專案「總覽」頁面,然後向下捲動至「專案 OAuth 範圍」。
錯誤 403, PERMISSION_DENIED: The caller does not have permission
表示用來授權要求的 Cloud Platform 專案與指令碼所使用的專案不同。
HTTP 要求
POST https://script.googleapis.com/v1/scripts/{scriptId}:run
這個網址使用 gRPC 轉碼語法。
路徑參數
參數 | |
---|---|
scriptId |
要執行的指令碼的指令碼 ID。在「專案設定」頁面的「ID」下方找出指令碼 ID。 |
要求主體
要求主體的資料會採用以下結構:
JSON 表示法 |
---|
{ "function": string, "parameters": [ value ], "sessionState": string, "devMode": boolean } |
欄位 | |
---|---|
function |
要在指定指令碼中執行的函式名稱。這個名稱不含括號或參數。可參照所含程式庫 (例如 |
parameters[] |
要傳遞至執行函式的參數。每個參數的物件類型應與 Apps Script 中的預期類型相符。參數不能是 Apps Script 專用的物件類型 (例如 |
sessionState |
已淘汰。僅適用於 Android 外掛程式。這個 ID 代表使用者在 Google 文件或試算表 Android 版應用程式中目前使用的工作階段,並以額外資料的形式納入啟動外掛程式的 Intent 中。使用工作階段狀態執行 Android 外掛程式時,會取得繫結指令碼的權限。也就是說,該程式碼可存取使用者目前的遊標位置 (在 Google 文件中) 或所選儲存格 (在 Google 試算表中) 等資訊。如要擷取狀態,請呼叫 |
devMode |
如果 |
回應主體
如果成功,回應主體即會包含具有以下結構的資料:
表示以 run
開始的 Apps Script 函式執行作業。函式完成執行後,才會收到執行回應。如需執行時間上限,請參閱 Apps 指令碼配額指南。
執行完畢後,可能會有以下四個結果之一:
- 如果指令碼函式成功傳回,
response
欄位會包含ExecutionResponse
物件,且該物件的result
欄位含有函式的傳回值。 - 如果指令碼函式 (或 Apps Script 本身) 擲回例外狀況,
error
欄位會包含Status
物件。Status
物件的details
欄位包含一個含有單一ExecutionError
物件的陣列,可提供錯誤性質的相關資訊。 - 如果執行作業尚未完成,
done
欄位即為false
,且response
和error
欄位皆不存在。 - 如果
run
呼叫本身失敗 (例如因要求格式錯誤或授權錯誤),此方法會傳回 4XX 範圍的 HTTP 回應代碼,而且回應格式也不同。用戶端程式庫會將 4XX 回應自動轉換為例外狀況類別。
JSON 表示法 |
---|
{ "done": boolean, // Union field |
欄位 | |
---|---|
done |
這個欄位會指出指令碼是否已執行完畢。已完成的執行作業會填入填入的 |
聯集欄位 result 。這是作業的執行結果,可能為 error ,或是有效的 response 。如果 done == false ,系統不會將結果設定為 error ,也不會設定為 response 。如果 done == true ,則可以只設定 error 或 response 其中之一。請注意,部分服務可能無法提供結果。result 只能是下列其中一個設定: |
|
error |
如果 |
response |
如果指令碼函式傳回成功,這個欄位會包含含有函式傳回值的 包含任意類型欄位的物件。額外的 |
授權範圍
需要下列其中一種 OAuth 範圍:
https://apps-apis.google.com/a/feeds
https://apps-apis.google.com/a/feeds/alias/
https://apps-apis.google.com/a/feeds/groups/
https://mail.google.com/
https://sites.google.com/feeds
https://www.google.com/calendar/feeds
https://www.google.com/m8/feeds
https://www.googleapis.com/auth/admin.directory.group
https://www.googleapis.com/auth/admin.directory.user
https://www.googleapis.com/auth/documents
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/dynamiccreatives
https://www.googleapis.com/auth/forms
https://www.googleapis.com/auth/forms.currentonly
https://www.googleapis.com/auth/groups
https://www.googleapis.com/auth/script.cpanel
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/script.scriptapp
https://www.googleapis.com/auth/script.send_mail
https://www.googleapis.com/auth/script.storage
https://www.googleapis.com/auth/script.webapp.deploy
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/spreadsheets.currentonly
https://www.googleapis.com/auth/sqlservice
https://www.googleapis.com/auth/userinfo.email
詳情請參閱 OAuth 2.0 總覽。
狀態
如果 run
呼叫成功,但指令碼函式 (或 Apps Script 本身) 會擲回例外狀況,則回應主體的 error
欄位會包含這個 Status
物件。
JSON 表示法 |
---|
{ "code": integer, "message": string, "details": [ { "@type": string, field1: ..., ... } ] } |
欄位 | |
---|---|
code |
狀態碼。針對這個 API,這個值:
|
message |
向開發人員顯示的錯誤訊息,僅提供英文版。所有向使用者顯示的錯誤訊息都會經過本地化,並在 |
details[] |
包含單一 包含任意類型欄位的物件。額外的 |