Search code examples
google-apps-scriptgmail

How do I call the function in server side script : Google App Script when I hit a hyperlink in Gmail body (client side)?


  1. I send an approval request in Gmail by sending hyperlink as HTML Body type. - Successful
  2. Instead of calling a page, I referred the link to call a function inside server side script. -Successful
  3. 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.

  4. 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.


Solution

    • Publish your web app:

      • Run as "me"
      • Access: Anyone incl. Anonymous
    • Use query params to open your web-app

    message = message.replace(
      '##LINK##',
      ScriptApp.getService().getUrl() +
        encodeURI(
          '?func=pressedApprove&trackSheet=' +
            trackSheet.getId() + //use a unique id string of sheet
            '&rowNumber=' +
            rowNumber
        )
    );
    
    • Use doGet() to receive the hyperlink and run the required function:
    function doGet(e){
      var params = e.parameter;
      if(params.func === 'pressedApprove'){
        return pressedApprove(params.trackSheet, params.rowNumber);
      } 
    }