Apps Script API ช่วยให้คุณสร้างและแก้ไขโปรเจ็กต์ 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", "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" }] }