การสร้างการ์ดแบบอินเทอร์แอกทีฟ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ส่วนเสริมส่วนใหญ่นอกจากจะแสดงข้อมูลแล้ว ยังกำหนดให้ผู้ใช้ป้อนข้อมูลด้วย เมื่อสร้างส่วนเสริมที่อิงตามการ์ด คุณสามารถใช้วิดเจ็ตแบบอินเทอร์แอกทีฟ เช่น ปุ่ม รายการเมนูในแถบเครื่องมือ หรือช่องทําเครื่องหมาย เพื่อขอข้อมูลจากผู้ใช้ที่ส่วนเสริมจําเป็นต้องใช้ หรือให้การควบคุมการโต้ตอบอื่นๆ
ส่วนใหญ่แล้ว คุณจะทำให้วิดเจ็ตเป็นแบบอินเทอร์แอกทีฟได้โดยลิงก์กับการดำเนินการที่เฉพาะเจาะจงและใช้ลักษณะการทำงานที่จำเป็นในฟังก์ชันการเรียกกลับ โปรดดูรายละเอียดที่หัวข้อการดำเนินการของส่วนเสริม
ในกรณีส่วนใหญ่ คุณสามารถทำตามขั้นตอนทั่วไปต่อไปนี้เพื่อกำหนดค่าวิดเจ็ตให้ดำเนินการบางอย่างเมื่อเลือกหรืออัปเดต
- สร้างออบเจ็กต์
Action
โดยระบุฟังก์ชัน Callback ที่ควรเรียกใช้ พร้อมกับพารามิเตอร์ที่จําเป็น
- ลิงก์วิดเจ็ตกับ
Action
โดยการเรียกใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ตที่เหมาะสม
- ใช้ฟังก์ชัน Callback เพื่อแสดงลักษณะการทำงานที่จำเป็น
ตัวอย่าง
ตัวอย่างต่อไปนี้จะตั้งค่าปุ่มที่แสดงการแจ้งเตือนผู้ใช้หลังจากมีการคลิก การคลิกจะทริกเกอร์ฟังก์ชัน Callback ของ notifyUser()
ด้วยอาร์กิวเมนต์ที่ระบุข้อความการแจ้งเตือน การส่งคืนรายการที่สร้างขึ้น ActionResponse
จะแสดงการแจ้งเตือน
/**
* Build a simple card with a button that sends a notification.
* @return {Card}
*/
function buildSimpleCard() {
var buttonAction = CardService.newAction()
.setFunctionName('notifyUser')
.setParameters({'notifyText': 'Button clicked!'});
var button = CardService.newTextButton()
.setText('Notify')
.setOnClickAction(buttonAction);
// ...continue creating widgets, then create a Card object
// to add them to. Return the built Card object.
}
/**
* Callback function for a button action. Constructs a
* notification action response and returns it.
* @param {Object} e the action event object
* @return {ActionResponse}
*/
function notifyUser(e) {
var parameters = e.parameters;
var notificationText = parameters['notifyText'];
return CardService.newActionResponseBuilder()
.setNotification(CardService.newNotification()
.setText(notificationText))
.build(); // Don't forget to build the response!
}
ออกแบบการโต้ตอบที่มีประสิทธิภาพ
โปรดคำนึงถึงสิ่งต่อไปนี้เมื่อออกแบบการ์ดแบบอินเทอร์แอกทีฟ
โดยปกติวิดเจ็ตแบบอินเทอร์แอกทีฟต้องมีเมธอดแฮนเดิลอย่างน้อย 1 เมธอดเพื่อกําหนดลักษณะการทํางาน
ใช้ฟังก์ชัน setOpenLink()
ของวิดเจ็ตเป็นตัวจัดการเมื่อคุณมี URL และต้องการเปิดในแท็บเท่านั้น
ซึ่งช่วยให้ไม่ต้องกําหนดออบเจ็กต์ Action
และฟังก์ชันการเรียกกลับ หากต้องสร้าง URL ก่อน หรือต้องทําขั้นตอนอื่นๆ เพิ่มเติมก่อนเปิด URL ให้กําหนด Action
และใช้ setOnClickOpenLinkAction()
แทน
เมื่อใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ต setOpenLink()
หรือ setOnClickOpenLinkAction()
คุณจะต้องระบุออบเจ็กต์ OpenLink
เพื่อกำหนด URL ที่เปิด นอกจากนี้ คุณยังใช้ออบเจ็กต์นี้เพื่อระบุลักษณะการทํางานของการเปิดและปิดโดยใช้ลิสต์OpenAs
และOnClose
ได้ด้วย
วิดเจ็ตมากกว่า 1 รายการอาจใช้ออบเจ็กต์ Action
เดียวกันได้
อย่างไรก็ตาม คุณต้องกําหนดออบเจ็กต์ Action
ที่แตกต่างกันหากต้องการระบุพารามิเตอร์อื่นให้กับฟังก์ชัน Callback
เขียนฟังก์ชัน Callback ให้เรียบง่าย บริการการ์ดจะจำกัดเวลาดำเนินการของฟังก์ชันการเรียกกลับไว้ที่ไม่เกิน 30 วินาทีเพื่อให้ส่วนเสริมตอบสนองอยู่เสมอ หากการดําเนินการใช้เวลานานกว่านั้น UI ของส่วนเสริมอาจไม่อัปเดตการแสดงการ์ดอย่างถูกต้องเพื่อตอบสนองAction
หากสถานะข้อมูลในแบ็กเอนด์ของบุคคลที่สามมีการเปลี่ยนแปลงเนื่องจากผู้ใช้โต้ตอบกับ UI ของส่วนเสริม เราขอแนะนำให้ส่วนเสริมตั้งค่าบิต "สถานะมีการเปลี่ยนแปลง" เป็น true
เพื่อให้ระบบล้างแคชฝั่งไคลเอ็นต์ที่มีอยู่ ดูรายละเอียดเพิ่มเติมได้ในคำอธิบายวิธีการ ActionResponseBuilder.setStateChanged()
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-12-22 UTC
[null,null,["อัปเดตล่าสุด 2024-12-22 UTC"],[[["\u003cp\u003eCard-based add-ons use interactive widgets like buttons and menus to collect user input and enhance user interactions.\u003c/p\u003e\n"],["\u003cp\u003eWidgets are made interactive by linking them to actions, which trigger callback functions to execute specific behaviors when interacted with.\u003c/p\u003e\n"],["\u003cp\u003eWhen defining widget actions, you can specify a callback function and any necessary parameters using the \u003ccode\u003eAction\u003c/code\u003e object and appropriate handler functions.\u003c/p\u003e\n"],["\u003cp\u003eFor opening URLs, \u003ccode\u003esetOpenLink()\u003c/code\u003e or \u003ccode\u003esetOnClickOpenLinkAction()\u003c/code\u003e can be used with an \u003ccode\u003eOpenLink\u003c/code\u003e object to define the URL and behavior.\u003c/p\u003e\n"],["\u003cp\u003eKeep callback functions concise, as they have execution time limits, and consider using \u003ccode\u003esetStateChanged()\u003c/code\u003e to update the UI when backend data changes due to user interactions.\u003c/p\u003e\n"]]],["Card-based add-ons use interactive widgets like buttons to gather user input or provide controls. Widgets are made interactive by linking them to actions via a callback function. To configure a widget, create an `Action` object with the callback function and parameters, link it using a widget handler function, and implement the callback function. For opening URLs directly, `setOpenLink()` avoids defining an `Action`. Ensure callbacks are simple (under 30 seconds), and for backend data changes, use `setStateChanged()`.\n"],null,["# Building interactive cards\n\nMost add-ons, in addition to presenting data, require the user to enter\ninformation. When you build a card-based add-on, you can use\ninteractive [widgets](/workspace/add-ons/concepts/widgets) such as buttons,\ntoolbar menu items, or checkboxes to ask the user for data that your add-on\nneeds or provide other interaction controls.\n\nAdding actions to widgets\n-------------------------\n\nFor the most part, you make widgets interactive by linking them to\nspecific *actions* and implementing the required behavior in a callback\nfunction. See [add-on actions](/workspace/add-ons/concepts/actions) for details.\n\nIn most cases, you can follow this general procedure to configure a widget to\ntake a specific action when selected or updated:\n\n1. Create an [`Action`](/apps-script/reference/card-service/action) object, specifing the callback function that should execute, along with any required parameters.\n2. Link the widget to the `Action` by calling the appropriate [widget handler function](/workspace/add-ons/concepts/actions#widget_handler_functions).\n3. Implement the [callback function](/workspace/add-ons/concepts/actions#callback_functions) to enact the required behavior.\n\nExample\n-------\n\nThe following example sets a button that displays a user notification\nafter it is clicked. The click triggers the `notifyUser()` callback function\nwith an argument that specifies the notification text. Returning a built\n[`ActionResponse`](/apps-script/reference/card-service/action-response)\nresults in a displayed notification. \n\n /**\n * Build a simple card with a button that sends a notification.\n * @return {Card}\n */\n function buildSimpleCard() {\n var buttonAction = CardService.newAction()\n .setFunctionName('notifyUser')\n .setParameters({'notifyText': 'Button clicked!'});\n var button = CardService.newTextButton()\n .setText('Notify')\n .setOnClickAction(buttonAction);\n\n // ...continue creating widgets, then create a Card object\n // to add them to. Return the built Card object.\n }\n\n /**\n * Callback function for a button action. Constructs a\n * notification action response and returns it.\n * @param {Object} e the action event object\n * @return {ActionResponse}\n */\n function notifyUser(e) {\n var parameters = e.parameters;\n var notificationText = parameters['notifyText'];\n return CardService.newActionResponseBuilder()\n .setNotification(CardService.newNotification()\n .setText(notificationText))\n .build(); // Don't forget to build the response!\n }\n\nDesign effective interactions\n-----------------------------\n\nWhen designing interactive cards, keep the following in mind:\n\n- Interactive widgets usually need at least one handler method to define their\n behavior.\n\n- Use the [`setOpenLink()`](/workspace/add-ons/concepts/actions#setOpenLink) widget\n handler function when you have a URL and just want to open it in a tab.\n This avoids the need to define an\n [`Action`](/apps-script/reference/card-service/action) object and callback\n function. If you need to build the URL first, or take any other additional\n steps before opening the URL, define an\n [`Action`](/apps-script/reference/card-service/action) and use\n [`setOnClickOpenLinkAction()`](/workspace/add-ons/concepts/actions#setOnClickOpenLinkAction)\n instead.\n\n- When using the [`setOpenLink()`](/workspace/add-ons/concepts/actions#setOpenLink)\n or [`setOnClickOpenLinkAction()`](/workspace/add-ons/concepts/actions#setOnClickOpenLinkAction)\n widget handler functions, you need to provide an\n [`OpenLink`](/apps-script/reference/card-service/open-link)\n object to define which URL to open. You can also use this object\n to specify opening and closing behavior using the\n [`OpenAs`](/apps-script/reference/card-service/open-as) and\n [`OnClose`](/apps-script/reference/card-service/on-close) enums.\n\n- It is possible for more than one widget to use the same\n [`Action`](/apps-script/reference/card-service/action) object.\n However, you need to define different\n [`Action`](/apps-script/reference/card-service/action) objects if you want\n to provide the callback function different parameters.\n\n- Keep your callback functions simple. To keep the add-ons responsive, the\n [Card service](/apps-script/reference/card-service/card-service) limits callback functions to a maximum of 30 seconds of\n execution time. If the execution takes longer than that, your add-on UI may\n not update its card display properly in response to the\n [`Action`](/apps-script/reference/card-service/action) .\n\n- If a data status on a third-party backend changes as the result of a user\n interaction with your add-on UI, it is recommended that the add-on set\n a 'state changed' bit to `true` so that any existing client side cache is\n cleared. See the\n [`ActionResponseBuilder.setStateChanged()`](/apps-script/reference/card-service/action-response-builder#setStateChanged(Boolean))\n method description for additional details."]]