You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AppScript中为QUERY公式添加单引号失败的问题求助

问题:Google Sheets通过Apps Script设置QUERY公式时单引号丢失

我需要在单元格中设置以下QUERY公式:

=QUERY({'tab1'!A2:K; 'tab2/variation with spaces or slashes'!A2:K}; "SELECT Col1, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11 WHERE Col1 = 'Nieuw' AND Col2= TRUE ORDER BY Col3, Col4, Col6"; 0)

但设置完成后,生成的公式总是丢失表名外的单引号,变成这样:

=QUERY({tab1!A2:K; tab2/variation with spaces or slashes!A2:K}; ...)

我尝试了以下代码及多种变体,但都无法保留单引号:

var nieuwebSheet = ss.getSheetByName("Nieuwe boeken");

// Set the formula in cell A2 of the "Nieuwe boeken" tab
var formula = '=QUERY({';
for (var i = 0; i < TabNames.length; i++) {
  if (i > 0) {
    formula += '; ';
  }
  formula += "'" + TabNames[i] + "'!A2:K";
}
formula += '}; "SELECT Col1, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11 WHERE Col1 = \'Nieuw\' AND Col2= TRUE ORDER BY Col3, Col4, Col6"; 0)';
nieuwebSheet.getRange("A2").setFormula(formula);

我还试过这种写法:

formula += '\'' + quotedTabNames[i] + '\'!A2:K';

这是大型项目的一部分,无法共享实际表格,麻烦帮忙排查问题,谢谢!

内容的提问来源于stack exchange,提问作者IgN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:05:02