0

I'm trying to set up a script to send an email everytime a new row is added into my google sheets worksheet. The worksheet is currently connected to my google forms.

However, I can't get this code to save as the error shows SyntaxError: Invalid or unexpected token (line 11, file "Email.gs".

I've been trying several times but can't figure out what is wrong. I would really appreciate some help with this script, Thank you.



function sendEmail() {

 //setup function

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

 if (ActiveSheet.getName() == 'FORM' ) {

   var StartRow = 2;

   var RowRange = ActiveSheet.getLastRow() – StartRow + 1;

   var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,17);

   var AllValues = WholeRange.getValues();

   var message = "";

   //iterate loop

   for (i in AllValues) {

     //set current row

     var CurrentRow = AllValues[i];


       //set HTML template for information

       message +=


           "<p><b>Entered: </b>" + CurrentRow[1] + "</p>" +

             "<p><b>Timestamp: </b>" + CurrentRow[2] + "</p>" +

               "<p><b>Name: </b>" + CurrentRow[3] + "</p>" +

                 "<p><b>ID No.: </b>" + CurrentRow[4] + "</p>" +

                   "<p><b>Contact: </b>" + CurrentRow[5] + "</p>" +

                      "<p><b>Address: </b>" + CurrentRow[6] + "</p>" + 
                        
                        "<p><b>Temperature: </b>" + CurrentRow[7] + "</p>" +
                          
                           "<p><b>Check In Date: </b>" + CurrentRow[8] + "</p>" +
                             
                              "<p><b>Room Number: </b>" + CurrentRow[9] + "</p>" +
                                
                                 "<p><b>Bank Details: </b>" + CurrentRow[10] + "</p>" +
                                   
                                    "<p><b>Symptoms: </b>" + CurrentRow[11] + "</p>" +
                                      
                                       "<p><b>Contact: </b>" + CurrentRow[12] + "</p>" +
                                         
                                          "<p><b>Agree to SOP: </b>" + CurrentRow[13] + "</p>" +
                                            
                                             "<p><b>Declaration: </b>" + CurrentRow[14] + "</p>" +
                                               
                                                "<p><b>Full Name: </b>" + CurrentRow[15] + "</p>" +
                        
                        
                        "</p><br><br>";

 
     }

   }//For loop close

   //define who to send emails to

   var SendTo = "myemail@email.com";

   //set subject line

   var Subject = "New Form";

   //send the actual email   if message is not empty

   if (message) {

     MailApp.sendEmail({

       to: SendTo,

       subject: Subject,

       htmlBody: message,

     });

   }//if message

 }//if sheetName Review

}//End Func
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Use either a single quote or a double quote. Or backticks if you are going for template strings. `“` or `“` is not valid syntax, where did you get the idea about using them? Re: `message` concatenation - hadouken! – Oleg Valter is with Ukraine Aug 15 '20 at 10:00
  • Thank you for the reply. I have just corrected the double quote errors, could be due to the text editor that I was using earlier. After running, it still shows the same error on Line 11. Wondering what else is wrong with the script above? – user14109448 Aug 15 '20 at 10:48
  • 1
    Maybe, use an IDE instead (like VS Code or upcoming update to the web IDE). it will save you from that. Don't see anything apart from the fact that you declared `i` on the global scope (since it is missing declaration). What is on line 11? – Oleg Valter is with Ukraine Aug 15 '20 at 11:05
  • 2
    As copied and pasted here, `ActiveSheet.getLastRow() – StartRow + 1` contains an En Dash character (code 8211) which is not a valid JavaScript operator, where you mean to use a hyphen (code 45). This was likely inserted by using a WYSIWYG text editor instead of a plain text editor. As Oleg recommended, this is solved by typing into (not copy/pasting) the online script editor or using a plain text editor like VS Code, vi, nano, etc. – dwmorrin Aug 15 '20 at 15:41
  • 1
    Did you find the original code on a blog, forum or similar place? What is the source of your code (add the link) ? What IDE do you used? – Rubén Aug 15 '20 at 20:46

1 Answers1

0

It's very likely that the syntax errors mentioned in the comments (use of curly quotes, instead of straight quotes, en dash instead of dash / hyphen ) were caused by copying the code from a website that display it code with the wrong characters, this maybe caused by the website template or a web browser extension.

If you are using the the Google Apps Script editor you should learn how to use it to debug your code in order to be able to find this syntax errors.

An additional option is to use another IDE with better syntax highlighting. You could even go beyond this by learning to use code lint.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166