简体   繁体   中英

GAS: Error on SpreadSheet for mailing

Now I'm trying to send email by using lists on the spreadsheet with Google Apps Script.

I coded (well, just copy and paste sample program on the book actually) like following, but an error occurred that is "TypeError: Cannot call method "getLastRow" of null."

  function sendMail() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var mailSheet = mySS.getSheetByName("メール");  
  var addSheet = mySS.getSheetByName("住所録");
  var endRow = addSheet.getLastRow();
  var mySubject = mailSheet.getRange("C2").getValue();

  for (i = 2; i <= endRow; i++ ) {
    if (addSheet.getRange(i, 3).getValue()) {
      var myBody = addSheet.getRange(i, 1)
         .getValue() + "様¥n¥n";
      myBody = myBody + mailSheet.getRange("C3").getValue();
      var mailTo = addSheet.getRange(i, 2).getValue();
      GmailApp.sendEmail(mailTo, mySubject, myBody);
    }
  }
  Browser.msgBox("メール送信しました"); 
 }

sorry for some include Japanese, but I created 2 sheets named "メール" and "住所録” then input data.

Could someone help me to fix this?

You spreadsheet does not have a sheet named "住所録".

The method getSheetByName does not throw an error if the sheet isn't found, it just returns null

I think the problem is the fact that your sheet names are in Japanese. The encoding used by the script in Google's servers will be different. Therefore, it will not return any sheet. Rename your sheets to English and it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM