-1

I added a few conditional statements in my google apps script and then I got a "left assignment error." I'm not sure I understand what that means or where the error lies in my code.

I'm also wondering if there's another way to save the values, because sometimes I get an error telling me that "get.Value()" function is being used too much?

Any help would be appreciated! Thank you

function SendReceiptSAPI19() {

  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

//Global variables
 var tempItccaM = '1f_dujgqVfYlGFG0OGYBlzoBYpzRMXj7vTsYIVW4i4to';
 var tempItccaF = '1Hk9xgsyO-PYuS-YR0R0rEOfFtZZn3SB3yx_tipEA5ps';
 var tempUispM = '1yKCfIfDdt31rhxFpigT83wrxuPMEwsO-2Xt0dsIFHXo';
 var tempUispF = '1H6kZOT8ZH1ubU8cqHNi1nZjzbw298o1gpu3O_-prl7k'; 
 var tempAdoM = '1AD1aI6UeJJa1GF4tEcXIyx4wJjwU6F1NK54WUrTtarA';
 var tempAdoF = '1p0a2F4EZwYqZAjYk31XWEOwoYygzmOM8KxuNXCZ0ubw'; 
 var docName = 'Ricevuta 2019';

//testing how to make a for loop
  for(var i = 2; i < 3; i++) {
//getting the Name and email and storing it in a variable
  var FName = activeSheet.getRange(i,5).getValue();
  var SName = activeSheet.getRange(i, 4).getValue();
  var Email = activeSheet.getRange(i,16).getValue();
  var CF = activeSheet.getRange(i,12).getValue();
  var Residente = activeSheet.getRange(i,9).getValue();
  var Civ = activeSheet.getRange(i,10).getValue();
  var cap = activeSheet.getRange(i,11).getValue();
  var num = activeSheet.getRange(i,2).getValue();
  var anno = activeSheet.getRange(i,24).getValue();
  var itcca = activeSheet.getRange(i,21).getValue();
  var uisp = activeSheet.getRange(i,19).getValue();
  var ado = activeSheet.getRange(i,22).getValue();
  var Receipt = activeSheet.getRange(i,31).getValue();
  var Pronto = activeSheet.getRange(i,30).getValue();
  var Paid = activeSheet.getRange(i,29).getValue();
  var Sesso = activeSheet.getRange(i,6).getValue();

//make a if-else statement to check for receipt and payment
//FOR MALES: checking for teachers, even those that do not pay uisp but do pay ado
if (Paid==1 && Pronto ==1 && Receipt!=1 && itcca>=30 && ado==30 && Sesso=='M'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempAdoM)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
  copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Uisp@', uisp);
  copyBody.replaceText('@Ado@', ado);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
  var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez' +
       '</body>'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

} //FOR FEMALE TEACHERS
  else if(Paid==1 && Pronto ==1 && Receipt!=1 && itcca>=30 && ado==30 && Sesso=='F'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempAdoF)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
  copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Uisp@', uisp);
  copyBody.replaceText('@Ado@', ado);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
  var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez' +
       '</body>'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

}

//FOR MALE UISP/ITCCA STUDENTS
else if (Paid==1 && Pronto ==1 && Receipt!=1 && itcca>=30 && uisp>=10 && Sesso='M'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempUispM)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
  copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Uisp@', uisp);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
 var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

} 
//FOR FEMALE UISP/ITCCA STUDENTS
 else if (Paid==1 && Pronto ==1 && Receipt!=1 && itcca>=30 && uisp>=10 && Sesso='F'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempUispF)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
  copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Uisp@', uisp);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
 var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

}
//FOR MALE ITCCA ONLY STUDENTS
else if(Paid==1 && Pronto ==1 && Receipt!=1 && itcca==30 && Sesso='M'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempItccaM)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
 var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

}
//FOR FEMALE ITCCA ONLY STUDENTS
else if(Paid==1 && Pronto ==1 && Receipt!=1 && itcca==30 && Sesso='F'){

  //making a copy of the template document
  var copyId   = DriveApp.getFileById(tempItccaF)
  .makeCopy(docName + ' per ' + num)
  .getId();
  var copyDoc  = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  //replace placeholders in the copy document
 copyBody.replaceText('@FName@', FName);
  copyBody.replaceText('@SName@', SName);
  copyBody.replaceText('@CF@', CF);
  copyBody.replaceText('@Residente@', Residente);
  copyBody.replaceText('@Civ@', Civ);
  copyBody.replaceText('@CAP@', cap);
  copyBody.replaceText('@N@', num);
  copyBody.replaceText('@Data@', anno);
  // Save and close the temporary document
  copyDoc.saveAndClose();
  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  //attach pdf and send email
  var subject = "Ricevuta Itcca 2018-2019";
 var html = 
      '<body>' +
        'Questa la tua ricevuta delle quote pagate. <br/>' +
          'Se vi sono errori per favore rispondi a questa email e segnalaceli, grazie. <br/>' +
            '<br/>' +
            'Cordiali saluti, <br/>' +
              'Carlo Lopez'
  MailApp.sendEmail(
    Email, 
    subject, 
    html, {
      htmlBody: html,
      attachments: pdf
    }
  );
  activeSheet.getRange(i, 31).setValue(1)

}


}

}
Sheila
  • 1
  • 2

1 Answers1

1

Your error is probably coming from && Sesso='M' (l.126). Then few lines later, the same on && Sesso='F', then && Sesso='M' and && Sesso='F' again. You should probably too add some ; after assignment of var html....

Finally, I strongly advice you to use getvalues() and setValues() in order to limit access to the spreadsheet. Keep in mind that communications between GS and sheet are quite expensive and should be limited at maximum to avoid performance issues. This isn't so complicated: you read data only one time and obtain a big array to parse GS side, then generate a new big array to save all data in one shot.

James Whiteley
  • 3,363
  • 1
  • 19
  • 46
Tristan
  • 108
  • 7
  • Why does that conditional add an error? Sorry I'm quite new at this and learned to do these scripts just for making these emails, as the previous program used went down... – Sheila Jan 16 '19 at 17:44