简体   繁体   中英

Error with Google Sheets script – insert formula into cell

I'm getting this error trying to create a script for a workbook:

Syntax error: SyntaxError: missing ) after argument list line: 5 file: Add Import Range Formula.gs

I'm trying to create a script that will insert my formula into a cell... here's the formula itself (which includes calling another script).

=FILTER(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1H7tXnwHS37SSFbnQGAl0aQujCSzzf3oKvvjLg7fdwVs/edit#gid=149220930","Master Roster!A:B"), INDEX(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1H7tXnwHS37SSFbnQGAl0aQujCSzzf3oKvvjLg7fdwVs/edit#gid=149220930","Master Roster!A:C"),0,3)=GetSheetName())

It's working great when I paste it manually into my cell, but I'm unsure why the script won't accept it.

Here's the full script I'm trying to get working:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("c2");
cell.setFormula("=FILTER(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1H7tXnwHS37SSFbnQGAl0aQujCSzzf3oKvvjLg7fdwVs/edit#gid=149220930","Master Roster!A:B"), INDEX(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1H7tXnwHS37SSFbnQGAl0aQujCSzzf3oKvvjLg7fdwVs/edit#gid=149220930","Master Roster!A:C"),0,3)=GetSheetName())");

Any help how to get it working?

You need to escape some of the quotes. Also, query() is easier here than filter() . Try this:

cell.setFormula('=query(importrange("1H7tXnwHS37SSFbnQGAl0aQujCSzzf3oKvvjLg7fdwVs", "Master Roster:A1,B"), "where Col3 = \'" & GetSheetName() & "\' "; 1)');

Note that your code will insert a formula in the "first" sheet, ie, the one that appears leftmost in the tab bar. It will still do the same when the first sheet has been hidden, so check the hamburger menu ≣ in the tab bar to see if your first sheet is not currently visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM