鐵人賽-Google Apps Script整合運用
	增加一個參數
  - 
	
資料寫入單一儲存格:將函式增加一個「type」的參數,且預設值為 ''
/*======================================== 資料寫入單一儲存格 =========================================*/ function setCellData(sheet, rowIndex, colIndex, value, type = '') { let ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet); let range = ws.getRange(rowIndex, colIndex); if (type === '文字') { range.setNumberFormat('@').setValue(value); // 指定格式為文字 } else { range.setValue(value); } }
 - 
	
當我們增加「type」時,資料是一維陣列,似乎不夠用,此時可以考慮用 陣列 包 物件 [{}, {}] 的方式,整理資料
陣列:取值靠「索引」,有順序性 => 0 1 2 .......
物件:取值靠「屬性」 - 
	
規劃 結構
/*======================================== 取得結構 =========================================*/ function get_stru_custom() { let stru = [ { title: '流水號', type: '' }, { title: '客戶名稱', type: '文字' }, { title: '客戶電話', type: '文字' }, { title: '客戶地址', type: '文字' }, { title: '備註', type: '文字' }, ]; return stru; }
 - 
	
設定標題列,變更為
/*======================================== 設定標題列 =========================================*/ function set_head_custom() { let stru = get_stru_custom(); let sheet = 'day2'; let rowIndex = 1; for (let i in stru) { let colIndex = Number(i) + 1;//i 型態為字串,須轉為數字做計算 setCellData(sheet, rowIndex, colIndex, stru[i]['title'], '文字'); } } - 
	
設定客戶資料
/*======================================== 設定客戶資料 =========================================*/ function set_data_custom() { let sheet = 'day2'; let ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet); let stru = get_stru_custom(); let rowIndex, customData; // ---------------------------------寫入一筆記錄 rowIndex = ws.getLastRow() + 1; customData = [1, '育將電腦', '0123456789', '台南市永康區大灣路158號', '備註1']; for (let i in customData) { let colIndex = Number(i) + 1;//i 型態為字串,須轉為數字做計算 setCellData(sheet, rowIndex, colIndex, customData[i], stru[i]['type']); } // ---------------------------------寫入一筆記錄 end // ---------------------------------寫入一筆記錄 rowIndex = ws.getLastRow() + 1; customData = [2, 'Google', '1234567890', '美國', '備註2']; for (let i in customData) { let colIndex = Number(i) + 1;//i 型態為字串,須轉為數字做計算 setCellData(sheet, rowIndex, colIndex, customData[i], stru[i]['type']); } // ---------------------------------寫入一筆記錄 end } - 
	
第5項結果
流水號,我們希望當
新增時,流水號是空值,讓系統自動取得最大值+1
編輯時,比對流水號是在什麼「列指標」,進而將資料更新
待我們後面章節,介紹到「在試算表撈出資料」,再來說明

 
.videobox {
          position: relative;
          width: 100%;
          height: 0;
          padding-bottom: 56.25%;
        }
        .videobox iframe {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
	Youtube影片:https://youtu.be/33auG2a1s6A