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
}]
}]
}
}
}
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-12 UTC。
[null,null,["最終更新日 2025-09-12 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,["The 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\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```"]]