- I send an approval request in Gmail by sending hyperlink as HTML Body type. - Successful
- Instead of calling a page, I referred the link to call a function inside server side script. -Successful
It calls the function automatically as soon as the email reaches the recipient. - Failure. Supposedly when the recipient clicks the Approve hyperlink, then only the function supposed to triggered.
I created a index.html, used HTMLService.createoutputfromfile and other HTMLService features, it failed. I do not know how do I establish a communication from Client Side to Server Side script; from Gmail to Google App Script.
Google App Script
function sendEmailToApproverOne(emailAdd, action, trackSheet,rowNumber){
var form_Name = FormApp.getActiveForm().getTitle();//Get the Name of this specific Form
var btnAction = "";//---gets the button name(Approve/Acknowledge) when the recipient receives the email ----
if(action == "Approval"){
btnAction = "Approve";
}
else if(action == "Acknowledgement"){
btnAction = "Acknowledge";
}
var message = getMessage(btnAction,trackSheet,rowNumber);//----calls a function to create the email body----
GmailApp.sendEmail(emailAdd, form_Name +': For your perusal to '+ btnAction , '', {htmlBody:message});
}
function getMessage(buttonLabel,trackSheet,rowNumber) {
var htmlOutput = HtmlService.createHtmlOutputFromFile('emailBody');
var message = htmlOutput.getContent()
message = message.replace("##LINK##", pressedApprove(trackSheet,rowNumber));
message = message.replace('##BUTTONLABEL##',buttonLabel);
return message;
}
//This function: pressedApprove() is triggered when the Approver presses approve in Email button
function pressedApprove(trackSheet,rowNumber){
Logger.log("This button is clicked");
//====some code to do with tracksheet and rownumber.
}
<a href="##LINK##" id="myLink" >##BUTTONLABEL##</a>
Expected to see the function: pressedApprove() only triggered when the hyperlink is clicked inside the Gmail. Not expecting to auto calls every time when I run the code to send Gmail.
How do I set the restriction that the function only triggered when the hyperlink pressed in Gmail body.