Quản lý dự án

API Apps Script cho phép bạn tạo và sửa đổi các dự án Apps Script từ ứng dụng của mình. Các ví dụ trên trang này minh hoạ cách thực hiện một số hoạt động quản lý dự án phổ biến bằng API.

LƯU Ý: Bạn phải bật API Apps Script trước khi sử dụng.

Trong những ví dụ này, phần giữ chỗ scriptId được dùng để cho biết vị trí bạn sẽ cung cấp mã dự án tập lệnh. Hãy làm theo các bước bên dưới để tìm mã tập lệnh:

  1. Trong dự án Apps Script, ở trên cùng bên trái, hãy nhấp vào biểu tượng Cài đặt dự án .
  2. Bên cạnh "Mã tập lệnh", hãy nhấp vào Sao chép.

Tạo một dự án Apps Script mới

Yêu cầu projects.create sau đây sẽ tạo một tập lệnh độc lập mới.

POST https://scriptmanagement.googleapis.com/v1/projects/
{
  "title": "My Script"
}

Truy xuất siêu dữ liệu dự án

Yêu cầu projects.get sau đây sẽ lấy siêu dữ liệu của dự án tập lệnh.

GET https://scriptmanagement.googleapis.com/v1/projects/scriptId

Phản hồi bao gồm một đối tượng, chẳng hạn như đối tượng này:

{
  "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" },
}

Truy xuất tệp dự án

Yêu cầu projects.getContent sau đây lấy nội dung của dự án tập lệnh, bao gồm cả nguồn mã và siêu dữ liệu cho từng tệp tập lệnh.

GET https://scriptmanagement.googleapis.com/v1/projects/scriptId/content

Phản hồi bao gồm một đối tượng Nội dung, chẳng hạn như đối tượng này:

{
  "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"
  }]
}

Cập nhật tệp dự án

Yêu cầu projects.updateContent sau đây sẽ cập nhật nội dung của dự án tập lệnh được chỉ định. Nội dung này được lưu trữ dưới dạng phiên bản HEAD và dùng khi tập lệnh được thực thi dưới dạng một dự án thực thi 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"
  }]
}

Phản hồi bao gồm một đối tượng Nội dung, chẳng hạn như đối tượng này:

{
  "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"
  }]
}