将本地GmailApp.sendEmail(GAS)转换为Web App脚本

我设置了一个有效的Google Apps脚本,以在单击工作簿中的按钮时发送自动电子邮件(GmailApp.sendEmail)。

但是,我试图将脚本转换为Google Web App,以便组织内的任何用户都有权运行脚本并通过按按钮触发自动电子邮件。

我对如何适应getui()中的本地代码有些迷惑。我知道我需要添加诸如doget(e)之类的功能并将其部署为Web应用程序,但是我对Web App的了解不足以编辑代码。

这是我工作的本地代码:

// This constant is written in column E for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'E-MAIL SENT';
var ui = Spreadsheetapp.getUi();

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function email(){
  var rng = Spreadsheetapp.getactiveSheet().getRange('A2:F2')
  var checkvalue = Spreadsheetapp.getactiveSheet().getRange('E2').getvalue();
  var email = rng.getvalues()[0];
  var data = rng.getvalues();
  for (var i = 0; i < data.length; ++i) {
  var row = data[i];
  var emailSent = checkvalue; // emailSent confirmation cell
  if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
  GmailApp.sendEmail(email[0],email[1],email[2]);
  Spreadsheetapp.getactiveSheet().getRange('E2').setvalue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      Spreadsheetapp.flush();
  }
 }
}

非常感谢您的帮助!

xihaibo 回答:将本地GmailApp.sendEmail(GAS)转换为Web App脚本

尝试一下:

Code.gs:

function email(){
  var ss=SpreadsheetApp.openById("SpreadsheetId");//need spreadsheet id
  var sh=ss.getSheetByName("SheetName");//when you open up a spreadsheet like this the active sheet is alway ss.getSheets()[0] the left most sheet so you should user get sheet by name instead.
  var rg=sh.getRange('A2:F2');
  var email=rg.getValues()[0];
  if (email[4]!="EMAIL_SENT") { 
  GmailApp.sendEmail(email[0],email[1],email[2]);
  sh.getRange('E2').setValue("EMAIL_SENT");
  }
}

function doGet() {
  return HtmlService.createHtmlOutputFromFile('html filename without extension');
}

html:

<!DOCTYPE html>
<html>
  <head>
  <base target="_top">
  </head>
<body>
  <input type="button" value="Send" onClick="google.script.run.email();" />
</body>
</html>

Client to Server Communication

本文链接:https://www.f2er.com/3071794.html

大家都在问