Search code examples
google-apps-scriptgmail

Script is reading email as a number instead of string


I'm trying to follow this guide (Never programed before):

https://www.groovypost.com/howto/google-sheets-send-email-based-on-cell-value/

But It keeps giving me the error:

Exception: The parameters (number[],String,String) don't match the method signature for MailApp.sendEmail. sendEmail @ Send Email.gs:8

For what I understand this means the email value is taking as a number. But I have no idea how to solve this.

function sendEmail() {
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Client Input").getRange("D2");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'Please go to your Issues Tracker for more information'; // Second column
var subject = ' Notification Action Required';
MailApp.sendEmail(emailAddress, subject, message);  
}

Solution

  • Since emailRange.getValues() returns a 2D array you need to reference the actual string value by its indices, which in this case, [0][0] as the array has only one value.

    var emailAddress = emailRange.getValues()[0][0];
    

    Or if you intend to use one and only one email address, just use:

    var emailAddress = emailRange.getValue();
    

    References:

    Class Range | getValues()

    JavaScript Arrays