The essence of the script in sending a message to the mail, the address of which is taken from the Google table, you run, gives an error: TypeError: Не удается вызвать метод "getRange" объекта null .

Why is the null object passed to the getRange method?

 function sendMails() { // число адресатов var rowsAmount = 3; // Здесь надо указать тему писем var messageSubject = "Пресс-релиз"; // Создаем переменную для активного листа var currentSheet = SpreadsheetApp.getActiveSheet(); // Выделяем диапазон данных и получаем из него данные var dataRange = currentSheet.getRange(1, 1, rowsAmount, 2); var data = dataRange.getValues(); // Обходим поочередно ряды в таблице for (i in data) { var row = data[i]; // Из первого столбца берем адрес var emailAddress = row[0]; // А из второго имя получателя var messageText = row[1] + ", " + "высылаем Вам пресс-релиз"; MailApp.sendEmail(emailAddress, messageSubject, messageText); } } 
  • one
    No, it means that SpreadsheetApp.getActiveSheet () returned null - Grundy
  • Not this way. The null object is called .getRange() . Run console.log(currentSheet); - look what will be there. I assume that there will be null . - YozhEzhi
  • @YozhEzhi, Logger.log() ? And @grundy wrote it right. - oshliaer
  • @ iandryax5, you did not say in which context the script runs. Is it Standalone or Bound ? - oshliaer

2 answers 2

Method

 SpreadsheetApp.getActiveSheet(); 

will return the sheet only when you call the function from the document, sidebar, or from the created menu in the document ( https://developers.google.com/apps-script/guides/menus ).

If you want to test your function from the code editor, you need to open the document by id or link

 var ss = SpreadsheetApp.openById(id); var ss = SpreadsheetApp.openByUrl(url); 

and then select the sheet

 var currentSheet = ss.getSheets()[0]; var currentSheet = ss.getSheetByName(name); 

    Most likely the problem is in context.

    Either the script runs outside the table, i.e. This is a standalone script, or it is attached to the Document.

    Need to rewrite

     var currentSheet = SpreadsheetApp.getActiveSheet(); 

    on something like that

     var currentSheet = SpreadsheetApp.openById(id).getSheetByName(name); 

    The code from the topic is fully working. Example TypeError: Cannot call the “getRange” method of object null [634069] #ruSO Menu - Trial - sendMails