1

I am trying to create a Google Sheet Script that sets a formula and autofills the formula for each new row added and getting the error: SyntaxError: missing ) after argument list (line 3, file "Code.gs")

Here is the script:

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 ss.getRange("AE2").setFormula("=LEFT(F2,FIND("for $",F2)-1)");
 }

I can't figure out where the missing ) is? The formula works without any problems in the sheet itself

Honestly I am not experienced in writing scripts so any help will be appreciated

3
  • 1
    Color highlighting shows the issue. You have the nested quotes It sees "=LEFT(F2,FIND(" and than it errors out Commented May 1, 2020 at 17:51
  • If you need to have literal double quotes, it's better to use single quoted strings: ss.getRange("AE2").setFormula('=LEFT(F2,FIND("for $",F2)-1)'); Commented May 1, 2020 at 17:59
  • Thanks makes perfect sense and works! Appreciate it Commented May 2, 2020 at 1:10

2 Answers 2

1

You can try escaping your " marks.

function myFunction() {
   var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
   ss.getRange("AE2").setFormula("=LEFT(F2,FIND(\"for $\",F2)-1)");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You'll need to escape the inner quotation marks otherwise they break the string input for setFormula to early and cause the SyntaxError. You escape characters in the string with a backslash (\).

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 ss.getRange("AE2").setFormula("=LEFT(F2,FIND(\"for $\",F2)-1)");
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.