/*========================================
渲染網頁
=========================================*/
function render(file, argsObject, title='') {
let tmp = HtmlService.createTemplateFromFile(file);
for(let i in argsObject){
tmp[i] = argsObject[i];
}
if(title){//主樣版
return tmp.evaluate().setTitle(title).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL).addMetaTag('viewport', 'width=device-width, initial-scale=1');
}else{//子樣版
return tmp.evaluate().getContent();
}
}
function doGet(e){
let title = '育將電腦';
let menu = render('menu',{title: title});
return render('index',{menu: menu}, title);
}
將「程式碼.gs」有關「custom」的函式,通通搬到「prog_custom.gs」
/*========================================
取得結構
=========================================*/
function get_stru_custom() {
let stru = [
{ "title": "流水號", "type": "" },
{ "title": "客戶名稱", "type": "文字" },
{ "title": "客戶電話", "type": "文字" },
{ "title": "客戶地址", "type": "文字" },
{ "title": "備註", "type": "文字" }
];
return stru;
}
/*========================================
取得標題列
=========================================*/
function get_head_custom() {
let ss = SpreadsheetApp.getActiveSpreadsheet()
let ws = ss.getSheetByName('day2');
let headData = ws.getSheetValues(1, 1, 1, ws.getLastColumn())[0];
console.log(headData);
return headData;
}
/*========================================
取得資料
=========================================*/
function get_data_custom() {
let ss = SpreadsheetApp.getActiveSpreadsheet()
let ws = ss.getSheetByName('day2');
if (ws.getLastRow() < 2) return [];//無資料
let data_custom = ws.getSheetValues(2, 1, ws.getLastRow() - 1, ws.getLastColumn());
console.log(data_custom[0]);
return data_custom;
}
/*=====================================
設定標題
=====================================*/
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;
setCellData(sheet, rowIndex, colIndex, stru[i]['title']);
}
}
/*========================================
設定客戶資料
=========================================*/
function set_data_custom() {
let customData = [1, '育將電腦', '0123456789', '台南市永康區大灣路158號', '備註1'];
let sheet = 'day2';
let rowIndex = 2;
for (let i in customData) {
let colIndex = Number(i) + 1;
setCellData(sheet, rowIndex, colIndex, customData[i]);
}
customData = [2, 'Google', '1234567890', '美國', '備註2'];
rowIndex = 3;
for (let i in customData) {
let colIndex = Number(i) + 1;
setCellData(sheet, rowIndex, colIndex, customData[i]);
}
}
在GAS除了用試算表來儲存資料,還有指令碼屬性,可以儲存。
Google Apps Script(GAS)中的指令碼屬性(Script Properties)用於存儲與腳本項目相關的全局配置設置或需要在腳本執行之間保留的數據。
var SCRIPT_PROP = PropertiesService.getScriptProperties();
setProperty(屬性,值)
getProperty(屬性)
/*=====================================
安裝程式
=====================================*/
function setup() {
//管理員email
SCRIPT_PROP.setProperty("adminEmail", Session.getActiveUser().getEmail());
}
// 權限
let isAdmin = SCRIPT_PROP.getProperty("adminEmail") === Session.getActiveUser().getEmail() ? true : false;