项目管理

借助 Apps Script API,您可以在应用中创建和修改 Apps 脚本项目。本页面上的示例说明了如何使用该 API 实现一些常见的项目管理操作。

注意:必须启用 Apps Script API,然后才能使用。

在这些示例中,占位符 scriptId 用于指明您应在何处提供脚本项目 ID。请按照以下步骤查找脚本 ID:

  1. 在 Apps 脚本项目中,点击左上角的项目设置图标
  2. 点击“脚本 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"
  }]
}