您可以使用 Apps Script API 透過應用程式建立及修改 Apps Script 專案。本頁的範例說明如何透過 API 執行一些常見的專案管理作業。
注意:您必須啟用 Apps Script API 。
在這些範例中,預留位置 scriptId 用於指出您提供指令碼專案 ID 的位置。跟著 如要找出指令碼 ID,請按照下列步驟操作:
- 在 Apps Script 專案中,按一下左上方的「專案設定」。 。
- 按一下「腳本 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" }] }