借助 Apps Script API,您可以创建和修改 Apps 脚本项目 。本页面上的示例说明了一些常见项目 管理操作可以通过该 API 实现。
注意:必须启用 Apps Script API 。
在这些示例中,占位符 scriptId 用于指明在何处提供脚本项目 ID。按照 按以下步骤查找脚本 ID:
- 在 Apps 脚本项目中,点击左上角的项目设置 。
- 点击“脚本 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" }] }