用雲端打造自動化環境(永康)
//Line權杖
var token = "GMUKLBlRjCxl3fGNveDtvPFPu1i2YmtH2DzEnv4Lti8";
//日曆id
var googleCalendarID = "ugm158@gmail.com";
function getFormData(e) {
console.log(e);
var ss = SpreadsheetApp.getActive();
var ws = ss.getActiveSheet();
var title = ws.getRange(1, 1, 1, ws.getLastColumn()).getValues()[0];//取得表單中文欄名
var message = "\n";
var data = {};
for(i in title){
message += title[i] + ":" + e.values[i] + "\n";
data[title[i]] = e.values[i];
}
//通知Line
sendLineNotify(message);
//通知日曆
//時間戳記 繳費單位 繳費日期 繳費金額 備註
var title = data['繳費單位'] + "-" + data['繳費金額']; //日曆標題
var startDate = data['繳費日期'];//日期
var description = data['繳費單位'] + "\n" + data['繳費日期'] + "\n" + data['繳費金額']+ "\n" + data['備註'];//日曆內容
var color = 11;
setCalendar(title, startDate, description, color);
}
function sendLineNotify(message){
var options =
{
"method" : "post",
"payload" : {"message" : message},
"headers" : {"Authorization" : "Bearer " + token}
};
UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
}
function setCalendar(title, startDate, description, color) {
startDate = new Date(startDate.replace(/-/g, "/"));//轉換日期格式
//透過ID取得日曆
var cal = CalendarApp.getCalendarById(googleCalendarID);
//沒有結束日期的全天事件
var even = cal.createAllDayEvent(title, startDate, {description:description});
//設定形成顏色
even.setColor(color);
}