API ของ Apps Script ช่วยให้คุณสร้างและแก้ไขโปรเจ็กต์ Apps Script จากแอปได้ ตัวอย่างในหน้านี้จะแสดงให้เห็นว่าการทํางานของการจัดการโปรเจ็กต์ทั่วไปด้วย API มีอะไรบ้าง
หมายเหตุ: ต้องเปิดใช้ Apps Script API ก่อนการใช้งาน
ในตัวอย่างเหล่านี้ ตัวยึดตําแหน่ง scriptId ใช้เพื่อระบุตําแหน่งที่คุณจะใส่รหัสโปรเจ็กต์สคริปต์ ทําตามขั้นตอนด้านล่างเพื่อค้นหารหัสสคริปต์
- ในโปรเจ็กต์ Apps Script ให้คลิกการตั้งค่าโปรเจ็กต์ที่ด้านซ้ายบน
- &"รหัสสคริปต์," คลิกคัดลอก
สร้างโปรเจ็กต์ 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", "projectOrigin": "APPS_SCRIPT_IDE", "cloudProject": "cloudProject", "encryptedCloudProjectToken": "token", "securityZone": "STANDARD", "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
การตอบสนองจะประกอบด้วยออบเจ็กต์เนื้อหา เช่นออบเจ็กต์นี้
{ "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" }] }
การตอบสนองจะประกอบด้วยออบเจ็กต์เนื้อหา เช่นออบเจ็กต์นี้
{ "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" }] }