0

I am trying to make a script that creates a new tab based on data submitted from a form and inserts a formula into the new tab on creation. The last line of the script is giving me a syntax error. "Missing ) after argument list". All parenthesis seem to be closed properly. Am I missing something?

function makeTabs() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var last = sheet.getLastRow();
  for(var i=0; i<last; i++){
    var tabName = sheet.getRange(i+1,1).getValue();
    var create = ss.insertSheet(tabName);
    var tab = ss.getRange('A2').activate();
    var tab = ss.getCurrentCell().setFormula('=QUERY(\'Form Responses 1\'!A2:R,"select * where C="'tabName'",-1)")');
  }
}
3
  • 1
    Your variable is outside of quotes, and you are not preforming any operation on it. Change to this ... C=" + tabName + " ... Commented Feb 7, 2020 at 22:40
  • This helped a lot. It got the tabName into the document. I did need to add extra quotes in to make sure that the Query ran on the front end: C=""' +tabName+ '""" Commented Feb 7, 2020 at 23:53
  • check your 'tabName' in setformula expression Commented Feb 8, 2020 at 0:55

2 Answers 2

1

Assuming that tabName is a String, it should be single quote enclosed on the Google Sheets side. Also use + to concatenate on the Google Apps Script side

Replace

'=QUERY(\'Form Responses 1\'!A2:R,"select * where C="'tabName'",-1)")'

by

'=QUERY(\'Form Responses 1\'!A2:R,"select * where C=\'"' + tabName + '\'",-1)")'

By the other hand, bear in mind that the Best Practices discourage the use of Apps Script read/write methods inside of loops

Reference

Sign up to request clarification or add additional context in comments.

Comments

0

There is a syntax error in the last statement where you are concatenating two strings with tabName between. String concatenation can be performed with the + operator, like this:

'=QUERY(\'Form Responses 1\'!A2:R,"select * where C="' + tabName + '",-1)")'

The error is received because without an operator to signify an operation or a comma to signify an additional argument, it's expecting to see the close of the function with ).

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.