单击按钮时,向所选行第 10 列的地址发送电子邮件

Send an email to the address on the 10th column of the selected row when a button is clicked

我想编写一个 "Send Email" 按钮,当我 select 一行并单击该按钮时,系统将从该行的 J 列检索电子邮件地址并发送一个固定的向该电子邮件地址发送消息。

但是,我遇到了错误 "Invalid email: Undefined",这可能意味着我检索单元格的代码不正确。

我哪里错了?

这是按钮的代码。

    function sendEmail() {
  // define a couple constants for the function
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const ui = SpreadsheetApp.getUi();

  // if valid, get the correct row of data.
    var row = sheet.getActiveCell().getRow();

  // get the range for that row
    var rowRange = sheet.getRange(row, 1, 1, 10).getValues();

    // Make sure the selected row has text
  if(sheet.getRange(row,9).isBlank()) {
    ui.alert("Please assign a teacher for this AOR.");
    return;
  }

  // Build the object to use in MailApp
    var address = rowRange[0][10];
    var AORno = rowRange[0][4];
    var body = "Dear Teacher, Your AOR number " + AORno + "has been approved. You may now proceed with claiming your expenses under this AOR."
    var msg = "Email has been sent to " + address + "."
    var subj = "[Automatic notification] Your AOR has been approved."

  // Send the email
    ui.alert(msg, ui.ButtonSet.OK)
    MailApp.sendEmail(address, subj, body);

    // set the send status in col 4
    sheet.getRange(row, 11).setValue("Sent");
}

电子表格的link是here.

请参阅 AOR 标签。

根据您的脚本和共享电子表格,我认为数组索引可能存在问题。电子表格的range索引是从1开始的,而array的索引是从0开始的。那么修改如下呢?

发件人:

var address = rowRange[0][10];
var AORno = rowRange[0][4];

收件人:

var address = rowRange[0][9];
var AORno = rowRange[0][3];

如果这不起作用,请告诉我。我要修改。