اجرای توابع اسکریپت برنامه ها
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Apps Script API (و قبلاً Apps Script Execution API) به شما اجازه می دهد تا از راه دور یک تابع را در پروژه اسکریپتی که به آن دسترسی دارید اجرا کنید. برنامه شما می تواند یک تابع Apps Script داده شده را فراخوانی کند و در صورت نیاز پارامترهای ورودی را به آن ارائه دهد و یک پاسخ برگشتی دریافت کند.
مثالهای موجود در این صفحه نشان میدهند که چگونه میتوان برخی از عملیات اجرایی رایج را با API به دست آورد. برای اطلاعات بیشتر از جمله الزامات مجوز ویژه ، به راهنمای اجرای یک عملکرد مراجعه کنید.
در این مثالها، scriptId مکانها برای نشان دادن محل ارائه شناسه پروژه اسکریپت استفاده میشود. برای پیدا کردن شناسه اسکریپت مراحل زیر را دنبال کنید:
- در پروژه Apps Script، در بالا سمت راست، settings تنظیمات پروژه کلیک کنید.
- در کنار «شناسه اسکریپت»، روی « کپی » کلیک کنید.
یک تابع را اجرا کنید
درخواست scripts.run زیر یک تابع Apps Script به نام listFolderContent
را فراخوانی می کند و آن را به folderId Drive و یک عدد صحیح 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
}]
}]
}
}
}
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\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```"]]