0

I am getting this error while trying to run the email sending script in Google App script. I don't have any knowledge of Javascript(I plan to learn after learning Python) and I got this code from samples code provided by Google App script.

The number of rows and columns in Google spreadsheet: https://prnt.sc/tha2o1

Below is the code.

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 6; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 7);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[3]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Sending emails from a Spreadsheet';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
5
  • 2
    Which type are you using the container-bound script type or the standalone script type? Commented Jul 14, 2020 at 3:03
  • @Tanaike standalone script Commented Jul 14, 2020 at 3:09
  • 1
    Thank you for replying. I think that the reason of your issue is that. By this, I think that sheet is null, and such error occurs. Please use the container-bound script of the Spreadsheet and test it again. Commented Jul 14, 2020 at 3:10
  • Related stackoverflow.com/q/50105012/1595451 Commented Jul 14, 2020 at 4:37
  • 1
    Does this answer your question? TypeError: Cannot call method "getActiveSheet" of null. at myFunction(Code:6) Commented Jul 14, 2020 at 5:00

1 Answer 1

2

I was running a standalone script and it didn't work. You are supposed to run container-bound script if you wish to work with Google Sheets/Docs/Forms/Sites. Here is how you can run the container bound script. https://developers.google.com/apps-script/guides/bound

Sign up to request clarification or add additional context in comments.

2 Comments

If you followed the tutorial exactly, on step 3 it says open the script editor from the google sheets. That should have created a container-bound script.
I found it later when @Tanaike inquired about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.