/*============================================
得到 全域變數 結構
============================================*/
function get_stru_global(){
let stru = [
{ title: '網站標題',
name: 'web_title',
sort: 1,
kind: '簡答',
type: '文字',
option: '',
validate: '必填',
form_width: 8,
default: 'XXX',
readme: '',
fun: '',
list_show: '',
list_sort: '',
list_align: '',
list_width: '' },
{ title: '螢幕寬度',
name: 'width',
sort: 2,
kind: '下拉選單',
type: '文字',
option: 'container,container-fluid',
validate: '',
form_width: 2,
default: 'container',
readme: '',
fun: '',
list_show: '',
list_sort: '',
list_align: '',
list_width: '' },
{ title: '記錄LineBot',
name: 'record_line_bot',
sort: 9,
kind: '下拉選單',
type: '文字',
option: ',是',
validate: '',
form_width: 2,
default: '',
readme: '',
fun: '',
list_show: '',
list_sort: '',
list_align: '',
list_width: '' },
{ title: 'CHANNEL_ACCESS_TOKEN',
name: 'CHANNEL_ACCESS_TOKEN',
sort: 3,
kind: '段落',
type: '文字',
option: '',
validate: '',
form_width: 12,
default: '',
readme: '',
fun: '',
list_show: '',
list_sort: '',
list_align: '',
list_width: '' },
{ title: '首頁內容',
name: 'index_content',
sort: 25,
kind: '網頁編輯器',
type: '文字',
option: '',
validate: '',
form_width: 12,
default: '',
readme: '',
fun: '',
list_show: '',
list_sort: '',
list_align: '',
list_width: '' }];
//console.log(stru);
return stru;
}
/*============================================
建立 全域變數 create_global();
============================================*/
function create_global(sheet="全域變數"){
let ws = Sheet.getWs(sheet);
let rowIndex,global;
let stru = get_stru_global();
if(ws === null){
//建立工作表
Sheet.createSheet(sheet);
//取得工作表
ws = Sheet.getWs(sheet);
for(let i in stru){
rowIndex = parseInt(i) + 1;
Sheet.setCellData(sheet,rowIndex,1,stru[i]['title'],'文字');
Sheet.setCellData(sheet,rowIndex,2,stru[i]['default'],stru[i]['type']);
}
}else{
global = get_global();
for(let i in stru){
rowIndex = parseInt(i) + 1;
if(global[stru[i]['title']] === null){
Sheet.setCellData(sheet,rowIndex,1,stru[i]['title'],'文字');
Sheet.setCellData(sheet,rowIndex,2,stru[i]['default'],stru[i]['type'][i]);
}else{
Sheet.setCellData(sheet,rowIndex,1,stru[i]['title'],'文字');
Sheet.setCellData(sheet,rowIndex,2,global[stru[i]['title']],stru[i]['type'][i]);
}
}
}
}
/*======================================
取得全域變數
======================================*/
function get_global(){
let stru = get_stru_global();
let global = {};
let rowIndex,range;
let ws = Sheet.getWs('全域變數');
for(let i in stru){
rowIndex = parseInt(i) + 1;
range_title = ws.getRange(rowIndex, 1);
range = ws.getRange(rowIndex, 2);
if(range_title.getValue()){
global[stru[i]['title']] = range.getValue();
}else{
global[stru[i]['title']] = null;
}
}
global['網站標題'] = global['網站標題'] ? global['網站標題'] : '請設立網站標題';
global['url'] = ScriptApp.getService().getUrl();//取得網頁應用程式網址
let isAdmin = JSON.parse(SCRIPT_PROP.getProperty("adminEmail"));
global['adminEmail'] = isAdmin[0];//Session.getActiveUser().getEmail();
global['userEmail'] = Session.getActiveUser().getEmail();
global['isAdmin'] = isAdmin.indexOf(global['userEmail']) != -1 ? true : false;//
global['emailQuota'] = MailApp.getRemainingDailyQuota();
global['CHANNEL_ACCESS_TOKEN'] = global['CHANNEL_ACCESS_TOKEN'] ? global['CHANNEL_ACCESS_TOKEN'].trim() : '';
if(SCRIPT_PROP.getProperty("LINE") === null){
global['LINE'] = {
userId:'', //機器人的用戶 ID
basicId:'', //機器人的基本ID
displayName:'', //機器人的顯示名稱
pictureUrl:'', //個人資料圖片網址
chatMode:'', //聊天模式 chat:響應模式設置為“聊天”。 bot:響應模式設置為“Bot”。
markAsReadMode:'' //標記為已讀模式 auto:啟用自動讀取設置。 manual:禁用自動讀取設置。
};
}else{
global['LINE'] = JSON.parse(SCRIPT_PROP.getProperty("LINE"));
}
//global['LINE'] = JSON.parse(SCRIPT_PROP.getProperty("LINE"));
//console.log(global['LINE']);
global['op'] = '';
return global;
}
/*============================================
得到 全域變數 資料
============================================*/
function get_row_global(sheet="全域變數"){
let ws = Sheet.getWs(sheet);
let range = ws.getRange(1, 2, ws.getLastRow());
let row = range.getValues().map(function(r){
return r[0];
});
return row;
}
/* ==========================
全域變數 表單函式
============================*/
function form_global(e) {
//-------------------------------------------- 取得 global結構
let stru = get_stru_global();//取得 全域變數 結構
let row = get_row_global();//取得 全域變數 資料
//-------------------------------------------- 取得必填欄位,過濾「required」有值
var validate = stru.filter(function(r){
return r['validate'] !== "";
});
//將規則轉成文字
let rules = validate.map(function(r){
if(r['validate'] == "email"){
return "'" + r['name'] +"': { required: true, email : true}" ;
} else{
return "'" + r['name'] +"': { required: true }" ;
}
}).join(",");
//將訊息轉成文字
let messages = validate.map(function(r){
if(r['validate'] == "email"){
return "'" + r['name'] +"': { required: '必填', email : 'email格式不正確'}" ;
} else{
return "'" + r['name'] +"': { required: '必填' }" ;
}
}).join(",");
global['formTitle'] = '全域變數';
//-------------------------------------------- 表單
let content = Sheet.render("form_global", {global: global, stru: stru, row: row, rules: rules, messages: messages});
//-------------------------------------------- 渲染網頁
return Sheet.render('index', {global: global, menu: menu, content: content}, global['formTitle']);
}
/* ==========================
寫入試算表
============================*/
function insert_global(formData){
//-------------------------------------------------------- 取得全域變數
let global = get_global();//取得全域變數
let sheet = "全域變數";
//--------------------------------------------- 設定工作表
let ws = Sheet.getWs(sheet);
//檢查 接收工作表
if(ws == null){
//檢查 全域變數
create_global();
ws = Sheet.getWs(sheet);
}
//-------------------------------------------------------- 取得限制條件
//-------------------------------------------------------- 取得 全域變數
let stru = get_stru_global();
//-------------------------------------------------------- 寫入資料
let rowIndex;
for(let i in stru){
rowIndex = parseInt(i) + 1;
Sheet.setCellData(sheet, rowIndex, 2, formData[stru[i]['name']], stru[i]['type']);
}
//----------------------------------------將「CHANNEL_ACCESS_TOKEN」寫入 指令碼屬性
SCRIPT_PROP.setProperty("CHANNEL_ACCESS_TOKEN", formData['CHANNEL_ACCESS_TOKEN']);
//----------------------------------------將「LineBot Info」寫入 指令碼屬性
SCRIPT_PROP.setProperty("LINE", JSON.stringify(getLineUserInfo(formData['CHANNEL_ACCESS_TOKEN'])));
//-------------------------------------取得全域變數
global = get_global();
//-------------------------------------menu子樣板
menu = Sheet.render('menu', {global: global});
let button = Sheet.render("button", {bg: 'btn-success',title: '全域變數', url: global.url+'?op=form_global',target:'' });
return Sheet.render('show_message', {global: global, title: '全域變數 資料寫入成功', message: button, bg: 'bg-primary'});
}