การใช้ฟังก์ชัน Apps Script
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Apps Script API (และเดิมคือ Apps Script Execution API) ช่วยให้คุณ
เรียกใช้ฟังก์ชันในโปรเจ็กต์สคริปต์ที่คุณมีสิทธิ์เข้าถึงจากระยะไกลได้
แอปของคุณเรียกใช้ฟังก์ชัน Apps Script ที่ระบุได้ โดยระบุพารามิเตอร์อินพุตหาก
จำเป็น และรับการตอบกลับที่ส่งคืน
ตัวอย่างในหน้านี้แสดงให้เห็นว่าการดำเนินการเรียกใช้ที่พบบ่อยบางอย่างสามารถทำได้ด้วย API อย่างไร ดูข้อมูลเพิ่มเติมรวมถึงข้อกำหนดในการให้สิทธิ์พิเศษได้ในคู่มือการเรียกใช้ฟังก์ชัน
ในตัวอย่างเหล่านี้ ระบบจะใช้ตัวยึดตำแหน่ง scriptId
เพื่อระบุตำแหน่งที่คุณจะระบุรหัสโปรเจ็กต์สคริปต์ ทำตาม
ขั้นตอนด้านล่างเพื่อค้นหารหัสสคริปต์
- ในโปรเจ็กต์ Apps Script ให้คลิกการตั้งค่าโปรเจ็กต์ที่ด้านซ้ายบน
settings
- คลิกคัดลอกข้าง "รหัสสคริปต์"
ดำเนินการฟังก์ชัน
การเรียกใช้คำขอ scripts.run
ต่อไปนี้จะเรียกใช้ฟังก์ชัน Apps Script ที่ชื่อ listFolderContent
โดยส่ง
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
}]
}]
}
}
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\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```"]]