專案管理

Apps Script API 可讓您從應用程式建立及修改 Apps Script 專案。本頁的範例說明如何透過 API 執行一些常見的專案管理作業。

注意:必須先啟用 Apps Script API 才能使用。

在這些範例中,預留位置 scriptId 是用來指出您要在何處提供指令碼專案 ID。請按照下列步驟找出指令碼 ID:

  1. 在 Apps Script 專案中,按一下左上方的「專案設定」圖示
  2. 按一下「指令碼 ID」旁邊的「複製」

建立新的 Apps Script 專案

下列 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"
  }]
}