Using script editor to send an email from button in Google Sheets

时间:2019-04-08 13:59:09

标签: google-apps-script google-sheets

I am trying to make the button that I have on a sheet send an email using scripts but, I have not been able to get it to send the correct sheet or it sends a blank email.

I have tried a few different version of the Script but still can't get it to do anything I need it to.

I have 3 total sheets Sheet1 collects data for Sheet2. Sheet2 contains the Data I need to email and the button with the script to send the email. The third sheet is UserData which contains a list of emails that the data needs to be sent to.

...
function CustomEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getLastRow();
var UserData = range.getValues();
for (i in UserData) {
var row = UserData[i];
var email = row[1];
MailApp.sendEmail(row[1], "Shift Report", "Have a great Shift");
}
}
...

I hope to be able to input my data into Sheet1 and then review sheet two and hit the sendEmail button.

1 个答案:

答案 0 :(得分:0)

 function CustomEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getLastRow();

getLastRow()返回一个整数,而不是范围。

getLastRow

  var UserData = range.getValues();

由于范围实际上不是范围,因此上述行将不会产生任何值。

  for (i in UserData) {

这种过程应该保留给具有键/值对的对象。并且应该始终使用var声明变量。

    var row = UserData[i];
    var email = row[1];
    MailApp.sendEmail(row[1], "Shift Report", "Have a great Shift");
  }
}