Home > database >  Issue in automailing script using Google Apps Script for Spreadsheet
Issue in automailing script using Google Apps Script for Spreadsheet

Time:09-24

I'm working on a Google spreadsheet generated from form's answers and I'm trying to compile automatic mail for form sender, using the script below. However, when I run this, it displayed the error: "TypeError: Cannot read property 'getRange' of null", eventhough I have filled in the form to insert values in the spreadsheet. Can someone help me with this? Thanks a lot!

function emailConfirmation() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('sheet1');

  var recipient = sheet.getRange(sheet.getLastRow(), 2).getValue();
  var subject = '[Khóa học viết luận học bổng] Xác nhận đăng ký tham gia';

  var name = sheet.getRange(sheet.getLastRow(), 4).getValue();
  var resno = sheet.getLastRow()
  if (resno <= 5)
  {
  var body = 'Xin chào'   name   ', \r\n Cảm ơn bạn đã đăng ký tham gia Khóa học viết luận học bổng.'   'Chúc mừng bạn đã trở thành 1 trong 5 người điền đơn đầu tiên.'   'Phương thức thanh toán cho khóa học là chuyển khoản.';
 
  }
  else
  {
    var body = 'Xin chào'   name   ', \r\n Cảm ơn bạn đã đăng ký tham gia Khóa học viết luận học bổng.'   'Phương thức thanh toán cho khóa học là chuyển khoản.';
  }

  MailApp.sendEmail(recipient, subject, body);
}

CodePudding user response:

It is as what @Sergey and Cooper suspected. You are using an incorrect sheet name in your enter image description here

  • Sheet names are the tab at the bottom part of your Google Sheet file (sample are those encircled with green)

What you used earlier is the Spreadsheet name Registration Form (Responses).


What @Sargey means is that, since you provided a sheet name that is not existing in your Google Sheet file when you used ss.getSheetByName('sheet 1'); or ss.getSheetByName('Registration Form (Responses)');, getSheetByName(name) will return null since there is no sheet with the given name.

The value null represents the intentional absence of any object value.

  • Related