Last active
April 17, 2016 13:26
-
-
Save redbug312/15cac266f969aa9630012a3de3981b5e to your computer and use it in GitHub Desktop.
[Google App Scripts] send emails when the sheet received a submitting from the form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developers.google.com/apps-script/reference/properties/properties#deletepropertykey | |
var colMap = PropertiesService.getScriptProperties(); | |
colMap.setProperties({ | |
"時間戳記": 0, | |
"蘆薈洗面乳": 4, | |
"蘆薈水晶凝露": 5, | |
"原味魷魚絲": 7, | |
"水晶魚": 8, | |
"紅豆糕": 9, | |
"泉利鹹餅": 11, | |
"紫菜酥": 12, | |
"正ㄧ花生酥": 13, | |
"萬泰海苔酥": 14, | |
"紅豆冰心糕": 15, | |
"杏仁小魚": 17, | |
"干貝醬": 18, | |
"小管醬": 19, | |
"化石餅乾": 20, | |
"仙人掌法式軟糖": 21, | |
"黑糖糕": 25, | |
"仙人掌冰心糕": 26, | |
"姓名": 28, | |
"系級": 29, | |
"信箱": 30, | |
"手機": 31, | |
"領貨時間": 33, | |
"大立花枝丸": 34, | |
"干貝醬備註": 35, | |
"繳費時間": 36, | |
"建議": 37 | |
}); | |
var goodsOrder = [25, 26, 34, 4, 5, 7, 8, 9, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21]; | |
// return the value in the column | |
function COL(record, colname) | |
{ | |
var columnNum = parseInt(colMap.getProperty(colname),10); // vaule of colMap | |
grid = record[columnNum]; | |
return(grid == '') ?0 :grid; | |
} | |
function getColName(ColNum) | |
{ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheets()[0]; | |
var targetData = sheet.getSheetValues(1, ColNum+1, 1, 1); | |
return targetData[0][0]; | |
} | |
function getLastRow() | |
{ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheets()[0]; | |
var lastRowNum = sheet.getLastRow(); | |
var lastColNum = sheet.getLastColumn(); | |
var targetData = sheet.getSheetValues(lastRowNum, 1, 1, lastColNum); | |
return targetData[0]; | |
} | |
// return main content of the mail | |
function getMailContent(data) | |
{ | |
var HTMLcontent = | |
"<div class=\"body\" style=\"font-family: 'Microsoft YaHei'; color: #333; background: #EEE;\">"; | |
HTMLcontent += | |
"<div class=\"section\" style=\"padding: 10px 15px;\">" + | |
"<p><b>" + COL(data,"姓名") + "</b>同學你好:</p>" + | |
"<p>澎友週預購表單已經<b>送出成功</b>!<br />" + | |
"您所購買的商品如下:</p><ul style=\"color: #000; background: #FFF; margin: 10px; padding: 20px 30px;\">"; | |
var sum = 0; | |
for(i in goodsOrder){ | |
order = goodsOrder[i]; | |
if(data[order] != 0){ | |
sum += data[order]; | |
HTMLcontent += '<li>' + getColName(order)+ ' <b>' + data[order] + '</b> 份'; | |
if(order == 18) HTMLcontent += ':' + COL(data,"干貝醬備註"); | |
HTMLcontent += '</li>'; | |
} | |
} | |
if(sum == 0) HTMLcontent += "唉呀!您似乎未填購任何商品,煩請重新填寫 :)</ul></p></div>"; | |
else{ | |
HTMLcontent += "</ul>"; | |
if(COL(data,"建議") != 0) HTMLcontent += "相當感謝您額外填寫意見回饋,若有任何問題請私訊粉專;<br />並"; | |
HTMLcontent += "請待之後繳錢/領貨通知信件,澎友週感謝您的支持!</p>"; | |
if(sum > 3) HTMLcontent += "<p style=\"font-size: 1.5em; color: #F28C46\">你,準備好一起享受這場盛宴了嗎?</p></div>"; | |
else HTMLcontent += "<p style=\"font-size: 1.5em; color: #5D9CEC\">你,準備好一起享受這場盛宴了嗎?</p></div>"; | |
} | |
HTMLcontent += | |
"<div class=\"footer\" style=\"text-align: right; background: #FFF; margin: 0; padding: 2px 15px;\" align=\"right\" link=\"#FFF\" alink=\"#FFF\" vlink=\"#FFF\">" + | |
"<p>—<a href=\"https://www.facebook.com/2016PHriendweek/\">2016台大澎友週:澎魚宴來了</a>,於 " + COL(data,"時間戳記") + " 寄送</p>" + | |
"</div></div>"; | |
return HTMLcontent; | |
} | |
// execute when receiving results from form | |
function main() | |
{ | |
var data = getLastRow(); | |
var HTMLcontent = getMailContent(data); | |
MailApp.sendEmail({ | |
to: COL(data,"信箱"), | |
subject: "2016澎友週預購確認信", | |
htmlBody: HTMLcontent | |
}); | |
MailApp.sendEmail({ | |
to: "[email protected]", | |
subject: "2016澎友週預購確認信-官方信箱副本", | |
htmlBody: HTMLcontent | |
}); | |
} | |
function DEBUG() | |
{ | |
var data = getLastRow(); | |
var HTMLcontent = getMailContent(data); | |
Logger.log(HTMLcontent); | |
} |
BUG:
There would be empty rows appearing from nowhere in the form. I can't find what causes that bug, from the record of form to the history of the sheet; I can't reproduce the situation to trigger this bug, neither. this bug happened 8 times in 100 valid submitting in average. If you know any information about bug, please tell me that, thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
used for the activity, 2016台大澎友週:「澎魚宴來了!!!」