การดำเนินการสากลคือองค์ประกอบของรายการเมนูที่ช่วยให้ผู้ใช้เปิดหน้าเว็บใหม่ แสดงการ์ด UI ใหม่ หรือเรียกใช้ฟังก์ชัน Apps Script ที่เฉพาะเจาะจงเมื่อเลือก การทำงานของ Universal Actions คล้ายกับการดำเนินการบนการ์ด ยกเว้นว่า Universal Actions จะอยู่ในการ์ดทุกใบในส่วนเสริมเสมอ ไม่ว่าบริบทของส่วนเสริมปัจจุบันจะเป็นอย่างไรก็ตาม
การใช้การดำเนินการสากลช่วยให้มั่นใจได้ว่าผู้ใช้จะมีสิทธิ์เข้าถึงฟังก์ชันการทำงานบางอย่างเสมอ ไม่ว่าผู้ใช้จะโต้ตอบกับส่วนใดของส่วนเสริมก็ตาม ตัวอย่างกรณีการใช้งานการดำเนินการสากลมีดังนี้
- เปิดหน้าเว็บการตั้งค่า (หรือแสดงการ์ดการตั้งค่า)
- แสดงข้อมูลความช่วยเหลือแก่ผู้ใช้
- เริ่มเวิร์กโฟลว์ใหม่ เช่น "เพิ่มลูกค้าใหม่"
- แสดงการ์ดที่ให้ผู้ใช้ส่งความคิดเห็นเกี่ยวกับส่วนเสริม
เมื่อใดก็ตามที่คุณมีการดำเนินการที่ไม่ขึ้นอยู่กับบริบทปัจจุบัน คุณควรพิจารณาทำให้เป็นการดำเนินการสากล
การใช้ Universal Action
ระบบจะกำหนดค่าการดำเนินการแบบสากลในโปรเจ็กต์ไฟล์ Manifest ของส่วนเสริม เมื่อกำหนดค่า การดำเนินการสากลแล้ว ผู้ใช้ส่วนเสริมจะใช้การดำเนินการดังกล่าวได้เสมอ หากผู้ใช้ กำลังดูการ์ด ชุดการดำเนินการทั่วไปที่คุณกำหนดจะปรากฏในเมนูการ์ดเสมอ หลังจากการดำเนินการของการ์ด ที่คุณกำหนดไว้สำหรับการ์ดนั้น การดำเนินการสากลจะปรากฏในเมนูการ์ดตามลำดับเดียวกันกับที่กำหนดไว้ในไฟล์ Manifest ของส่วนเสริม
การกำหนดค่าการดำเนินการแบบสากล
คุณกำหนดค่าการดำเนินการสากลในไฟล์ Manifest ของส่วนเสริมได้ ดูรายละเอียดเพิ่มเติมได้ที่ ไฟล์ Manifest
สำหรับการดำเนินการแต่ละอย่าง คุณจะระบุข้อความที่ควรปรากฏในเมนูสำหรับการดำเนินการนั้น
จากนั้นคุณจะระบุฟิลด์ openLink ที่ระบุว่าการดำเนินการ
ควรเปิดหน้าเว็บในแท็บใหม่โดยตรงได้ หรือจะระบุฟิลด์ runFunction ที่ระบุฟังก์ชันเรียกกลับของ Apps Script เพื่อ
เรียกใช้เมื่อมีการเลือกการดำเนินการสากลก็ได้
เมื่อใช้ runFunction ฟังก์ชันเรียกกลับที่ระบุจะดำเนินการอย่างใดอย่างหนึ่งต่อไปนี้
- สร้างการ์ด UI เพื่อแสดงทันทีโดยการส่งคืนออบเจ็กต์
UniversalActionResponseที่สร้างขึ้น - เปิด URL หลังจากทำภารกิจอื่นๆ โดยการส่งคืนออบเจ็กต์
UniversalActionResponseที่สร้างขึ้น - ดำเนินการงานเบื้องหลังโดยไม่เปลี่ยนไปใช้การ์ดใหม่หรือเปิด URL ในกรณีนี้ ฟังก์ชันการเรียกกลับจะไม่แสดงผลใดๆ
เมื่อมีการเรียกใช้ ฟังก์ชันเรียกกลับจะส่งออบเจ็กต์เหตุการณ์ ที่มีข้อมูลเกี่ยวกับบริบทของแอดออนและการ์ดที่เปิดอยู่
ตัวอย่าง
ข้อมูลโค้ดต่อไปนี้แสดงตัวอย่างข้อความที่ตัดตอนมาจากไฟล์ Manifest สำหรับ ส่วนเสริม Google Workspace ที่ใช้การดำเนินการสากล ขณะขยาย Gmail โค้ดจะตั้งค่าขอบเขตข้อมูลเมตาอย่างชัดเจนเพื่อให้ส่วนเสริมระบุได้ว่าใครเป็นผู้ส่งข้อความที่เปิด
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata"
],
"addOns": {
"common": {
"name": "Universal Actions Only Addon",
"logoUrl": "https://www.example.com/hosted/images/2x/my-icon.png",
"openLinkUrlPrefixes": [
"https://www.google.com",
"https://www.example.com/urlbase"
],
"universalActions": [{
"label": "Open google.com",
"openLink": "https://www.google.com"
}, {
"label": "Open contact URL",
"runFunction": "openContactURL"
}, {
"label": "Open settings",
"runFunction": "createSettingsResponse"
}, {
"label": "Run background sync",
"runFunction": "runBackgroundSync"
}],
...
},
"gmail": {
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "getContextualAddOn"
}
]
},
...
},
...
การดำเนินการสากล 3 รายการที่กำหนดไว้ในตัวอย่างก่อนหน้าจะทำสิ่งต่อไปนี้
- เปิด google.com จะเปิด https://www.google.com ใน แท็บใหม่
- เปิด URL ของรายชื่อติดต่อจะเรียกใช้ฟังก์ชันที่กำหนด URL ที่จะเปิด
แล้วเปิดในแท็บใหม่โดยใช้ออบเจ็กต์
OpenLinkโค้ดจะสร้าง URL โดยใช้อีเมลของผู้ส่ง - เปิดการตั้งค่าจะเรียกใช้ฟังก์ชัน
createSettingsCards()ที่กำหนดไว้ใน โปรเจ็กต์สคริปต์ของส่วนเสริม ฟังก์ชันนี้จะแสดงออบเจ็กต์UniversalActionResponseที่ถูกต้องซึ่งมีชุดการ์ดที่มีการตั้งค่าส่วนเสริมและข้อมูลอื่นๆ หลังจากฟังก์ชันสร้างออบเจ็กต์นี้เสร็จแล้ว UI จะแสดงรายการ ของการ์ด (ดูการแสดงการ์ดหลายใบ) - เรียกใช้การซิงค์เบื้องหลังจะเรียกใช้ฟังก์ชัน
runBackgroundSync()ที่กำหนดไว้ในโปรเจ็กต์สคริปต์ของส่วนเสริม ฟังก์ชันนี้ไม่ได้สร้างการ์ด แต่จะ ทำงานอื่นๆ ในเบื้องหลังซึ่งไม่เปลี่ยนแปลง UI เนื่องจาก ฟังก์ชันไม่ได้แสดงผลUniversalActionResponseUI จึงไม่แสดงการ์ดใหม่เมื่อฟังก์ชันเสร็จสิ้น แต่ UI จะแสดงวงกลมสัญญาณบอกสถานะการโหลดขณะที่ฟังก์ชันทำงาน
ตัวอย่างวิธีสร้างฟังก์ชัน openContactURL(),
createSettingsResponse() และ runBackgroundSync() มีดังนี้
/**
* Open a contact URL.
* @param {Object} e an event object
* @return {UniversalActionResponse}
*/
function openContactURL(e) {
// Activate temporary Gmail scopes, in this case so that the
// open message metadata can be read.
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
// Build URL to open based on a base URL and the sender's email.
// This URL must be included in the openLinkUrlPrefixes whitelist.
var messageId = e.gmail.messageId;
var message = GmailApp.getMessageById(messageId);
var sender = message.getFrom();
var url = "https://www.example.com/urlbase/" + sender;
return CardService.newUniversalActionResponseBuilder()
.setOpenLink(CardService.newOpenLink()
.setUrl(url))
.build();
}
/**
* Create a collection of cards to control the add-on settings and
* present other information. These cards are displayed in a list when
* the user selects the associated "Open settings" universal action.
*
* @param {Object} e an event object
* @return {UniversalActionResponse}
*/
function createSettingsResponse(e) {
return CardService.newUniversalActionResponseBuilder()
.displayAddOnCards(
[createSettingCard(), createAboutCard()])
.build();
}
/**
* Create and return a built settings card.
* @return {Card}
*/
function createSettingCard() {
return CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Settings'))
.addSection(CardService.newCardSection()
.addWidget(CardService.newSelectionInput()
.setType(CardService.SelectionInputType.CHECK_BOX)
.addItem("Ask before deleting contact", "contact", false)
.addItem("Ask before deleting cache", "cache", false)
.addItem("Preserve contact ID after deletion", "contactId", false))
// ... continue adding widgets or other sections here ...
).build(); // Don't forget to build the card!
}
/**
* Create and return a built 'About' informational card.
* @return {Card}
*/
function createAboutCard() {
return CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('About'))
.addSection(CardService.newCardSection()
.addWidget(CardService.newTextParagraph()
.setText('This add-on manages contact information. For more '
+ 'details see the <a href="https://www.example.com/help">'
+ 'help page</a>.'))
// ... add other information widgets or sections here ...
).build(); // Don't forget to build the card!
}
/**
* Run background tasks, none of which should alter the UI.
* Also records the time of sync in the script properties.
*
* @param {Object} e an event object
*/
function runBackgroundSync(e) {
var props = PropertiesService.getUserProperties();
props.setProperty("syncTime", new Date().toString());
syncWithContacts(); // Not shown.
updateCache(); // Not shown.
validate(); // Not shown.
// no return value tells the UI to keep showing the current card.
}