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

时间:2017-12-14 01:01:39

标签: google-apps-script google-sheets google-spreadsheet-api

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

但是,我遇到错误"无效的电子邮件:未定义",这可能意味着我检索单元格的代码不正确。

我哪里出错了?

这是按钮的代码。

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

电子表格的链接为here.

请参阅AOR标签。

1 个答案:

答案 0 :(得分:0)

从您的脚本和共享电子表格中,我认为数组索引可能存在问题。电子表格的范围索引从1开始。但是数组的范围从0开始。那么如何修改如下?

来自:

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

致:

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

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