Google 应用程序脚本:在 google sheet 到达最后一行后调用另一个函数

Google app script : call another function after reached last row on google sheet

可能我不知道如何在到达 google sheet 上注册码列的最后一行时调用另一个函数,用户单击 google 表单上的提交将自动发送电子邮件出另一个 sendMail 函数 说例如 'registration had been closed, thank you.'

我的代码:

function sendmail() {

  var ss = SpreadsheetApp.getActive();
  var sh1 = ss.getSheetByName("Sheet1");
  var EMAIL_SENT = "EMAIL_SENT";
  
  var lastRow = sh1.getLastRow();
  
  var email = sh1.getRange(lastRow, 2).getValue();
  var name = sh1.getRange(lastRow, 1).getValue();
  var registercode = sh1.getRange(lastRow, 3).getValue();
  
  subjecttxt = "Thank you for registered with us"
  email_Body = "Dear " + name + "," + "<br>" +
              "your registeration is successfully, your registered code is " + registercode + 
              "Thank you for your registeration"
  
              MailApp.sendEmail({
                to:email,
                subject: subjecttxt,
                htmlBody: email_Body,
              }) ;           
  sh1.getRange(lastRow, 4).setValue(EMAIL_SENT);
}

我相信你的目标如下。

  • 在您的情况下,值被放入列“A”和“B”。那时候你的函数sendmail就是运行。此时,当值被放入“A”和“B”列时,当该行超过“C”列的最后一行时,您想发送registration had been closed, thank you.的电子邮件正文。

如果我的理解是正确的,下面的修改怎么样?

修改后的脚本:

function sendmail() {
  // This is from 
  Object.prototype.get1stEmptyRowFromTop = function (columnNumber, offsetRow = 1) {
    const range = this.getRange(offsetRow, columnNumber, 2);
    const values = range.getDisplayValues();
    if (values[0][0] && values[1][0]) {
      return range.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
    } else if (values[0][0] && !values[1][0]) {
      return offsetRow + 1;
    }
    return offsetRow;
  };

  var ss = SpreadsheetApp.getActive();
  var sh1 = ss.getSheetByName("Sheet1");
  var EMAIL_SENT = "EMAIL_SENT";
  var lastRow = sh1.get1stEmptyRowFromTop(1) - 1;
  var email = sh1.getRange(lastRow, 2).getValue();
  var name = sh1.getRange(lastRow, 1).getValue();
  var registercode = sh1.getRange(lastRow, 3).getValue();
  var subjecttxt = "Thank you for registered with us";
  var email_Body;
  if (lastRow < sh1.get1stEmptyRowFromTop(3)) {
    email_Body = "Dear " + name + "," + "<br>" + "your registeration is successfully, your registered code is " + registercode + "Thank you for your registeration";
  } else {
    email_Body = "registration had been closed, thank you.";
  }
  MailApp.sendEmail({
    to: email,
    subject: subjecttxt,
    htmlBody: email_Body,
  });
  sh1.getRange(lastRow, 4).setValue(EMAIL_SENT);
}
  • 关于if (lastRow < sh1.get1stEmptyRowFromTop(3)) {,,,},在这个脚本中,当“A”列和“B”列的值被放到“C”列最后一行的下一行时, email_Body = "registration had been closed, thank you." 被使用。当你想在“C”列的最后一行使用这个email_Body时,请将(lastRow < sh1.get1stEmptyRowFromTop(3))修改为(lastRow < sh1.get1stEmptyRowFromTop(3) - 1)

  • 当你想将"registration had been closed, thank you."添加到email_Body时,当值被放到“C”列最后一行的同一行时,请修改以上if语句如下

      if (lastRow < sh1.get1stEmptyRowFromTop(3) - 1) {
        email_Body = "Dear " + name + "," + "<br>" + "your registeration is successfully, your registered code is " + registercode + "Thank you for your registeration";
      } else {
        email_Body += "registration had been closed, thank you.";
      }