- 請開啟「收集電子郵件地址」
function getFormResponse() {
//表單名稱
var FORMNAME = "Google表單名稱";
//取得表單
var formHandle = DriveApp.getFilesByName(FORMNAME).next();
//由表單 Id 開啟表單
var form = FormApp.openById(formHandle.getId());
//取得表單名稱
var formTitle = form.getTitle();
//取得表單回覆內容
var formResponse = form.getResponses();
//處理表單上問答內容
for(var i = 0; i < formResponse.length ; i++){
//取得表單上的項目
var itemResponses = formResponse[i].getItemResponses();
//取得表單上「收集電子郵件地址」
var respondentEmail = formResponse[i].getRespondentEmail();
//Logger.log(respondentEmail);
var itemContext = "Email : " + respondentEmail + "\n";
//將表單每一項問答組成訊息
for(var j = 0; j < itemResponses.length ; j++)
{
//取得問題標題
itemContext += itemResponses[j].getItem().getTitle();
//取得問題回覆內容
itemContext += " : " + itemResponses[j].getResponse() + "\n";
}
//取得填寫表單時間
itemContext += "\n填寫時間 : " + formResponse[i].getTimestamp() + "\n\n\n";
//送出表單到Line
sendToLine(itemContext);
//寄信給填報者
if(respondentEmail){
MailApp.sendEmail(respondentEmail,FORMNAME,itemContext);
}
}
//刪除回應問題
form.deleteAllResponses();
}
function sendToLine(message){
var token = "你的Line Notify token";
var options =
{
method : "post",
payload : "message=" + message,
headers : {"Authorization" : "Bearer "+ token},
muteHttpExceptions : true
};
UrlFetchApp.fetch("https://notify-api.line.me/api/notify",options);
}