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

我怕爱的太早我们不能终老 提交于 2019-12-11 19:27:17

问题


  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.


回答1:


  • 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);
  } 
}


来源:https://stackoverflow.com/questions/55950204/how-do-i-call-the-function-in-server-side-script-google-app-script-when-i-hit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!