简体   繁体   English

谷歌表格脚本出错 - 将公式插入单元格

[英]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语法错误: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() .此外, query()在这里比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.当第一张工作表被隐藏时,它仍然会执行相同的操作,因此请检查标签栏中的汉堡菜单 ≣ 以查看您的第一张工作表当前是否不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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