Last active
April 2, 2023 11:23
-
-
Save imantung/2d3da43494a357d573edcb23f0fc9955 to your computer and use it in GitHub Desktop.
Example to enable generate unique ticket number and send notification for google form helpdesk
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
// Read more at: https://medium.com/@imantung/simple-helpdesk-using-google-forms-and-apps-script-ee3c755bb2b7 | |
const TOAST_TIMEOUT = 3; // toast show for 3 sec | |
const FORM_SUBMIT_SHEETNAME = "Form Responses 1" | |
const TICKET_NO_LENGTH = 5; | |
const TICKET_NO_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
const SUPPORT_EMAIL = "[email protected]" | |
const SUPPORT_NAME = "Your Humble Support" | |
function onFormSubmit(e) | |
{ | |
let ticketNo = generateTicketNo(); | |
let email = e.namedValues["Email Address"].toString(); | |
sendNofication({ | |
ticketNo: ticketNo, | |
recipient: email, | |
subject: "You have create incident report #" + ticketNo, | |
message: "Thank you for reporting the incident. Our support team will take a look at this soon and come back to you soon.", | |
incident: { | |
email: email, | |
name: e.namedValues["Name"].toString(), | |
summary: e.namedValues["Summary"].toString(), | |
description: e.namedValues["Description"].toString(), | |
bookingCode: e.namedValues["Booking Code"].toString(), | |
custCode: e.namedValues["Customer Code"].toString() | |
}, | |
}) | |
// set ticketNo to column 2 (column B) | |
let sh = e.range.getSheet(); | |
sh.getRange(sh.getLastRow(),2,1).setValues([[ticketNo]]); | |
} | |
function onEdit(e) | |
{ | |
let activeSheet = e.source.getActiveSheet(); | |
if (activeSheet.getName() === FORM_SUBMIT_SHEETNAME){ | |
let col = e.range.columnStart; | |
let row = e.range.rowStart; | |
// get raw data from column A to K (11 column) | |
let data = activeSheet.getRange(row,1,1,11).getValues() | |
let curr = data[0][col-1].toString(); | |
let ticketNo = data[0][1].toString(); // column B | |
let custEmail = data[0][2].toString(); // column C | |
let incident = { | |
email: custEmail, | |
name: data[0][3], // column D | |
summary: data[0][4], // column E | |
description: data[0][5], // column F | |
bookingCode: data[0][6], // column G | |
custCode: data[0][7] // column H | |
} | |
switch(col){ | |
case 9: // edit triggered on column I: Assignee | |
sendNofication({ | |
ticketNo: ticketNo, | |
recipient: curr, | |
subject: "You have assign to Incident #" + ticketNo, | |
message: "You have assign to Incident #" + ticketNo, | |
incident: incident, | |
}) | |
info("Incident #" + ticketNo + ": Notify assignee " + curr) | |
break; | |
case 10: // edit triggered on column J: Status | |
let status = curr; | |
let remarkToCust = data[0][10]; | |
let message = "Your Incident Report (#" + ticketNo + ") have been update to " + status + "."; | |
if (remarkToCust !== ""){ | |
message = message + " Remark: " + remarkToCust; | |
} | |
sendNofication({ | |
ticketNo: ticketNo, | |
recipient: custEmail, | |
subject: "Status Update on Incident #" + ticketNo + ": " + status, | |
message: message, | |
incident: incident, | |
}) | |
info("Incident #" + ticketNo + ": Notify reporter " + custEmail) | |
break; | |
} | |
} | |
} | |
function sendNofication(p){ | |
// Learn more: https://spreadsheet.dev/send-email-from-google-sheets | |
MailApp.sendEmail({ | |
to: p.recipient, | |
subject: p.subject, | |
htmlBody: getHTMLBody(p), | |
replyTo: SUPPORT_EMAIL, // set reply to support email instead dev email | |
name: SUPPORT_NAME // set sender name to support name | |
}) | |
} | |
function getHTMLBody(p){ | |
// Consider to use html template for better style | |
// Learn more: https://spreadsheet.dev/send-html-email-from-google-sheets | |
return p.message + "<br \><br \>" | |
+ "Incident Details<br \>" | |
+ "<table border=1>" | |
+ "<tr>" + "<td>Customer Email</td><td>" + p.incident.email +"</td></tr>" | |
+ "<tr>" + "<td>Customer Name</td><td>" + p.incident.name +"</td></tr>" | |
+ "<tr>" + "<td>Incident Summary</td><td>" + p.incident.summary +"</td></tr>" | |
+ "<tr>" + "<td>Incident Description</td><td>" + p.incident.description +"</td></tr>" | |
+ "<tr>" + "<td>Booking Code</td><td>" + p.incident.bookingCode +"</td></tr>" | |
+ "<tr>" + "<td>Customer Code</td><td>" + p.incident.custCode +"</td></tr>" | |
+ "</table>" | |
+ "<br \><br \>" | |
+ "Regards,<br \>" | |
+ SUPPORT_NAME; | |
} | |
function generateTicketNo () { | |
// OG: https://gist.github.com/clayperez/0d689b02693b2e94a7d1ddea98a0571c | |
var rtn = ''; | |
for (var i = 0; i < TICKET_NO_LENGTH; i++) { | |
rtn += TICKET_NO_CHARSET.charAt(Math.floor(Math.random() * TICKET_NO_CHARSET.length)); | |
} | |
return rtn; | |
} | |
function info(msg){ | |
// Learn more: https://spreadsheet.dev/toast-notifications-in-google-sheets | |
SpreadsheetApp.getActiveSpreadsheet().toast(msg, "ℹ️ Infomation",TOAST_TIMEOUT); | |
} | |
function warn(msg){ | |
SpreadsheetApp.getActiveSpreadsheet().toast(msg, "⚠️ Warning",TOAST_TIMEOUT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment