1
votes

New to Google Scripts so I'm bumping my head on this entry level code. I want to send an email based on a specific sheet in a spreadsheet. The tab or sheet is named 'Test 2'. Currently this is my code.

/* Sends emails with data from the current spreadsheet.*/
    function sendEmails() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var startRow = 2; // First row of data to process
      var numRows = 2; // Number of rows to process
      var dataRange = getRange(startRow, 1, numRows, 2); // Fetch the range of cells A2:B6
      var data = dataRange.getValues(); // Fetch values for each row in the Range.
      for (var i in data) {
        var row = data[i];
        var emailAddress = row[0]; // First column
        var message = row[1]; // Second column
        var subject = 'THIS IS A TEST';
        MailApp.sendEmail(emailAddress, subject, message);
      }
    }

And right now it's throwing this error.

ReferenceError: "getRange" is not defined. (line 8, file "Code")

How can I get it to run while referencing that sheet 'Test 2'?

Thanks!

1

1 Answers

0
votes
  • You want to avoid the error of "getRange" is not defined..
  • You want to retrieve the values from the cell "A2:B6" of the sheet Test 2.

If my understanding is correct, how about the following modification?

From:

var dataRange = getRange(startRow, 1, numRows, 2);

To:

var dataRange = sheet.getSheetByName("Test 2").getRange("A2:B6");

Note:

  • In this case, at first, retrieve the sheet object of Test 2 and use the method of getRange for the sheet object.
  • I thought that a1Notation might be suitable for your situation.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.