इस क्विकस्टार्ट में, Google Apps Script का इस्तेमाल करके, Workspace Flows के लिए कस्टम स्टेप बनाने का तरीका बताया गया है. कस्टम चरण में, दो संख्याओं और अंकगणित की किसी कार्रवाई को इनपुट के तौर पर लिया जाता है. इसके बाद, कैलकुलेशन की जाती है और नतीजे को आउटपुट के तौर पर दिखाया जाता है.
मकसद
- Google Apps Script की मदद से, Workspace Flows के लिए कस्टम चरण बनाएं.
- कस्टम चरण को अपने Google Workspace संगठन में डिप्लॉय करें.
- Workspace Flows में, कस्टम फ़्लो स्टेप की जांच करें.
ज़रूरी शर्तें
- आपके पास ऐसा Google खाता होना चाहिए जिससे Gemini Alpha प्रोग्राम के ज़रिए Workspace Flows को ऐक्सेस किया जा सकता हो.
स्क्रिप्ट सेट अप करना
स्क्रिप्ट सेट अप करने के लिए, एक नया Apps Script प्रोजेक्ट बनाएं. इसके बाद, उसे अपने Cloud प्रोजेक्ट से कनेक्ट करें.
फ़्लो कैलकुलेटर क्विकस्टार्ट Apps Script प्रोजेक्ट खोलने के लिए, इस बटन पर क्लिक करें.
खास जानकारी पर क्लिक करें.
खास जानकारी वाले पेज पर,
कॉपी बनाएं पर क्लिक करें.
Apps Script प्रोजेक्ट की कॉपी का नाम डालें:
Copy of Flows calculator quickstart पर क्लिक करें.
प्रोजेक्ट का टाइटल में,
Flows calculator quickstartटाइप करें.नाम बदलें पर क्लिक करें.
ज़रूरी नहीं: क्विकस्टार्ट कोड की समीक्षा करना
पिछले सेक्शन में, आपने पूरे Apps Script प्रोजेक्ट को कॉपी किया था. इसमें आपके फ़्लो के कस्टम चरण के लिए ज़रूरी सभी ऐप्लिकेशन कोड शामिल हैं. इसलिए, हर फ़ाइल को कॉपी करके चिपकाने की ज़रूरत नहीं है.
इसके अलावा, पिछले सेक्शन में कॉपी की गई हर फ़ाइल की समीक्षा यहां की जा सकती है:
appsscript.jsonमेनिफ़ेस्ट फ़ाइल. यह एक खास JSON फ़ाइल होती है. इसमें प्रोजेक्ट की बुनियादी जानकारी होती है. इस जानकारी का इस्तेमाल करके Apps Script, स्क्रिप्ट को चलाता है.
appsscript.jsonकोड देखें{ "timeZone": "America/Los_Angeles", "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8", "addOns": { "common": { "name": "Calculator", "logoUrl": "https://www.gstatic.com/images/branding/productlogos/calculator_search/v1/web-24dp/logo_calculator_search_color_1x_web_24dp.png", "useLocaleFromApp": true }, "flows": { "workflowElements": [ { "id": "actionElement", "state": "ACTIVE", "name": "Calculate", "description": "Asks the user for two values and a math operation, then performs the math operation on the values and outputs the result.", "workflowAction": { "inputs": [ { "id": "value1", "description": "value1", "cardinality": "SINGLE", "dataType": { "basicType": "INTEGER" } }, { "id": "value2", "description": "value2", "cardinality": "SINGLE", "dataType": { "basicType": "INTEGER" } }, { "id": "operation", "description": "operation", "cardinality": "SINGLE", "dataType": { "basicType": "STRING" } } ], "outputs": [ { "id": "result", "description": "Calculated result", "cardinality": "SINGLE", "dataType": { "basicType": "INTEGER" } }, { "id": "log", "description": "Logged result of flow event.", "cardinality": "SINGLE", "dataType": { "basicType": "STRING" } } ], "onConfigFunction": "onConfigCalculateFunction", "onExecuteFunction": "onExecuteCalculateFunction" } } ] } } }
Calculator.gsGoogle Workspace Flows के लिए कस्टम चरण तय करता है. "कैलकुलेट करें" नाम वाले इस चरण में, दो संख्याओं और एक ऑपरेशन को इनपुट के तौर पर लिया जाता है. इसके बाद, कैलकुलेशन का नतीजा दिखाया जाता है.
Calculator.gsकोड देखें/** * This script defines a custom step for Google Workspace Flows. * The step, named "Calculate", takes two numbers and an operation as input * and returns the result of the calculation. * * The script includes functions to: * * 1. Define the configuration UI for the step using Card objects: * * - `onConfigCalculateFunction()`: Generates the main configuration card. * - Helper functions like `pushCard()`, `saveButton()` to build card components. * * 2. Handle the execution of the step. * * - `onExecuteCalculateFunction()`: Retrieves inputs, performs the calculation, * and returns outputs. * * To learn more, see the following quickstart guide: * https://developers.google.com/workspace/add-ons/workflows/quickstart */ /** * Creates an action response to push a new card onto the card stack. * * This function generates an action object that, when returned, causes the * provided card to be pushed onto the card stack, making it the currently * displayed card in the configuration UI. * @param {Object} card The Card object to push. * @return {Object} The action response object. */ function pushCard(card) { return { "action": { "navigations": [{ "push_card": card } ] } }; } /** * Creates an action response to update the currently displayed card. * * This function generates an action object that, when returned, causes the * currently displayed card to be replaced with the provided card in the * configuration UI. * @param {Object} card The Card object to update. * @return {Object} The render actions object. */ function updateCard(card) { return { "render_actions": { "action": { "navigations": [{ "update_card": card } ] } } }; } /** * Creates a button configuration object for saving the workflow. * * This function generates a button definition that, when clicked, triggers * a save action for the current workflow configuration. * @return {Object} The button widget object. */ function saveButton() { return { "text": "Save", "onClick": { "hostAppAction" : { "workflowAction" : { "saveWorkflowAction" : {} } } }, }; } /** * Creates a button configuration object for a refresh action. * * This function generates a button definition that, when clicked, triggers * a function to refresh the current card. * @param {string} functionName The name of the Apps Script function to call on click. * @return {Object} The button widget object. */ function refreshButton(functionName) { return { "text": "Refresh", "onClick": { "action" : { "function" : functionName } }, }; } /** * Generates and displays a configuration card for the sample calculation action. * * This function creates a card with input fields for two values and a dropdown * for selecting an arithmetic operation. The card also includes a "Save" * button to save the action configuration for the workflow. * * The input fields are configured to let the user select outputs from previous * workflow steps as input values using the `hostAppDataSource` property. * This function is called when the user adds or edits the "Calculate" step in the Flows UI. * @return {Object} The action response object containing the card to display. */ function onConfigCalculateFunction() { var card = { "sections": [ { "header": "Action sample: Calculate", "widgets": [ { "textInput": { "name": "value1", "label": "First value", "hostAppDataSource" : { "workflowDataSource" : { "includeVariables" : true } } } }, { "selectionInput": { "name": "operation", "label": "Operation", "type": "DROPDOWN", "items": [ { "text": "+", "value": "+", }, { "text": "-", "value": "-", }, { "text": "x", "value": "x", }, { "text": "/", "value": "/", } ] } }, { "textInput": { "name": "value2", "label": "Second value", "hostAppDataSource" : { "workflowDataSource" : { "includeVariables" : true } } } } ] } ] }; return pushCard(card); } /** * Gets an integer value from variable data, handling both string and integer formats. * * This function attempts to extract an integer value from the provided variable data. * It checks if the data contains string values and, if so, parses the first string * as an integer. If integer values are present, it returns the first integer. * @param {Object} variableData The variable data object from the event. * @return {number} The extracted integer value. */ function getIntValue(variableData) { if (variableData.stringValues) { return parseInt(variableData.stringValues[0]); } return variableData.integerValues[0]; } /** * Executes the calculation action based on the inputs from a workflow event. * * This function retrieves input values ("value1", "value2") and the "operation" * from the workflow event, performs the calculation, and returns the "result" and * "log" as output variables. * This function is called when the workflow execution reaches this custom step. * @param {Object} event The event object passed by the Flows runtime. * @return {Object} The output variables object. */ function onExecuteCalculateFunction(event) { console.log("output: " + JSON.stringify(event)); var calculatedValue = 0; var value1 = getIntValue(event.workflow.actionInvocation.inputs["value1"]); var value2 = getIntValue(event.workflow.actionInvocation.inputs["value2"]); var operation = event.workflow.actionInvocation.inputs["operation"].stringValues[0]; if (operation == "+") { calculatedValue = value1 + value2; } else if (operation == "-") { calculatedValue = value1 - value2; } else if (operation == "x") { calculatedValue = value1 * value2; } else if (operation == "/") { calculatedValue = value1 / value2; } var renderAction = { "hostAppAction" : { "workflowAction" : { "returnOutputVariablesAction" : { "variableValues" : [ { "variableId": "result", "integerValues": [ calculatedValue ] } ] } } } }; }
अपने चरण को लागू करना और उसकी जांच करना
अपने चरण की जांच करने के लिए, अपने ऐड-ऑन के लिए टेस्ट डिप्लॉयमेंट सेट अप करें. इसके बाद, चरण को किसी फ़्लो में जोड़ें और फिर फ़्लो चलाएं.
अपने ऐड-ऑन के लिए टेस्ट डिप्लॉयमेंट सेट अप करें:
- Apps Script एडिटर में स्क्रिप्ट प्रोजेक्ट खोलें.
- डिप्लॉय करें > टेस्ट डिप्लॉयमेंट पर क्लिक करें.
- इंस्टॉल करें क्लिक करें
- सबसे नीचे, हो गया पर क्लिक करें.
Apps Script प्रोजेक्ट को दूसरे उपयोगकर्ताओं के साथ शेयर करके, उन्हें ऐड-ऑन को आज़माने की अनुमति दी जा सकती है. इसके लिए, उन्हें बदलाव करने का ऐक्सेस देना ज़रूरी है. इसके बाद, उपयोगकर्ताओं को पिछले चरण पूरे करने के लिए कहें.
इंस्टॉल होने के बाद, यह ऐड-ऑन तुरंत Flows में उपलब्ध हो जाता है. ऐड-ऑन दिखने से पहले, आपको Flows को रीफ़्रेश करना पड़ सकता है. ऐड-ऑन का इस्तेमाल करने से पहले, आपको इसे अनुमति देनी होगी.
टेस्ट डिप्लॉयमेंट के बारे में ज़्यादा जानने के लिए, पब्लिश नहीं किए गए ऐड-ऑन को इंस्टॉल करना लेख पढ़ें.
फ़्लो खोलें.
ऐसा फ़्लो बनाएं जिसमें आपका चरण शामिल हो:
- नया फ़्लो पर क्लिक करें.
- चुनें कि फ़्लो कैसे शुरू होगा. किसी चरण की जांच करते समय, ऐसा स्टार्टर चुनें जिसे खुद चालू किया जा सके. जैसे, खुद को ईमेल भेजना. अगर आपके चरण के लिए इनपुट वैरिएबल की ज़रूरत है, तो स्टार्टर के आउटपुट के तौर पर इनपुट वैरिएबल को कॉन्फ़िगर करें.
- चरण जोड़ें पर क्लिक करें. आपने जिस चरण को बनाया या अपडेट किया है उसे चुनें. इसे कैलकुलेट करें कहा जाता है.
- अपना चरण कॉन्फ़िगर करें. कैलकुलेट करने के चरण के लिए, दो वैल्यू और गणित की कोई कार्रवाई चुनें. यह चरण अपने-आप सेव हो जाता है.
- अपने चरण के आउटपुट की जांच करने के लिए, एक और चरण जोड़ें. उदाहरण के लिए, किसी ईमेल मैसेज में आउटपुट जोड़ने के लिए, Gmail का मैसेज भेजें चरण जोड़ा जा सकता है. मैसेज में, वैरिएबल पर क्लिक करें और अपने चरण का आउटपुट चुनें. कैलकुलेट करने के चरण के लिए, वैरिएबल > दूसरा चरण: कैलकुलेट किया गया नतीजा > कैलकुलेट किया गया नतीजा चुनें. वैरिएबल, मैसेज फ़ील्ड में चिप के तौर पर दिखता है.
- चालू करें पर क्लिक करें. आपका फ़्लो चलाने के लिए तैयार है.
फ़्लो को शुरू करने के लिए, फ़्लो का स्टार्टर सेट करें. उदाहरण के लिए, अगर आपको ईमेल मिलने पर कोई फ़्लो शुरू करना है, तो खुद को एक ईमेल भेजें.
पुष्टि करें कि फ़्लो उम्मीद के मुताबिक काम कर रहा है. फ़्लो बिल्डर के गतिविधि टैब पर जाकर, लॉग देखें. गतिविधि टैब में कस्टम लॉग बनाने का तरीका जानने के लिए, गतिविधि लॉग देखें.
अगले चरण
आपने Workspace Flows के लिए, कस्टम फ़्लो स्टेप बना लिया है और उसकी जांच कर ली है. अब आपको ये काम करने की अनुमति है:
ज़्यादा मुश्किल लॉजिक लागू करने के लिए, Gemini से मदद लें और चरण को अपनी ज़रूरत के हिसाब से बनाएं.
कॉन्फ़िगरेशन कार्ड बनाएं, ताकि चरण के कॉन्फ़िगरेशन को पसंद के मुताबिक बनाया जा सके.
गतिविधि और गड़बड़ियों को लॉग करें, ताकि आपके चरण रिकॉर्ड किए जा सकें और उनसे जुड़ी समस्याओं को हल किया जा सके.
फ़्लो इवेंट ऑब्जेक्ट की समीक्षा करें, ताकि फ़्लो के चलने के दौरान भेजे और पाए गए JSON पेलोड की समीक्षा की जा सके.