Last active
June 17, 2022 07:03
-
-
Save robinli/e29fe5694123037e2416 to your computer and use it in GitHub Desktop.
Google Form with email notification
This file contains 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
/** | |
*6 表單送出後發送Email | |
* 參考來源 | |
* http://gappsnews.blogspot.tw/2013/02/google-apps-scriptgoogle-form.html | |
*/ | |
function onCommit(){ | |
var today = new Date(); | |
var subject = "新增:電腦維修單-" + today.getMonth() + "/" + today.getDate(); | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var rows = sheet.getDataRange(); | |
var numRows = rows.getNumRows(); | |
var values = rows.getValues(); | |
var content = getLastRowTable(values); | |
var htmlBody = "Hi Peter: <br/><br/> 提醒新增一筆:電腦維修單! <br/><br/>" + content + '<br/><br/>Send by Google Apps'; | |
GmailApp.sendEmail( | |
"peterhsiao@systemlead.com,renee@systemlead.com", | |
subject, | |
htmlBody , | |
{from: 'robin@systemlead.com', htmlBody:htmlBody} | |
); | |
} | |
function getLastRowTable(arr){ | |
/*var newArr = new Array(); | |
newArr.push(arr[0]); | |
newArr.push(arr[arr.length-1]); | |
return jsonArrToTable(newArr); | |
*/ | |
return jsonObjToTableWithTitle(arr[0], arr[arr.length-1]); | |
} | |
function jsonArrToTable(arr){ | |
var TRs = ''; | |
for(var i = 0 ; i< arr.length ; i++){ | |
var row = arr[i]; | |
var TR = '<tr>'; | |
var keys = Object.keys(row); | |
for(var j = 0 ; j < keys.length ; j++) { | |
var rowvalue = row[keys[j]]; | |
TR += ('<td>' + rowvalue + '</td>' ); | |
} | |
TR+= '</tr>'; | |
TRs += TR; | |
} | |
var table = '<table>' + TRs +'</table>'; | |
return table; | |
} | |
function jsonObjToTableWithTitle(title, row){ | |
var TRs = ''; | |
var keys = Object.keys(row); | |
for(var j = 0 ; j < keys.length ; j++) { | |
var TR = '<tr>'; | |
var rowvalue = row[keys[j]]; | |
TR += ('<td>' + title[keys[j]] + '</td><td><b>' + rowvalue + '</b></td>' ); | |
TR+= '</tr>'; | |
TRs += TR; | |
} | |
var table = '<table>' + TRs +'</table>'; | |
return table; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment