線上書籍

Home

雲端工具入門

增加欄位
  1. 在「餐點」右邊增加「單價」
  2. 糖、冰 [冰] 右邊增加「小計」
印出傳入變數e
  1. console.log(e); => 去執行填表單並送出=>在編輯器的執行項目觀看變數
Logging output too large. Truncating output. { toString: [Function], authMode: { toString: [Function: toString], name: [Function: toString], toJSON: [Function: toString], ordinal: [Function: ordinal], compareTo: [Function: compareTo], NONE: { toString: [Function: toString], name: [Function: toString], toJSON: [Function: toString], ordinal: [Function: ordinal], compareTo: [Function: compareTo], NONE: [Circular], CUSTOM_FUNCTION: [Object], LIMITED: [Object], FULL: [Circular] }, CUSTOM_FUNCTION: { toString: [Function: toString], name: [Function: toString], toJSON: [Function: toString], ordinal: [Function: ordinal], compareTo: [Function: compareTo], NONE: [Object], CUSTOM_FUNCTION: [Circular], LIMITED: [Object], FULL: [Circular] }, LIMITED: { toString: [Function: toString], name: [Function: toString], toJSON: [Function: toString], ordinal: [Function: ordinal], compareTo: [Function: compareTo], NONE: [Object], CUSTOM_FUNCTION: [Object], LIMITED: [Circular], FULL: [Circular] }, FULL: [Circular] }, namedValues: { '時間戳記': [ '2024/5/23 下午 5:44:03' ], '糖、冰 [糖]': [ '5分' ], '糖、冰 [冰]': [ '5分' ], '是否升級為套餐?(附飲料甜點,加 25 元)': [ '是' ], '姓名': [ '123' ], '餐點': [ '照燒雞腿飯 $130元' ] }, range: { toString: [Function], setComment: [Function], getComment: [Function], getFilter: [Function], setNumberFormat: [Function], getNumberFormat: [Function], clear: [Function], getValue: [Function], isBlank: [Function], merge: [Function], check: [Function], setValue: [Function], offset: [Function], sort: [Function], copyTo: [Function], setValues: [Function], trimWhitespace: [Function], createDataSourceTable: [Function], setTextRotation: [Function], getA1Notation: [Function], setFontFamilies: [Function], setFontLine: [Function], setFontLines: [Function], setFontSizes: [Function], setFontStyle: [Function], setFontStyles: [Function], setFontWeight: [Function], setFontWeights: [Function], setHorizontalAlignments: [Function], setNumberFormats: [Function], setVerticalAlignments: [Function], copyValuesToRange: [Function], copyFormatToRange: [Function], getFontColor: [Function], getFontColorObject: [Function], getFontColors: [Function], getFontColorObjects: [Function], getFontLine: [Function], getFontLines: [Function], getFontSizes: [Function], getFontStyle: [Function], getFontStyles: [Function], getFontWeights: [Function], getHorizontalAlignment: [Function], getHorizontalAlignments: [Function], getVerticalAlignments: [Function], isStartColumnBounded: [Function], isStartRowBounded: [Function], isEndColumnBounded: [Function], isEndRowBounded: [Function], autoFillToNeighbor: [Function], setShowHyperlink: [Function], getTextRotation: [Function], getTextRotations: [Function], setTextRotations: [Function], setVerticalText: [Function], setTextDirections: [Function], getTextDirections: [Function], splitTextToColumns: [Function], applyRowBanding: [Function], applyColumnBanding: [Function], setWrapStrategy: [Function], setWrapStrategies: [Function], getWrapStrategy: [Function], getWrapStrategies: [Function], createPivotTable: [Function], createDataSourcePivotTable: [Function], shiftRowGroupDepth: [Function], shiftColumnGroupDepth: [Function], expandGroups: [Function], collapseGroups: [Function], getRichTextValue: [Function], getRichTextValues: [Function], setRichTextValue: [Function], setRichTextValues: [Function], getTextStyles: [Function], setTextStyles: [Function], insertCheckboxes: [Function], removeCheckboxes: [Function], activateAsCurrentCell: [Function], deleteCells: [Function], getEndColumn: [Function], getNextDataCell: [Function], getDataRegion: [Function], getFormulaR1C1: [Function], getFormulasR1C1: [Function], getDataSourceFormula: [Function], getNumberFormats: [Function], getBackgroundColors: [Function], insertCells: [Function], setFormulas: [Function], setFormulaR1C1: [Function], setFormulasR1C1: [Function], setBackgroundColors: [Function], getDisplayValue: [Function], getDisplayValues: [Function], mergeAcross: [Function], mergeVertically: [Function], isPartOfMerge: [Function], setBackgroundObject: [Function], setBackgrounds: [Function], getBackgroundObject: [Function], getBackgrounds: [Function], getBackgroundObjects: [Function], setBackgroundRGB: [Function], setComments: [Function], setFontColor: [Function], setFontColorObject: [Function], setFontColors: [Function], setBackground: [Function], clearComment: [Function], getNumRows: [Function], getFontFamilies: [Function], getFormulas: [Function], activate: [Function], uncheck: [Function], getEndRow: [Function], breakApart: [Function], getBorders: [Function], getSheet: [Function], setBorder: [Function], setWrap: [Function], setWraps: [Function], getGridId: [Function], getWrap: [Function], getWraps: [Function], randomize: [Function], autoFill: [Function], isChecked: [Function], getBorder: [Function], getTextDirection: [Function], setTextDirection: [Function], getRowIndex: [Function], getColumnIndex: [Function], getTextStyle: [Function], getFontWeight: [Function], getBackground: [Function], getNumColumns: [Function], getBackgroundColor: [Function], setBackgroundColor: [Function], getVerticalAlignment: [Function], getFontFamily: [Function], setFontFamily: [Function], setTextStyle: [Function], getComments: [Function], getCell: [Function], getRow: [Function], getWidth: [Function], clearFormat: [Function], moveTo: [Function], clearContent: [Function], createFilter: [Function], protect: [Function], getMergedRanges: [Function], setBackgroundObjects: [Function], setFontColorObjects: [Function], getDataValidation: [Function], getDataValidations: [Function], setDataValidation: [Function], setDataValidations: [Function], clearDataValidations: [Function], setFontSize: [Function], getHeight: [Function], getLastRow: [Function], getFontSize: [Function], getDataSourceUrl: [Function], getDataTable: [Function], getBandings: [Function], createTextFinder: [Function], addDeveloperMetadata: [Function], getDeveloperMetadata: [Function], getLastColumn: [Function], getDataSourcePivotTables: [Function], getDataSourceTables: [Function], getDataSourceFormulas: [Function], createDeveloperMetadataFinder: [Function], setHorizontalAlignment: [Function], setVerticalAlignment: [Function], canEdit: [Function], setNotes: [Function], getNotes: [Function], getFormula: [Function], setFormula: [Function], getColumn: [Function], getNote: [Function], clearNote: [Function], setNote: [Function], removeDuplicates: [Function], getValues: [Function], columnEnd: 7, columnStart: 1, rowEnd: 3, rowStart: 3 }, source: { toString: [Function], isReadable: [Function], isWritable: [Function], getName: [Function], getKey: [Function], setName: [Function], getId: [Function], copy: [Function], getOwner: [Function], rename: [Function], getUrl: [Function], getSheets: [Function], addMenu: [Function], removeMenu: [Function], msgBox: [Function], inputBox: [Function], toast: [Functi
  1. 請看到「namedValues」
  2. 我們也要利用 head
  3. console.log(e.namedValues);
    console.log(head);
  4. 2024年5月23日 下午5:49:47 偵錯 { '餐點': [ '日式豬排飯 $120 元' ], '時間戳記': [ '2024/5/23 下午 5:49:45' ], '姓名': [ 'qqq' ], '糖、冰 [糖]': [ '7分' ], '糖、冰 [冰]': [ '7分' ], '是否升級為套餐?(附飲料甜點,加 25 元)': [ '是' ] } 2024年5月23日 下午5:49:47 偵錯 [ '時間戳記', '姓名', '餐點', '單價', '是否升級為套餐?(附飲料甜點,加 25 元)', '糖、冰 [糖]', '糖、冰 [冰]', '小計' ]

     

增加程式碼:在 let head 與 let row 之間 //------------------------------------- 傳入的值 let namedValues = e.namedValues; //------------------------------------- 欄號 let priceIndex = head.indexOf('單價') + 1; //單價欄號 let subtotalIndex = head.indexOf('小計') + 1; //小計欄號 //------------------------------------- 取得餐點、是否升級 let meal = namedValues['餐點'][0]; // 餐點 let upgrade = namedValues['是否升級為套餐?(附飲料甜點,加 25 元)'][0]; // 是否升級為套餐? //-------------------------------------在這裡進行單價和小計的計算(根據您的需求) let price = extractNumberFromString(meal); // 如果升級為套餐,則加 25 元 let subtotal = (upgrade === '是') ? price + 25 : price; //------------------------------------- 寫入單價和小計 ws.getRange(rowIndex, priceIndex).setValue(price); // 單價 ws.getRange(rowIndex, subtotalIndex).setValue(subtotal); // 小計 將餐點的單價分離函式 function extractNumberFromString(str) { // 使用正则表达式匹配字符串中的数字 var matches = str.match(/\d+/); if (matches && matches.length > 0) { var numberValue = parseInt(matches[0]); // 将匹配的数字字符串转换为整数 return numberValue; } else { // Logger.log('未找到数字'); return 0; } }