Google AppScript syntax error for sendemail. Can't ID my problem

好久不见. 提交于 2021-02-05 09:27:29

问题


I am trying to get Google sheets to send personalized emails from long list on another sheet in the workbook. I used a tutorial (as I am a certified novice at all coding languages) but the AppScript is telling me that line 4 has a syntax error. I cannot for the life of me figure out what I've done wrong, but I'm sure that its blatantly obvious when explained by someone with these legitimate skills. Here is the script:

function sendEmail() {
      
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var sheet1=ss.getSheetByName(‘Sheet1’);
    var sheet2=ss.getSheetByName(‘Sheet2’);
    var subject = sheet2.getRange(2,1).getValue();
    var n=sheet1.getLastRow();
    for (var i = 2; i < n+1 ; i++ ) {
    var emailAddress = sheet1.getRange(i,2).getValue();
    var name=sheet1.getRange(i,1).getValue();
    var message = sheet2.getRange(2,2).getValue();
      
    message=message.replace(“<name>”,name);
    MailApp.sendEmail(emailAddress, subject, message);
    }

    }

Anyone that could help me get this running would be a true life saver!


回答1:


The code in the question has , and , (opening/closing curly single and double quotes) instead of ' and " (straight single and double quote). Replace the first by the corresponding straight quote.

I have seen that this problem usually happens to people new to Google Apps Script that find a "nice" piece of code on a website that isn't really friendly for publishing code. It's very likely the website is using something that replaces straight quotes by the also called typographic quotes.

Related

  • Notify via email when a cell is updated in google spreadsheet
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Illegal_character



回答2:


Try to change the following code, might be you are using the wrong apostrophe and you forget to put ; in second line

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName('Sheet2');



回答3:


function sendEmail() {
  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var sheet1=ss.getSheetByName('Sheet1');
  var sheet1sr=2;//data start row
  var sheet2=ss.getSheetByName('Sheet2');
  var subject=sheet2.getRange(2,1).getValue();
  var msg=sheet2.getRange(2,2).getValue();
  var vA=sheet1.getRange(sheet1sr,1,sheet1.getLastRow()-sheet1sr+1,2).getValues();
  for (var i=0;i<vA.length;i++) {
    let message=msg;
    message=message.replace("<name>",vA[i][1]);
    if(MailApp.getRemainingDailyQuota()>0) {
      MailApp.sendEmail(vA[i][0], subject, message);
    }else{
      SpreadsheetApp.getUi().alert('Remaining Daily Quota is exhausted.')
    }
  }
}

You were using incorrect single and double quotes also



来源:https://stackoverflow.com/questions/62969726/google-appscript-syntax-error-for-sendemail-cant-id-my-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!