นอกจากจะมีอินเทอร์เฟซแบบการ์ดเมื่อผู้ใช้อ่านข้อความ Gmail แล้ว ส่วนเสริมของ Google Workspace ที่ขยายการใช้งานให้ Gmail ยังมีอินเทอร์เฟซอีกแบบเมื่อผู้ใช้เขียนข้อความใหม่หรือตอบกลับข้อความที่มีอยู่ ซึ่งจะช่วยให้ส่วนเสริมของ Google Workspace ช่วยเขียนอีเมลให้กับผู้ใช้ได้โดยอัตโนมัติ
การเข้าถึง UI การเขียนของส่วนเสริม
คุณสามารถดู UI การเขียนของส่วนเสริมได้ 2 วิธี วิธีแรกคือการเริ่มเขียนฉบับร่างหรือตอบกลับใหม่ในขณะที่ส่วนเสริมเปิดอยู่ วิธีที่สองคือการเริ่มต้นส่วนเสริมขณะเขียนฉบับร่าง
ทั้ง 2 กรณีจะทําให้ส่วนเสริมทํางานฟังก์ชันทริกเกอร์การเขียนที่เกี่ยวข้อง ตามที่กำหนดไว้ในไฟล์ Manifest ของส่วนเสริม ฟังก์ชันทริกเกอร์การเขียนจะสร้าง UI การเขียนสำหรับการดำเนินการเขียนดังกล่าว ซึ่ง Gmail จะแสดงให้ผู้ใช้เห็น
การสร้างส่วนเสริมการเขียน
คุณเพิ่มฟังก์ชันการเขียนไปยังส่วนเสริมได้โดยทําตามขั้นตอนทั่วไปต่อไปนี้
- เพิ่มช่อง
gmail.composeTrigger
ไปยังไฟล์ Manifest ของโปรเจ็กต์ส่วนเสริมและอัปเดตขอบเขตไฟล์ Manifest เพื่อระบุขอบเขตที่จำเป็นสำหรับการเขียน - ใช้ฟังก์ชันทริกเกอร์การเขียนที่สร้าง UI การเขียนเมื่อทริกเกอร์เริ่มทำงาน ฟังก์ชันทริกเกอร์การเขียนจะแสดงผลออบเจ็กต์
Card
รายการเดียวหรืออาร์เรย์ของออบเจ็กต์Card
ที่ประกอบขึ้นเป็น UI การเขียนสำหรับการดำเนินการเขียน - ใช้ฟังก์ชันเรียกกลับที่เกี่ยวข้องซึ่งจำเป็นต่อการตอบสนองกับการโต้ตอบ UI ที่เขียนขึ้นของผู้ใช้ ฟังก์ชันเหล่านี้ไม่ใช่การทำงานเขียน (ซึ่งจะทำให้ UI การเขียนแสดงขึ้นเท่านั้น) แต่เป็นฟังก์ชันเดี่ยวที่ควบคุมสิ่งที่จะเกิดขึ้นเมื่อเลือกองค์ประกอบต่างๆ ของ UI การเขียน เช่น การ์ด UI ที่มีปุ่มมักมีฟังก์ชันเรียกกลับที่เกี่ยวข้องซึ่งจะทำงานเมื่อผู้ใช้คลิกปุ่มนั้น ฟังก์ชันเรียกกลับสำหรับวิดเจ็ตที่อัปเดตเนื้อหาของข้อความร่างควรแสดงออบเจ็กต์
UpdateDraftActionResponse
เขียนฟังก์ชันทริกเกอร์
UI การเขียนของส่วนเสริมสร้างขึ้นในลักษณะเดียวกับ UI ข้อความของส่วนเสริม โดยใช้บริการการ์ด Apps Script เพื่อสร้างการ์ดและเติมวิดเจ็ตเหล่านั้น
คุณต้องติดตั้ง gmail.composeTrigger.selectActions[].runFunction
ที่กำหนดในไฟล์ Manifest ฟังก์ชันทริกเกอร์การเขียนต้องแสดงผลออบเจ็กต์ Card
รายการเดียวหรืออาร์เรย์ของออบเจ็กต์ Card
ที่ประกอบ UI การเขียนสำหรับการดำเนินการดังกล่าว ฟังก์ชันเหล่านี้คล้ายกับฟังก์ชันทริกเกอร์ตามบริบทมาก และควรสร้างการ์ดในลักษณะเดียวกัน
สร้างออบเจ็กต์เหตุการณ์ทริกเกอร์
เมื่อเลือกการทำงานการเขียน ระบบจะเรียกใช้ฟังก์ชันทริกเกอร์การเขียนที่เกี่ยวข้องและส่งฟังก์ชันออบเจ็กต์เหตุการณ์เป็นพารามิเตอร์ ออบเจ็กต์เหตุการณ์สามารถนำข้อมูลเกี่ยวกับบริบทของส่วนเสริม และฉบับร่างที่เขียนไว้ในฟังก์ชันทริกเกอร์ได้
ดูโครงสร้างออบเจ็กต์เหตุการณ์สำหรับรายละเอียดเกี่ยวกับวิธีจัดเรียงข้อมูลในออบเจ็กต์เหตุการณ์ ค่าของช่องไฟล์ Manifest gmail.composeTrigger.draftAccess
จะควบคุมข้อมูลที่อยู่ในออบเจ็กต์เหตุการณ์บางส่วน ดังนี้
หากช่องไฟล์ Manifest
gmail.composeTrigger.draftAccess
คือNONE
หรือไม่ได้ระบุอยู่ ออบเจ็กต์เหตุการณ์จะมีเฉพาะข้อมูลเพียงเล็กน้อยหากตั้งค่า
gmail.composeTrigger.draftAccess
เป็นMETADATA
ออบเจ็กต์เหตุการณ์ที่ส่งไปยังฟังก์ชันทริกเกอร์การเขียนจะเติมข้อมูลรายชื่อผู้รับอีเมลที่กำลังเขียนอยู่
การแทรกเนื้อหาลงในฉบับร่างที่ใช้งานอยู่
โดยปกติแล้ว UI การเขียนของส่วนเสริมของ Google Workspace จะมีตัวเลือกและการควบคุมที่จะช่วยเขียนข้อความ สำหรับกรณีการใช้งานเหล่านี้ เมื่อผู้ใช้เลือกใน UI แล้ว ส่วนเสริมจะตีความการเลือกและอัปเดตอีเมลฉบับร่างที่ใช้งานอยู่ในปัจจุบันให้สอดคล้องกัน
เราได้ขยายบริการการ์ดสำหรับคลาสต่อไปนี้เพื่อให้อัปเดตอีเมลฉบับร่างปัจจุบันได้ง่ายขึ้น
ContentType
- Enum ที่กำหนดว่าจะเพิ่ม HTML ที่เปลี่ยนแปลงได้, HTML ที่เปลี่ยนแปลงไม่ได้ (ซึ่งผู้ใช้ Gmail แก้ไขไม่ได้) หรือเนื้อหาข้อความธรรมดาUpdateDraftActionResponse
- แสดงการตอบกลับการดำเนินการที่อัปเดตอีเมลฉบับร่างปัจจุบันUpdateDraftActionResponseBuilder
- เครื่องมือสร้างสำหรับออบเจ็กต์UpdateDraftActionResponse
UpdateDraftBodyAction
- หมายถึงการดำเนินการที่อัปเดตเนื้อหาในอีเมลฉบับร่างปัจจุบันUpdateDraftBodyType
- Enum ที่กำหนดการเปลี่ยนแปลงเนื้อหาUpdateDraftSubjectAction
- หมายถึงการดำเนินการที่อัปเดตช่องเรื่องของอีเมลฉบับร่างปัจจุบันUpdateDraftToRecipientsAction
- หมายถึงการดำเนินการที่อัปเดตผู้รับของอีเมลฉบับร่างปัจจุบันUpdateDraftCcRecipientsAction
- หมายถึงการดำเนินการที่อัปเดตผู้รับสำเนาของอีเมลฉบับร่างปัจจุบันUpdateDraftBccRecipientsAction
- หมายถึงการดำเนินการที่อัปเดตผู้รับสำเนาลับของอีเมลฉบับร่างปัจจุบัน
โดยปกติแล้ว UI การเขียนของส่วนเสริมจะมีวิดเจ็ต "บันทึก" หรือ "แทรก" ที่ผู้ใช้คลิกเพื่อระบุว่าทำการเลือกใน UI เสร็จแล้ว และต้องการให้เพิ่มตัวเลือกลงในอีเมลที่กำลังเขียน หากต้องการเพิ่มการโต้ตอบนี้ วิดเจ็ตควรมีออบเจ็กต์ Action
ที่เชื่อมโยง ซึ่งจะสั่งให้ส่วนเสริมเรียกใช้ฟังก์ชันเรียกกลับที่เฉพาะเจาะจงเมื่อมีการคลิกวิดเจ็ต คุณต้องใช้ฟังก์ชันเรียกกลับเหล่านี้ ฟังก์ชันเรียกกลับแต่ละรายการควรแสดงผลออบเจ็กต์
UpdateDraftActionResponse
ที่สร้างขึ้นมาซึ่งมีรายละเอียดการเปลี่ยนแปลงที่จะดำเนินการกับอีเมลฉบับร่างปัจจุบัน
ตัวอย่างที่ 1
ตัวอย่างข้อมูลโค้ดต่อไปนี้แสดงวิธีสร้าง UI การเขียนที่อัปเดตหัวเรื่อง รวมถึงผู้รับ "ถึง" "สำเนา" และ "สำเนาลับ" ของอีเมลฉบับร่างปัจจุบัน
/**
* Compose trigger function that fires when the compose UI is
* requested. Builds and returns a compose UI for inserting images.
*
* @param {event} e The compose trigger event object. Not used in
* this example.
* @return {Card[]}
*/
function getComposeUI(e) {
return [buildComposeCard()];
}
/**
* Build a card to display interactive buttons to allow the user to
* update the subject, and To, Cc, Bcc recipients.
*
* @return {Card}
*/
function buildComposeCard() {
var card = CardService.newCardBuilder();
var cardSection = CardService.newCardSection().setHeader('Update email');
cardSection.addWidget(
CardService.newTextButton()
.setText('Update subject')
.setOnClickAction(CardService.newAction()
.setFunctionName('applyUpdateSubjectAction')));
cardSection.addWidget(
CardService.newTextButton()
.setText('Update To recipients')
.setOnClickAction(CardService.newAction()
.setFunctionName('updateToRecipients')));
cardSection.addWidget(
CardService.newTextButton()
.setText('Update Cc recipients')
.setOnClickAction(CardService.newAction()
.setFunctionName('updateCcRecipients')));
cardSection.addWidget(
CardService.newTextButton()
.setText('Update Bcc recipients')
.setOnClickAction(CardService.newAction()
.setFunctionName('updateBccRecipients')));
return card.addSection(cardSection).build();
}
/**
* Updates the subject field of the current email when the user clicks
* on "Update subject" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateSubjectAction() {
// Get the new subject field of the email.
// This function is not shown in this example.
var subject = getSubject();
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction()
.addUpdateSubject(subject))
.build();
return response;
}
/**
* Updates the To recipients of the current email when the user clicks
* on "Update To recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateToRecipientsAction() {
// Get the new To recipients of the email.
// This function is not shown in this example.
var toRecipients = getToRecipients();
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction()
.addUpdateToRecipients(toRecipients))
.build();
return response;
}
/**
* Updates the Cc recipients of the current email when the user clicks
* on "Update Cc recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateCcRecipientsAction() {
// Get the new Cc recipients of the email.
// This function is not shown in this example.
var ccRecipients = getCcRecipients();
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction()
.addUpdateToRecipients(ccRecipients))
.build();
return response;
}
/**
* Updates the Bcc recipients of the current email when the user clicks
* on "Update Bcc recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateBccRecipientsAction() {
// Get the new Bcc recipients of the email.
// This function is not shown in this example.
var bccRecipients = getBccRecipients();
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction()
.addUpdateToRecipients(bccRecipients))
.build();
return response;
}
ตัวอย่างที่ 2
ข้อมูลโค้ดต่อไปนี้จะแสดงวิธีสร้าง UI การเขียนที่แทรกรูปภาพในอีเมลฉบับร่างปัจจุบัน
/**
* Compose trigger function that fires when the compose UI is
* requested. Builds and returns a compose UI for inserting images.
*
* @param {event} e The compose trigger event object. Not used in
* this example.
* @return {Card[]}
*/
function getInsertImageComposeUI(e) {
return [buildImageComposeCard()];
}
/**
* Build a card to display images from a third-party source.
*
* @return {Card}
*/
function buildImageComposeCard() {
// Get a short list of image URLs to display in the UI.
// This function is not shown in this example.
var imageUrls = getImageUrls();
var card = CardService.newCardBuilder();
var cardSection = CardService.newCardSection().setHeader('My Images');
for (var i = 0; i < imageUrls.length; i++) {
var imageUrl = imageUrls[i];
cardSection.addWidget(
CardService.newImage()
.setImageUrl(imageUrl)
.setOnClickAction(CardService.newAction()
.setFunctionName('applyInsertImageAction')
.setParameters({'url' : imageUrl})));
}
return card.addSection(cardSection).build();
}
/**
* Adds an image to the current draft email when the image is clicked
* in the compose UI. The image is inserted at the current cursor
* location. If any content of the email draft is currently selected,
* it is deleted and replaced with the image.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @param {event} e The incoming event object.
* @return {UpdateDraftActionResponse}
*/
function applyInsertImageAction(e) {
var imageUrl = e.parameters.url;
var imageHtmlContent = '<img style=\"display: block\" src=\"'
+ imageUrl + '\"/>';
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
.addUpdateContent(
imageHtmlContent,
CardService.ContentType.MUTABLE_HTML)
.setUpdateType(
CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
.build();
return response;
}