क्विकस्टार्ट: Google Apps Script की मदद से कैलकुलेटर वाला चरण बनाना

इस क्विकस्टार्ट गाइड में, Google Apps Script का इस्तेमाल करके Workspace Studio के लिए कस्टम स्टेप बनाने का तरीका बताया गया है. कस्टम चरण में, दो संख्याओं और अंकगणित की किसी कार्रवाई को इनपुट के तौर पर लिया जाता है. इसके बाद, कैलकुलेशन की जाती है और नतीजा दिखाया जाता है.

उपयोगकर्ता, एजेंट के हिस्से के तौर पर कैलकुलेटर स्टेप को कॉन्फ़िगर करता है.

पहली इमेज: इसमें दिखाया गया है कि उपयोगकर्ता, एजेंट के हिस्से के तौर पर कैलकुलेटर स्टेप को कॉन्फ़िगर कर रहा है.

मकसद

  • Google Apps Script की मदद से, Workspace Studio के लिए पसंद के मुताबिक चरण बनाएं.
  • कस्टम चरण को अपने Google Workspace संगठन में डिप्लॉय करें.
  • Workspace Studio में कस्टम चरण को टेस्ट करें.

ज़रूरी शर्तें

  • Workspace Studio का ऐक्सेस वाला Google खाता.

स्क्रिप्ट सेट अप करना

स्क्रिप्ट सेट अप करने के लिए, एक नया Apps Script प्रोजेक्ट बनाएं. इसके बाद, उसे अपने Cloud प्रोजेक्ट से कनेक्ट करें.

  1. Calculator quickstart Apps Script प्रोजेक्ट खोलने के लिए, इस बटन पर क्लिक करें.

    प्रोजेक्ट खोलें

  2. खास जानकारी पर क्लिक करें.

  3. खास जानकारी वाले पेज पर, कॉपी बनाने का आइकॉन कॉपी बनाएं पर क्लिक करें.

  4. Apps Script प्रोजेक्ट की कॉपी का नाम डालें:

    1. Calculator quickstart की कॉपी पर क्लिक करें.

    2. प्रोजेक्ट का टाइटल में, Calculator quickstart टाइप करें.

    3. नाम बदलें पर क्लिक करें.

ज़रूरी नहीं: क्विकस्टार्ट कोड की समीक्षा करना

पिछले सेक्शन में, आपने पूरे 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"
                }
              }
            ],
            "onConfigFunction": "onConfigCalculateFunction",
            "onExecuteFunction": "onExecuteCalculateFunction"
          }
        }
      ]
    }
  }
}
Calculator.gs

Google Workspace Studio के लिए कस्टम चरण तय करता है. "कैलकुलेट करें" नाम वाले इस चरण में, दो संख्याओं और एक ऑपरेशन को इनपुट के तौर पर लिया जाता है. इसके बाद, कैलकुलेशन का नतीजा दिखाया जाता है.

Calculator.gs कोड देखना

/**
 * This script defines a custom step for Google Workspace Studio.
 * 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/studio/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 step.
 *
 * This function generates a button definition that, when clicked, triggers
 * a save action for the current step 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 step.
 *
 * The input fields are configured to let the user select outputs from previous
 * steps as input values using the `hostAppDataSource` property.
 * This function is called when the user adds or edits the "Calculate" step in the 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];
}

/**
* Returns output variables from a step.
*
* This function constructs an object that, when returned, sends the
* provided variable values as output from the current step.
* The variable values are logged to the console for debugging purposes.
*/
function outputVariables(variableDataMap) {
 const workflowAction = AddOnsResponseService.newReturnOutputVariablesAction()
   .setVariableDataMap(variableDataMap);

 const hostAppAction = AddOnsResponseService.newHostAppAction()
   .setWorkflowAction(workflowAction);

 const renderAction = AddOnsResponseService.newRenderActionBuilder()
   .setHostAppAction(hostAppAction)
   .build();

 return renderAction;
}

/**
 * Executes the calculation action based on the inputs from an event.
 *
 * This function retrieves input values ("value1", "value2") and the "operation"
 * from the event, performs the calculation, and returns the "result" and
 * "log" as output variables.
 * This function is called when the agent reaches this custom step.
 * @param {Object} event The event object passed by the runtime.
 * @return {Object} The output variables object.
 */
function onExecuteCalculateFunction(event) {
 console.log("output: " + JSON.stringify(event));
 var calculatedValue = 0;
 var value1 = event.workflow.actionInvocation.inputs["value1"].integerValues[0];
 var value2 = event.workflow.actionInvocation.inputs["value2"].integerValues[0];
 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;
 }

 const variableDataMap = { "result": AddOnsResponseService.newVariableData().addIntegerValue(calculatedValue) };

 return outputVariables(variableDataMap);
}

अपने चरण को लागू करना और उसकी जांच करना

अपने चरण को आज़माने के लिए, अपने ऐड-ऑन के लिए टेस्ट डिप्लॉयमेंट सेट अप करें. इसके बाद, किसी एजेंट में चरण जोड़ें और फिर एजेंट को चलाएं.

  1. अपने ऐड-ऑन के लिए टेस्ट डिप्लॉयमेंट सेट अप करें:

    1. Apps Script एडिटर में स्क्रिप्ट प्रोजेक्ट खोलें.
    2. डिप्लॉय करें > टेस्ट डिप्लॉयमेंट पर क्लिक करें.
    3. इंस्टॉल करें क्लिक करें.
    4. सबसे नीचे, हो गया पर क्लिक करें.

    Apps Script प्रोजेक्ट को दूसरे उपयोगकर्ताओं के साथ शेयर करके, उन्हें ऐड-ऑन को आज़माने की अनुमति दी जा सकती है. इसके लिए, उन्हें बदलाव करने का ऐक्सेस देना ज़रूरी है. इसके बाद, उपयोगकर्ताओं को पिछले चरण पूरे करने के लिए कहें.

    इंस्टॉल होने के बाद, यह ऐड-ऑन एजेंट के लिए तुरंत उपलब्ध हो जाता है. ऐड-ऑन दिखने से पहले, आपको एजेंटों की सूची रीफ़्रेश करनी पड़ सकती है. ऐड-ऑन का इस्तेमाल करने से पहले, आपको इसे अनुमति देनी होगी.

    टेस्ट डिप्लॉयमेंट के बारे में ज़्यादा जानने के लिए, पब्लिश नहीं किए गए ऐड-ऑन को इंस्टॉल करना लेख पढ़ें.

  2. एजेंट खोलें.

  3. ऐसा एजेंट बनाएं जिसमें आपका चरण शामिल हो:

    1. नया एजेंट पर क्लिक करें.
    2. चुनें कि एजेंट कैसे शुरू होगा. किसी चरण की जांच करते समय, ऐसा स्टार्टर चुनें जिसे खुद चालू किया जा सके. जैसे, खुद को ईमेल भेजना. अगर आपके चरण के लिए किसी इनपुट वैरिएबल की ज़रूरत है, तो स्टार्टर के आउटपुट के तौर पर इनपुट वैरिएबल को कॉन्फ़िगर करें.
    3. चरण जोड़ें पर क्लिक करें. आपने जिस चरण को बनाया या अपडेट किया है उसे चुनें. इसे कैलकुलेट करें कहा जाता है.
    4. अपने चरण को कॉन्फ़िगर करें. कैलकुलेट करने के चरण के लिए, दो वैल्यू और गणित की कोई कार्रवाई चुनें. यह चरण अपने-आप सेव हो जाता है.
    5. अपने चरण के आउटपुट की जांच करने के लिए, एक और चरण जोड़ें. उदाहरण के लिए, किसी ईमेल मैसेज में आउटपुट जोड़ने के लिए, Gmail का मैसेज भेजें चरण जोड़ा जा सकता है. मैसेज में, वैरिएबल पर क्लिक करें और अपने चरण का आउटपुट चुनें. कैलकुलेट करने के चरण के लिए, वैरिएबल > दूसरा चरण: कैलकुलेट किया गया नतीजा > कैलकुलेट किया गया नतीजा चुनें. वैरिएबल, मैसेज फ़ील्ड में चिप के तौर पर दिखता है.
    6. चालू करें पर क्लिक करें. आपका एजेंट चलाने के लिए तैयार है.
  4. अपने एजेंट के स्टार्टर को चालू करके, एजेंट को चलाएँ. उदाहरण के लिए, अगर आपको ईमेल मिलने पर एजेंट शुरू करना है, तो खुद को एक ईमेल भेजें.

  5. पुष्टि करें कि एजेंट उम्मीद के मुताबिक काम कर रहा है. एजेंट बिल्डर के गतिविधि टैब पर जाकर, लॉग देखें. गतिविधि टैब में कस्टम लॉग बनाने का तरीका जानने के लिए, गतिविधि के लॉग देखें.

अगले चरण

आपने Workspace Studio के लिए, कस्टम चरण बना लिया है और उसकी जांच कर ली है. अब आपको ये काम करने की अनुमति है: