项目管理
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Apps Script API,您可以从应用中创建和修改 Apps Script 项目。本页面的示例展示了如何使用该 API 实现一些常见的项目管理操作。
注意:必须先启用 Apps Script API,然后才能使用它。
在这些示例中,占位符 scriptId 用于指示您提供脚本项目 ID 的位置。请按照以下步骤查找脚本 ID:
- 在 Apps 脚本项目中,点击左上角的项目设置图标 settings。
- 点击“脚本 ID”旁边的复制。
创建新的 Apps 脚本项目
以下 projects.create 请求会创建一个新的独立脚本。
POST https://scriptmanagement.googleapis.com/v1/projects/
{
"title": "My Script"
}
以下 projects.get 请求用于获取脚本项目的元数据。
GET https://scriptmanagement.googleapis.com/v1/projects/scriptId
响应包含一个对象,例如:
{
"scriptId": "scriptId",
"title": "My Title",
"parentId": "parentId",
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z",
"creator": { "name": "Grant" },
"lastModifyUser": { "name": "Grant" },
}
检索项目文件
以下 projects.getContent 请求会获取脚本项目的内容,包括每个脚本文件的代码来源和元数据。
GET https://scriptmanagement.googleapis.com/v1/projects/scriptId/content
响应包含一个 Content 对象,如下所示:
{
"scriptId": "scriptId",
"files": [{
"name": "My Script",
"type": "SERVER_JS",
"source": "function hello(){\nconsole.log('Hello world');}",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z",
"functionSet": {
"values": [
"name": "helloWorld"
]
}
}, {
"name": "appsscript",
"type": "JSON",
"source": "{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"CLOUD\"}",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z"
}]
}
更新项目文件
以下 projects.updateContent 请求会更新指定脚本项目的内容。此内容会存储为 HEAD 版本,并在脚本作为 API 可执行项目执行时使用。
PUT https://scriptmanagement.googleapis.com/v1/projects/scriptID/content
{
"files": [{
"name": "index",
"type": "HTML",
"source": "<html> <header><title>HTML Page</title></header> <body> My HTML </body> </html>"
}, {
"name": "My Script",
"type": "SERVER_JS",
"source": "function hello(){\nconsole.log('Hello world');}",
}, {
"name": "appsscript",
"type": "JSON",
"source": "{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"CLOUD\"}",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z"
}]
}
响应包含一个 Content 对象,如下所示:
{
"scriptId": "scriptId",
"files": [{
"name": "index",
"type": "HTML",
"source": "<html> <header><title>HTML Page</title></header> <body> My HTML </body> </html>",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z"
}, {
"name": "My Script",
"type": "SERVER_JS",
"source": "function hello(){\nconsole.log('Hello world');}",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z",
"functionSet": {
"values": [
"name": "helloWorld"
]
}
}, {
"name": "appsscript",
"type": "JSON",
"source": "{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"CLOUD\"}",
"lastModifyUser": {
"name": "Grant",
"email": "grant@example.com",
},
"createTime": "2017-10-02T15:01:23.045123456Z",
"updateTime": "2017-10-02T15:01:23.045123456Z"
}]
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Apps Script API enables programmatic creation and management of Apps Script projects, including tasks like creating new projects, retrieving project metadata, and updating project files.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the Apps Script API, ensure it's enabled in your Google Account settings.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve a script project's unique ID from the Project Settings in the Apps Script editor.\u003c/p\u003e\n"],["\u003cp\u003eThe API provides methods for accessing and modifying the content of script projects, such as source code and metadata for individual files within the project.\u003c/p\u003e\n"],["\u003cp\u003eUpdating project files through the API sets the updated content as the HEAD version, which is used for API execution.\u003c/p\u003e\n"]]],[],null,["# Project Management\n\nThe Apps Script API allows you to create and modify Apps Script projects\nfrom your app. The examples on this page illustrate how some common project\nmanagement operations can be achieved with the API.\n\nNOTE: The Apps Script API must be [enabled](https://script.google.com/home/usersettings)\nbefore usage.\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\nCreate a new Apps Script project\n--------------------------------\n\nThe following [projects.create](https://developers.google.com/apps-script/api/reference/rest/v1/projects/create)\nrequest creates a new [standalone script](https://developers.google.com/apps-script/guides/standalone). \n\n```\nPOST https://scriptmanagement.googleapis.com/v1/projects/\n``` \n\n```text\n{\n \"title\": \"My Script\"\n}\n```\n\nRetrieve project metadata\n-------------------------\n\nThe following [projects.get](https://developers.google.com/apps-script/api/reference/rest/v1/projects/get)\nrequest gets the script [project's metadata](https://developers.google.com/apps-script/api/reference/rest/v1/projects#Project). \n\n```\nGET https://scriptmanagement.googleapis.com/v1/projects/scriptId\n```\n\nThe response consists of an [object](https://developers.google.com/apps-script/api/reference/rest/v1/projects#Project)\nsuch as this one: \n\n```text\n{\n \"scriptId\": \"scriptId\",\n \"title\": \"My Title\",\n \"parentId\": \"parentId\",\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"creator\": { \"name\": \"Grant\" },\n \"lastModifyUser\": { \"name\": \"Grant\" },\n}\n```\n\nRetrieve project files\n----------------------\n\nThe following [projects.getContent](https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent)\nrequest gets the content of the script project, including the code source and\nmetadata for each script file. \n\n```\nGET https://scriptmanagement.googleapis.com/v1/projects/scriptId/content\n```\n\nThe response consists of a [Content](https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent#response-body)\nobject such as this one: \n\n```carbon\n{\n \"scriptId\": \"scriptId\",\n \"files\": [{\n \"name\": \"My Script\",\n \"type\": \"SERVER_JS\",\n \"source\": \"function hello(){\\nconsole.log('Hello world');}\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"functionSet\": {\n \"values\": [\n \"name\": \"helloWorld\"\n ]\n }\n }, {\n \"name\": \"appsscript\",\n \"type\": \"JSON\",\n \"source\": \"{\\\"timeZone\\\":\\\"America/New_York\\\",\\\"exceptionLogging\\\":\\\"CLOUD\\\"}\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\"\n }]\n}\n```\n\nUpdate project files\n--------------------\n\nThe following [projects.updateContent](https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent)\nrequest updates the content of the specified script project. This content is\nstored as the HEAD version, and is used when the script is executed as an API\nexecutable project. \n\n```\nPUT https://scriptmanagement.googleapis.com/v1/projects/scriptID/content\n``` \n\n```carbon\n{\n \"files\": [{\n \"name\": \"index\",\n \"type\": \"HTML\",\n \"source\": \"\u003chtml\u003e \u003cheader\u003e\u003ctitle\u003eHTML Page\u003c/title\u003e\u003c/header\u003e \u003cbody\u003e My HTML \u003c/body\u003e \u003c/html\u003e\"\n }, {\n \"name\": \"My Script\",\n \"type\": \"SERVER_JS\",\n \"source\": \"function hello(){\\nconsole.log('Hello world');}\",\n }, {\n \"name\": \"appsscript\",\n \"type\": \"JSON\",\n \"source\": \"{\\\"timeZone\\\":\\\"America/New_York\\\",\\\"exceptionLogging\\\":\\\"CLOUD\\\"}\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\"\n }]\n}\n```\n\nThe response consists of a [Content](https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent#response-body)\nobject such as this one: \n\n```carbon\n{\n \"scriptId\": \"scriptId\",\n \"files\": [{\n \"name\": \"index\",\n \"type\": \"HTML\",\n \"source\": \"\u003chtml\u003e \u003cheader\u003e\u003ctitle\u003eHTML Page\u003c/title\u003e\u003c/header\u003e \u003cbody\u003e My HTML \u003c/body\u003e \u003c/html\u003e\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\"\n }, {\n \"name\": \"My Script\",\n \"type\": \"SERVER_JS\",\n \"source\": \"function hello(){\\nconsole.log('Hello world');}\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"functionSet\": {\n \"values\": [\n \"name\": \"helloWorld\"\n ]\n }\n }, {\n \"name\": \"appsscript\",\n \"type\": \"JSON\",\n \"source\": \"{\\\"timeZone\\\":\\\"America/New_York\\\",\\\"exceptionLogging\\\":\\\"CLOUD\\\"}\",\n \"lastModifyUser\": {\n \"name\": \"Grant\",\n \"email\": \"grant@example.com\",\n },\n \"createTime\": \"2017-10-02T15:01:23.045123456Z\",\n \"updateTime\": \"2017-10-02T15:01:23.045123456Z\"\n }]\n}\n```"]]