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
การตอบกลับประกอบด้วยออบเจ็กต์ 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" }] }