Erik Stenerud
Erik Stenerud

Reputation: 35

Why doesn't my send email from google sheets script work?

function sendEmail(){
  var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet 1").getRange("A1");
  var emailAddress = emailRange.getValues();
  var message = 'Test message';
  var subject = 'Test subject';
  MailApp.sendEmail(emailAddress, subject, message);
}

When I try to run it, I get "Exception: The parameters (number[],String,String) don't match the method signature for MailApp.sendEmail. (line 6, file "Send_Email")"

When I copy and paste the email address from Sheet 1, A1 into the spot occupied by the variable "emailAddress", the script works.

Upvotes: 0

Views: 157

Answers (1)

Marios
Marios

Reputation: 27348

Explanation:

Assuming that your sheet name is "Sheet 1" and not "Sheet1", the mistake you made is located here:

emailRange.getValues();

You should use instead:

emailRange.getValue();

Solution:

function sendEmail(){
  var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet 1").getRange("A1");
  var emailAddress = emailRange.getValue();
  var message = 'Test message';
  var subject = 'Test subject';
  MailApp.sendEmail(emailAddress, subject, message);
}

Related:

What does the range method getValues() return and setValues() accept?

Upvotes: 2

Related Questions