ออบเจ็กต์ Action
ช่วยให้คุณสร้างลักษณะการทำงานแบบอินเทอร์แอกทีฟในส่วนเสริมของ Google Workspace ได้ โดยจะกำหนด
สิ่งที่เกิดขึ้นเมื่อผู้ใช้โต้ตอบกับวิดเจ็ต (เช่น ปุ่ม) ใน
UI ของส่วนเสริม
การดำเนินการจะแนบไปกับวิดเจ็ตที่กำหนดโดยใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ต ซึ่งยังกำหนดเงื่อนไขที่ทริกเกอร์การดำเนินการด้วย เมื่อทริกเกอร์แล้ว การดำเนินการจะเรียกใช้ ฟังก์ชัน Callback ที่กำหนด ฟังก์ชันเรียกกลับจะส่งออบเจ็กต์เหตุการณ์ที่มี ข้อมูลเกี่ยวกับการโต้ตอบฝั่งไคลเอ็นต์ของผู้ใช้ คุณต้องใช้ ฟังก์ชันเรียกกลับและให้ฟังก์ชันดังกล่าวแสดงผลออบเจ็กต์การตอบกลับที่เฉพาะเจาะจง
เช่น สมมติว่าคุณต้องการปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อ
คลิก โดยคุณต้องสร้างวิดเจ็ตปุ่มใหม่และใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ตปุ่ม
setOnClickAction(action)
เพื่อตั้งค่าAction
การสร้างการ์ด
Action
ที่คุณกำหนดจะระบุฟังก์ชันเรียกกลับของ Apps Script
ซึ่งจะทำงานเมื่อมีการคลิกปุ่ม ในกรณีนี้ คุณ
จะใช้ฟังก์ชันเรียกกลับเพื่อสร้างการ์ดที่ต้องการและแสดงผลออบเจ็กต์
ActionResponse
ออบเจ็กต์การตอบกลับจะบอกให้ส่วนเสริมแสดงการ์ดที่ฟังก์ชัน
Callback สร้างขึ้น
หน้านี้อธิบายการดำเนินการของวิดเจ็ตที่เฉพาะเจาะจงของไดรฟ์ที่คุณใส่ไว้ใน ส่วนเสริมได้
กระตุ้นการโต้ตอบ
ส่วนเสริมของ Google Workspace ที่ขยายไดรฟ์สามารถรวม การดำเนินการวิดเจ็ตเพิ่มเติมเฉพาะไดรฟ์ได้ การดำเนินการนี้ต้องใช้ ฟังก์ชันเรียกกลับ ของการดำเนินการที่เชื่อมโยงเพื่อแสดงผลออบเจ็กต์การตอบกลับเฉพาะ
การดำเนินการที่พยายาม | ฟังก์ชัน Callback ควรแสดงผล |
---|---|
ขอสิทธิ์เข้าถึงไฟล์สำหรับไฟล์ที่เลือก | DriveItemsSelectedActionResponse |
หากต้องการใช้การดำเนินการของวิดเจ็ตและออบเจ็กต์การตอบกลับเหล่านี้ เงื่อนไขต่อไปนี้ทั้งหมดต้องเป็นจริง
- ระบบจะทริกเกอร์การดำเนินการขณะที่ผู้ใช้เลือกรายการในไดรฟ์อย่างน้อย 1 รายการ
- ส่วนเสริมมี
https://www.googleapis.com/auth/drive.file
ขอบเขตไดรฟ์ใน ไฟล์ Manifest
ขอสิทธิ์เข้าถึงไฟล์สำหรับไฟล์ที่เลือก
ตัวอย่างต่อไปนี้แสดงวิธีสร้างอินเทอร์เฟซตามบริบทสำหรับ Google
ไดรฟ์ซึ่งจะทริกเกอร์เมื่อผู้ใช้เลือกรายการในไดรฟ์อย่างน้อย 1 รายการ
การทดสอบตัวอย่างจะทดสอบแต่ละรายการเพื่อดูว่าส่วนเสริมได้รับสิทธิ์เข้าถึงหรือไม่
หากไม่ได้รับ ก็จะใช้ออบเจ็กต์ DriveItemsSelectedActionResponse
เพื่อขอสิทธิ์ดังกล่าวจากผู้ใช้ เมื่อได้รับสิทธิ์สำหรับ
รายการหนึ่งๆ แล้ว ส่วนเสริมจะแสดงการใช้โควต้าไดรฟ์ของรายการนั้น
/**
* Build a simple card that checks selected items' quota usage. Checking
* quota usage requires user-permissions, so this add-on provides a button
* to request `drive.file` scope for items the add-on doesn't yet have
* permission to access.
*
* @param e The event object passed containing contextual information about
* the Drive items selected.
* @return {Card}
*/
function onDriveItemsSelected(e) {
var builder = CardService.newCardBuilder();
// For each item the user has selected in Drive, display either its
// quota information or a button that allows the user to provide
// permission to access that file to retrieve its quota details.
e['drive']['selectedItems'].forEach(
function(item){
var cardSection = CardService.newCardSection()
.setHeader(item['title']);
// This add-on uses the recommended, limited-permission `drive.file`
// scope to get granular per-file access permissions.
// See: https://developers.google.com/drive/api/v2/about-auth
if (item['addonHasFileScopePermission']) {
// If the add-on has access permission, read and display its
// quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"This file takes up: " + getQuotaBytesUsed(item['id'])));
} else {
// If the add-on does not have access permission, add a button
// that allows the user to provide that permission on a per-file
// basis.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"The add-on needs permission to access this file's quota."));
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClicked")
.setParameters({id: item.id});
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
builder.addSection(cardSection);
});
return builder.build();
}
/**
* Callback function for a button action. Instructs Drive to display a
* permissions dialog to the user, requesting `drive.file` scope for a
* specific item on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the item's
* Drive ID.
* @return {DriveItemsSelectedActionResponse}
*/
function onRequestFileScopeButtonClicked (e) {
var idToRequest = e.parameters.id;
return CardService.newDriveItemsSelectedActionResponseBuilder()
.requestFileScope(idToRequest).build();
}
/**
* Use the Advanced Drive Service
* (See https://developers.google.com/apps-script/advanced/drive),
* with `drive.file` scope permissions to request the quota usage of a
* specific Drive item.
*
* @param {string} itemId The ID of the item to check.
* @return {string} A description of the item's quota usage, in bytes.
*/
function getQuotaBytesUsed(itemId) {
try {
return Drive.Files.get(itemId,{fields: "quotaBytesUsed"})
.quotaBytesUsed + " bytes";
} catch (e) {
return "Error fetching how much quota this item uses. Error: " + e;
}
}