คู่มือเริ่มต้นฉบับย่อนี้จะสอนวิธีสร้างขั้นตอนที่กำหนดเองสำหรับ Workspace Studio โดยใช้ Google Apps Script ขั้นตอนที่กำหนดเองจะรับตัวเลข 2 ตัวและการดำเนินการทางคณิตศาสตร์เป็นอินพุต ทำการคำนวณ และแสดงผลลัพธ์
วัตถุประสงค์
- สร้างขั้นตอนที่กำหนดเองสำหรับ Workspace Studio ด้วย Google Apps Script
- ทำให้ขั้นตอนที่กำหนดเองใช้งานได้กับองค์กร Google Workspace ของคุณเอง
- ทดสอบขั้นตอนที่กำหนดเองใน Workspace Studio
ข้อกำหนดเบื้องต้น
- บัญชี Google ที่มีสิทธิ์เข้าถึง Workspace Studio
ตั้งค่าสคริปต์
หากต้องการตั้งค่าสคริปต์ ให้สร้างโปรเจ็กต์ Apps Script ใหม่ แล้วเชื่อมต่อกับโปรเจ็กต์ที่อยู่ในระบบคลาวด์
คลิกปุ่มต่อไปนี้เพื่อเปิดโปรเจ็กต์ Apps Script คู่มือเริ่มต้นฉบับย่อของเครื่องคิดเลข
คลิก ภาพรวม
ในหน้าภาพรวม ให้คลิก
ทำสำเนา
ตั้งชื่อสำเนาโปรเจ็กต์ Apps Script ดังนี้
คลิกสำเนาคู่มือเริ่มต้นฉบับย่อของเครื่องคิดเลข
ในชื่อโปรเจ็กต์ ให้พิมพ์
Calculator quickstartคลิกเปลี่ยนชื่อ
ไม่บังคับ: ตรวจสอบโค้ดคู่มือเริ่มต้นฉบับย่อ
ในส่วนก่อนหน้า คุณได้คัดลอกโปรเจ็กต์ Apps Script ทั้งหมดที่มีโค้ดแอปพลิเคชันที่จำเป็นทั้งหมดสำหรับขั้นตอนที่กำหนดเองของโฟลว์ จึงไม่จำเป็นต้องคัดลอกและวางแต่ละไฟล์
คุณสามารถตรวจสอบแต่ละไฟล์ที่คัดลอกในส่วนก่อนหน้าได้ที่นี่ (ไม่บังคับ)
appsscript.jsonไฟล์ Manifest ไฟล์ 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 ขั้นตอนที่ชื่อ "คำนวณ" จะรับตัวเลข 2 ตัวและการดำเนินการเป็นอินพุต และแสดงผลลัพธ์ของการคำนวณ
ดูโค้ด
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 flow 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); }
ทำให้ขั้นตอนใช้งานได้และทดสอบ
หากต้องการทดสอบขั้นตอน ให้ตั้งค่าการทำให้ใช้งานได้สำหรับการทดสอบสำหรับส่วนเสริม เพิ่มขั้นตอนลงในโฟลว์ แล้วเรียกใช้โฟลว์
ตั้งค่าการทำให้ใช้งานได้สำหรับการทดสอบสำหรับส่วนเสริมดังนี้
- เปิดโปรเจ็กต์สคริปต์ในเครื่องมือแก้ไข Apps Script
- คลิกทำให้ใช้งานได้ > การทำให้ใช้งานได้สำหรับการทดสอบ
- คลิกติดตั้ง
- คลิกเสร็จสิ้น ที่ด้านล่าง
คุณสามารถให้ผู้ใช้รายอื่นทดสอบส่วนเสริมได้โดยแชร์โปรเจ็กต์ Apps Script กับบัญชีของผู้ใช้ (ต้องมีสิทธิ์เข้าถึงเพื่อแก้ไข) จากนั้นแจ้งให้ผู้ใช้ทำตามขั้นตอนก่อนหน้า
เมื่อติดตั้งแล้ว ส่วนเสริมจะพร้อมใช้งานในโฟลว์ทันที คุณอาจต้องรีเฟรชโฟลว์ก่อนที่ส่วนเสริมจะปรากฏขึ้น นอกจากนี้ คุณต้องให้สิทธิ์ส่วนเสริมก่อนจึงจะใช้งานได้
ดูข้อมูลเพิ่มเติมเกี่ยวกับการทำให้ใช้งานได้สำหรับการทดสอบได้ที่ ติดตั้งส่วนเสริมที่ไม่ได้เผยแพร่
เปิดโฟลว์
สร้างโฟลว์ที่มีขั้นตอนของคุณดังนี้
- คลิก โฟลว์ใหม่.
- เลือกวิธีเริ่มโฟลว์ หากขั้นตอนต้องใช้ตัวแปรอินพุต ให้กำหนดค่าตัวแปรอินพุตเป็นส่วนหนึ่งของเอาต์พุตของตัวเริ่ม
- คลิก เพิ่มขั้นตอน เลือกขั้นตอนที่คุณสร้างหรืออัปเดต ซึ่งเรียกว่าคำนวณ
- กำหนดค่าขั้นตอน สำหรับขั้นตอนการคำนวณ ให้เลือกค่า 2 ค่าและการดำเนินการทางคณิตศาสตร์ ระบบจะบันทึกขั้นตอนโดยอัตโนมัติ
- หากต้องการทดสอบเอาต์พุตของขั้นตอน ให้เพิ่มอีกขั้นตอน เช่น หากต้องการเพิ่มเอาต์พุตลงในข้อความแชทของ Google Chat ให้เพิ่มขั้นตอนแจ้งเตือนฉันใน Chat ของ Chat ในข้อความ ให้คลิก ตัวแปร แล้วเลือกเอาต์พุตของ ขั้นตอน สำหรับขั้นตอนการคำนวณ ให้เลือก ตัวแปร > ขั้นตอนที่ 2: คำนวณ > ผลลัพธ์ที่คำนวณ ตัวแปรจะปรากฏเป็นชิปในช่องข้อความ
- คลิกเปิด โฟลว์พร้อมที่จะทำงานแล้ว
เรียกใช้โฟลว์โดยเริ่มตัวเริ่มของโฟลว์ เช่น หากโฟลว์เริ่มตามกำหนดการ โฟลว์จะทำงานในวันที่และเวลาที่ระบุ
ยืนยันว่าโฟลว์ทำงานตามที่คาดไว้ ตรวจสอบบันทึกโดยไปที่แท็บกิจกรรม ของเครื่องมือสร้างโฟลว์ ดูวิธีสร้างบันทึกที่กำหนดเองในแท็บกิจกรรมได้ที่ บันทึกกิจกรรม
ขั้นตอนถัดไป
คุณสร้างและทดสอบขั้นตอนที่กำหนดเองสำหรับ Workspace Studio เรียบร้อยแล้ว ตอนนี้คุณสามารถทำสิ่งต่อไปนี้ได้
ปรับแต่งขั้นตอนต่อไปโดยแจ้งให้ Geminiช่วยคุณใช้ตรรกะที่ซับซ้อนมากขึ้น
สร้างการ์ดการกำหนดค่า เพื่อปรับแต่งการกำหนดค่าขั้นตอน
บันทึกกิจกรรมและข้อผิดพลาด เพื่อ บันทึกและแก้ปัญหาการเรียกใช้ขั้นตอน
ตรวจสอบออบเจ็กต์เหตุการณ์ เพื่อ ตรวจสอบเพย์โหลด JSON ที่โฟลว์ส่งและรับเมื่อขั้นตอนทำงาน