ออบเจ็กต์ Action
ให้คุณสร้างพฤติกรรมแบบอินเทอร์แอกทีฟลงในส่วนเสริมของ Google Workspace ซึ่งจะกำหนดสิ่งที่จะเกิดขึ้นเมื่อผู้ใช้โต้ตอบกับวิดเจ็ต (เช่น ปุ่ม) ใน UI ของส่วนเสริม
การดำเนินการจะแนบไปกับวิดเจ็ตนั้นๆ โดยใช้ฟังก์ชันเครื่องจัดการวิดเจ็ต ซึ่งจะกำหนดเงื่อนไขที่ทริกเกอร์การดำเนินการด้วย เมื่อทริกเกอร์ การทำงานจะเรียกใช้ฟังก์ชันเรียกกลับที่กำหนดไว้ ระบบจะส่งฟังก์ชันเรียกกลับผ่านออบเจ็กต์เหตุการณ์ที่มีข้อมูลเกี่ยวกับการโต้ตอบฝั่งไคลเอ็นต์ของผู้ใช้ คุณต้องใช้ฟังก์ชันเรียกกลับและทำให้ฟังก์ชันตอบกลับเป็นออบเจ็กต์ที่เฉพาะเจาะจง
เช่น สมมติว่าคุณต้องการปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อคลิก ด้วยเหตุนี้ คุณต้องสร้างวิดเจ็ตปุ่มใหม่และใช้ฟังก์ชันแฮนเดิลวิดเจ็ตปุ่ม setOnClickAction(action)
เพื่อตั้งค่าการสร้างการ์ด Action
Action
ที่คุณกำหนดจะระบุฟังก์ชันเรียกกลับของ Apps Script ที่จะทำงานเมื่อมีการคลิกปุ่ม ในกรณีนี้ คุณจะใช้ฟังก์ชันเรียกกลับเพื่อสร้างการ์ดที่ต้องการและแสดงผลออบเจ็กต์ ActionResponse
ออบเจ็กต์การตอบกลับจะบอกให้ส่วนเสริมแสดงการ์ดที่ฟังก์ชันเรียกกลับสร้างขึ้น
หน้านี้จะอธิบายถึงการทำงานของวิดเจ็ตเฉพาะไดรฟ์ที่คุณสามารถรวมไว้ในส่วนเสริมได้
กระตุ้นการโต้ตอบ
ส่วนเสริมของ Google Workspace ที่ขยายไดรฟ์ได้จะมีการดำเนินการเพิ่มเติมเกี่ยวกับวิดเจ็ตเฉพาะไดรฟ์ การดำเนินการนี้ต้องใช้ฟังก์ชันเรียกกลับของการดำเนินการที่เกี่ยวข้องเพื่อแสดงผลออบเจ็กต์การตอบกลับแบบพิเศษ
พยายามดำเนินการแล้ว | ฟังก์ชันเรียกกลับควรจะแสดงผล |
---|---|
ส่งคำขอเข้าถึงไฟล์ที่เลือก | 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;
}
}