執行 Apps Script 函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
透過 Apps Script API (舊稱 Apps Script Execution API),您可以在有權存取的指令碼專案中,遠端執行函式。您的應用程式可以呼叫指定的 Apps Script 函式,視需要提供輸入參數,並接收傳回的回應。
本頁的範例說明如何透過 API 執行一些常見的執行作業。如要瞭解詳情 (包括特別授權規定),請參閱「執行函式」指南。
在這些範例中,預留位置 scriptId 用於指出您提供指令碼專案 ID 的位置。請按照下列步驟找出指令碼 ID:
- 在 Apps Script 專案中,按一下左上方的「專案設定」圖示 settings。
- 按一下「指令碼 ID」旁邊的「複製」。
執行一項函式
下列 scripts.run 要求會呼叫名為 listFolderContent
的 Apps Script 函式,並將 Drive folderId 和整數 MAX_SIZE
做為引數傳遞。函式會在開發模式中執行,也就是說,系統會執行最近儲存的函式版本,無論部署為可執行檔的版本為何。
要求通訊協定如下所示。「執行函式」指南說明如何使用 Google API 用戶端程式庫,以不同語言實作執行要求。
POST https://script.googleapis.com/v1/scripts/scriptId:run
{
"function": "listFolderContent",
"parameters": [
folderId,
MAX_SIZE
],
"devMode": true
}
呼叫的 Apps Script 函式完成後,這項要求的回應會包含執行結果或錯誤回應。在本例中,函式會成功傳回檔案名稱陣列:
{
"response": {
"result": [
"fileTitle1",
"fileTitle2",
"fileTitle3"
]
},
}
如果函式在執行 Apps Script 時發生錯誤,回應可能如下所示:
{
"response": {
"error": {
"code": 3,
"message": "ScriptError",
"details": [{
"@type": "type.googleapis.com/google.apps.script.v1.ExecutionError",
"errorMessage": "The script enountered an exeception it could not resolve.",
"errorType": "ScriptError",
"scriptStackTraceElements": [{
"function": "listFolderContent",
"lineNumber": 14
}]
}]
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Apps Script API enables remote execution of functions within your accessible script projects.\u003c/p\u003e\n"],["\u003cp\u003eYou can provide input parameters to the functions and receive corresponding responses.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the "Executing a function" guide for comprehensive information, including authorization requirements.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples demonstrate how to execute functions and handle potential errors using the API.\u003c/p\u003e\n"]]],[],null,["# Executing Apps Script Functions\n\nThe Apps Script API (and formerly the Apps Script Execution API) allows you\nto remotely execute a function in a script project you have access to. Your\napp can call a given Apps Script function, providing it input parameters if\nneeded, and receive a returned response.\n\nThe examples on this page illustrate how some common execution operations can\nbe achieved with the API. For more information **including special\n[authorization requirements](/apps-script/api/how-tos/execute#requirements)** ,\nsee the [Executing a function](/apps-script/api/how-tos/execute) guide.\n\nIn these examples, the placeholders \u003cvar translate=\"no\"\u003escriptId\u003c/var\u003e\nis used to indicate where you would provide the script project ID. Follow the\nsteps below to find the script ID:\n\n1. In the Apps Script project, at the top left, click **Project Settings** settings.\n2. Next to \"Script ID,\" click **Copy**.\n\nExecute a function\n------------------\n\nThe following [scripts.run](/apps-script/api/reference/rest/v1/scripts/run)\nrequest calls an Apps Script function named `listFolderContent`, passing it\nthe Drive \u003cvar translate=\"no\"\u003efolderId\u003c/var\u003e and an integer `MAX_SIZE` as arguments. The\nfunction is executed in development mode, meaning that the most recently\nsave version of the function is executed, regardless of what version is\ndeployed as an executable.\n\nThe request protocol is shown below. The\n[Executing functions](/apps-script/api/how-tos/execute) guide\nshows how to implement a run request in different languages using the Google\nAPI client libraries. \n\n```\nPOST https://script.googleapis.com/v1/scripts/scriptId:run\n``` \n\n```scdoc\n{\n \"function\": \"listFolderContent\",\n \"parameters\": [\n folderId,\n MAX_SIZE\n ],\n \"devMode\": true\n}\n```\n\nThe [response](/apps-script/api/reference/rest/v1/scripts/run#response-body)\nto this request, once the called Apps Script function completes,\ncontains the results of the execution or an error response. In\nthis example, the function successfully returns an array of file names: \n\n```text\n{\n \"response\": {\n \"result\": [\n \"fileTitle1\",\n \"fileTitle2\",\n \"fileTitle3\"\n ]\n },\n}\n```\n\nIf the function encountered an error during the Apps Script execution, the\nresponse could look like this: \n\n```carbon\n{\n \"response\": {\n \"error\": {\n \"code\": 3,\n \"message\": \"ScriptError\",\n \"/apps-script/api/reference/rest/v1/ExecutionError\": [{\n \"@type\": \"type.googleapis.com/google.apps.script.v1.ExecutionError\",\n \"errorMessage\": \"The script enountered an exeception it could not resolve.\",\n \"errorType\": \"ScriptError\",\n \"/apps-script/api/reference/rest/v1/ExecutionError#ScriptStackTraceElement\": [{\n \"function\": \"listFolderContent\",\n \"lineNumber\": 14\n }]\n }]\n }\n }\n}\n```"]]