Method: scripts.run

在 Apps Script 專案中執行函式。指令碼專案必須經過部署,才能搭配 Apps Script API 使用,且呼叫端應用程式必須共用同一個 Cloud Platform 專案。

這個方法需要使用 OAuth 2.0 權杖授權,且權杖至少須包含「授權」一節中列出的其中一個範圍;不需要授權的指令碼專案無法透過這個 API 執行。如要找出要納入驗證權杖的正確範圍,請開啟指令碼專案的「總覽」頁面,然後向下捲動至「專案 OAuth 範圍」。

錯誤 403, PERMISSION_DENIED: The caller does not have permission 表示用於授權要求的 Cloud Platform 專案,與指令碼使用的專案不同。

HTTP 要求

POST https://script.googleapis.com/v1/scripts/{deploymentId}:run

這個網址使用 gRPC 轉碼語法。

路徑參數

參數
deploymentId

string

API 可執行檔部署作業的部署 ID。在指令碼編輯器中,依序點選「部署」>「管理部署作業」,然後找出部署作業 ID

要求主體

要求主體會包含結構如下的資料:

JSON 表示法
{
  "function": string,
  "parameters": [
    value
  ],
  "sessionState": string,
  "devMode": boolean
}
欄位
function

string

要在指定指令碼中執行的函式名稱。名稱不含括號或參數。它可以參照所含程式庫中的函式,例如 Library.libFunction1

parameters[]

value (Value format)

要傳遞至執行中函式的參數。每個參數的物件類型都應與 Apps Script 中的預期類型相符。參數不能是 Apps Script 專屬的物件型別 (例如 DocumentCalendar),只能是原始型別,例如 stringnumberarrayobjectboolean。選用項目。

sessionState

string

已淘汰。僅適用於 Android 外掛程式。這個 ID 代表使用者目前在 Google 文件或試算表 Android 應用程式中的工作階段,會以額外資料的形式納入啟動外掛程式的 Intent 中。如果 Android 外掛程式以工作階段狀態執行,就會取得繫結指令碼的權限,也就是說,可以存取使用者目前的游標位置 (在 Google 文件中) 或所選儲存格 (在 Google 試算表中) 等資訊。如要擷取狀態,請呼叫 Intent.getStringExtra("com.google.android.apps.docs.addons.SessionState")。選用項目。

devMode

boolean

如果 true,且使用者是指令碼擁有者,指令碼會以最近儲存的版本執行,而不是部署供 Apps Script API 使用的版本。選用,預設值為 false

回應主體

如果成功,回應主體會含有以下結構的資料:

表示以 run 啟動的 Apps Script 函式執行作業。函式執行完畢後,才會收到執行回應。執行階段時間上限請參閱 Apps Script 配額指南

執行開始後,可能會出現下列四種結果:

  • 如果指令碼函式順利傳回,response 欄位會包含 ExecutionResponse 物件,而該物件的 result 欄位則會包含函式的傳回值。
  • 如果指令碼函式 (或 Apps Script 本身) 擲回例外狀況,error 欄位會包含 Status 物件。Status 物件的 details 欄位包含陣列,其中含有單一 ExecutionError 物件,提供錯誤性質的相關資訊。
  • 如果執行作業尚未完成,done 欄位會是 false,且不會顯示 responseerror 欄位。
  • 如果 run 呼叫本身失敗 (例如要求格式錯誤或授權錯誤),方法會傳回 4XX 範圍內的 HTTP 回應代碼,且回應主體格式不同。用戶端程式庫會自動將 4XX 回應轉換為例外狀況類別。

JSON 表示法
{
  "done": boolean,

  // Union field result can be only one of the following:
  "error": {
    object (Status)
  },
  "response": {
    "@type": string,
    field1: ...,
    ...
  }
  // End of list of possible types for union field result.
}
欄位
done

boolean

這個欄位會指出指令碼執行作業是否已完成。執行作業完成後,系統會填入 response 欄位,其中包含所執行函式的 ExecutionResponse

聯集欄位 result。這是作業的執行結果,可能為 error,或是有效的 response。如果 done == false,系統不會將結果設定為 error,也不會設定為 response。如果 done == true,系統只能將結果設定為 errorresponse 其中之一。部分服務可能不會提供結果。result 只能是下列其中一項:
error

object (Status)

如果 run 呼叫成功,但指令碼函式 (或 Apps Script 本身) 擲回例外狀況,這個欄位會包含 Status 物件。Status 物件的 details 欄位包含單一 ExecutionError 物件的陣列,提供錯誤性質的相關資訊。

response

object

如果指令碼函式順利傳回,這個欄位會包含 ExecutionResponse 物件,其中含有函式的傳回值。

包含任意類型欄位的物件。額外的 "@type" 欄位則包含能辨識類型的 URI。範例:{ "id": 1234, "@type": "types.example.com/standard/id" }

授權範圍

需要下列其中一種 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

integer

狀態碼。對於這個 API,這個值可以是:

  • 10:表示發生 SCRIPT_TIMEOUT 錯誤,
  • 3,表示發生 INVALID_ARGUMENT 錯誤,或
  • 1,表示 CANCELLED 執行作業。

message

string

向開發人員顯示的錯誤訊息,應以英文呈現。所有向使用者顯示的錯誤訊息都應經過本地化,並透過 details 欄位傳送,或是由用戶端加以本地化。

details[]

object

包含單一 ExecutionError 物件的陣列,提供錯誤性質的相關資訊。

包含任意類型欄位的物件。額外的 "@type" 欄位則包含能辨識類型的 URI。例如:{ "id": 1234, "@type": "types.example.com/standard/id" }