当我在 Gmail 正文(客户端)中点击超链接时,如何在服务器端脚本中调用该函数:Google App Script? [英] How do I call the function in server side script : Google App Script when I hit a hyperlink in Gmail body (client side)?

查看:8
本文介绍了当我在 Gmail 正文(客户端)中点击超链接时,如何在服务器端脚本中调用该函数:Google App Script?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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.

解决方案

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

这篇关于当我在 Gmail 正文(客户端)中点击超链接时,如何在服务器端脚本中调用该函数:Google App Script?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆