इंटरैक्टिव कार्ड बनाना
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
ज़्यादातर ऐड-ऑन, डेटा दिखाने के साथ-साथ उपयोगकर्ता से जानकारी डालने के लिए कहते हैं. कार्ड-आधारित ऐड-ऑन बनाते समय, बटन, टूलबार मेन्यू आइटम या चेकबॉक्स जैसे इंटरैक्टिव विजेट का इस्तेमाल किया जा सकता है. इससे, उपयोगकर्ता से वह डेटा मांगा जा सकता है जिसकी आपके ऐड-ऑन को ज़रूरत है या इंटरैक्शन के अन्य कंट्रोल दिए जा सकते हैं.
ज़्यादातर मामलों में, विजेट को इंटरैक्टिव बनाने के लिए, उन्हें किसी खास कार्रवाई से लिंक किया जाता है. साथ ही, कॉलबैक फ़ंक्शन में ज़रूरी व्यवहार लागू किया जाता है. ज़्यादा जानकारी के लिए, ऐड-ऑन ऐक्शन देखें.
ज़्यादातर मामलों में, किसी विजेट को कॉन्फ़िगर करने के लिए, इस सामान्य तरीके का पालन किया जा सकता है, ताकि चुने जाने या अपडेट होने पर कोई खास कार्रवाई की जा सके:
Action
ऑब्जेक्ट बनाएं. इसमें, ज़रूरी पैरामीटर के साथ-साथ, वह कॉलबैक फ़ंक्शन भी बताएं जिसे लागू करना है.
- सही विजेट हैंडलर फ़ंक्शन को कॉल करके, विजेट को
Action
से लिंक करें.
- ज़रूरी कार्रवाई करने के लिए, कॉलबैक फ़ंक्शन लागू करें.
उदाहरण
नीचे दिया गया उदाहरण, एक बटन सेट करता है. इस बटन पर क्लिक करने के बाद, उपयोगकर्ता को सूचना दिखती है. क्लिक करने पर, 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!
}
असरदार इंटरैक्शन डिज़ाइन करना
इंटरैक्टिव कार्ड डिज़ाइन करते समय, इन बातों का ध्यान रखें:
इंटरैक्टिव विजेट के व्यवहार को तय करने के लिए, आम तौर पर कम से कम एक हैंडलर तरीका ज़रूरी होता है.
अगर आपके पास कोई यूआरएल है और आपको उसे सिर्फ़ किसी टैब में खोलना है, तो setOpenLink()
विजेट हैंडलर फ़ंक्शन का इस्तेमाल करें.
इससे Action
ऑब्जेक्ट और कॉलबैक फ़ंक्शन तय करने की ज़रूरत नहीं होती. अगर आपको यूआरएल खोलने से पहले, यूआरएल बनाना है या कोई और अतिरिक्त कार्रवाई करनी है, तो Action
तय करें और इसके बजाय setOnClickOpenLinkAction()
का इस्तेमाल करें.
setOpenLink()
या setOnClickOpenLinkAction()
विजेट हैंडलर फ़ंक्शन का इस्तेमाल करते समय, आपको यह तय करने के लिए OpenLink
ऑब्जेक्ट देना होगा कि कौनसा यूआरएल खोलना है. इस ऑब्जेक्ट का इस्तेमाल, OpenAs
और OnClose
एन्सम का इस्तेमाल करके, खोलने और बंद करने के व्यवहार की जानकारी देने के लिए भी किया जा सकता है.
एक से ज़्यादा विजेट, एक ही
Action
ऑब्जेक्ट का इस्तेमाल कर सकते हैं.
हालांकि, अगर आपको कॉलबैक फ़ंक्शन को अलग-अलग पैरामीटर देने हैं, तो आपको अलग-अलग Action
ऑब्जेक्ट तय करने होंगे.
अपने कॉलबैक फ़ंक्शन को आसान रखें. ऐड-ऑन को रिस्पॉन्सिव रखने के लिए, कार्ड सेवा, कॉलबैक फ़ंक्शन को ज़्यादा से ज़्यादा 30 सेकंड तक चलाने की अनुमति देती है. अगर प्रोसेस में इससे ज़्यादा समय लगता है, तो हो सकता है कि आपका ऐड-ऑन यूज़र इंटरफ़ेस, Action
के जवाब में अपने कार्ड डिसप्ले को सही तरीके से अपडेट न कर पाए .
अगर आपके ऐड-ऑन यूज़र इंटरफ़ेस के साथ उपयोगकर्ता के इंटरैक्शन की वजह से, तीसरे पक्ष के बैकएंड पर डेटा की स्थिति बदलती है, तो हमारा सुझाव है कि ऐड-ऑन 'स्थिति बदली' बिट को true
पर सेट करे, ताकि क्लाइंट साइड का मौजूदा कैश मेमोरी मिटाया जा सके. ज़्यादा जानकारी के लिए, ActionResponseBuilder.setStateChanged()
के तरीके का ब्यौरा देखें.
जब तक कुछ अलग से न बताया जाए, तब तक इस पेज की सामग्री को Creative Commons Attribution 4.0 License के तहत और कोड के नमूनों को Apache 2.0 License के तहत लाइसेंस मिला है. ज़्यादा जानकारी के लिए, Google Developers साइट नीतियां देखें. Oracle और/या इससे जुड़ी हुई कंपनियों का, Java एक रजिस्टर किया हुआ ट्रेडमार्क है.
आखिरी बार 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."]]