将字符串转换为 Google 电子表格中的公式
Convert a string to a formula in Google Spreadsheet
我花了 10 多个小时阅读并尝试了不同的选项,但都没有成功。
我有这个字符串(这其实是其他公式生成的字符串)
QUERY({IMPORTRANGE(A1;$D);IMPORTRANGE(A2;$D);IMPORTRANGE(A3;$D);IMPORTRANGE(A4;$D)};"select Col13, sum(Col1), sum(Col2), sum(Col3), sum(Col4), sum(Col5), sum(Col6), sum(Col7), sum(Col8), sum(Col9), sum(Col10), sum(Col11), sum(Col12) group by Col13";0)
而且我希望它可以作为一个公式来阅读。
所以例如我尝试这个函数:
function doConvert(formula) {
// Strip leading "=" if there
if (formula.charAt(0) === '=') formula = formula.substring(1);
return eval(formula);
}
但我得到:
Error / SyntaxError: Falta ":" detrás del ID de propiedad.
(In English would be: ":" missing after property ID.
任何其他解决方案都很好。
将 =
添加到您生成的字符串中,然后像这样尝试:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet_Name_Here');
var src = sheet.getRange("A1"); // The cell which holds the formula
var str = src.getValue();
var cell = sheet.getRange("C5"); // The cell where you want the results to be in
cell.setFormula(str);
}
Google Sheets 公式无法在 server/client 代码上计算,只能在 Google Sheets UI.
上计算
如果您希望将字符串作为公式传递给单元格,请使用 Class 范围
中的 setFormula(formula)
方法
注意:无需在公式前添加等号。
例子
假设公式字符串在A1中,而你想把公式放在B1中
function setFormula(){
var sheet = SpreadsheetApp.getActiveSheet();
var source= sheet.getRange('A1');
var formula = source.getValue();
var target = sheet.getRange('B1');
target.setFormula(formula);
}
相关
- Is there a way to evaluate a formula that is stored in a cell?
- How to evaluate a spreadsheet formula within a custom function?
我花了 10 多个小时阅读并尝试了不同的选项,但都没有成功。
我有这个字符串(这其实是其他公式生成的字符串)
QUERY({IMPORTRANGE(A1;$D);IMPORTRANGE(A2;$D);IMPORTRANGE(A3;$D);IMPORTRANGE(A4;$D)};"select Col13, sum(Col1), sum(Col2), sum(Col3), sum(Col4), sum(Col5), sum(Col6), sum(Col7), sum(Col8), sum(Col9), sum(Col10), sum(Col11), sum(Col12) group by Col13";0)
而且我希望它可以作为一个公式来阅读。 所以例如我尝试这个函数:
function doConvert(formula) {
// Strip leading "=" if there
if (formula.charAt(0) === '=') formula = formula.substring(1);
return eval(formula);
}
但我得到:
Error / SyntaxError: Falta ":" detrás del ID de propiedad.
(In English would be: ":" missing after property ID.
任何其他解决方案都很好。
将 =
添加到您生成的字符串中,然后像这样尝试:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet_Name_Here');
var src = sheet.getRange("A1"); // The cell which holds the formula
var str = src.getValue();
var cell = sheet.getRange("C5"); // The cell where you want the results to be in
cell.setFormula(str);
}
Google Sheets 公式无法在 server/client 代码上计算,只能在 Google Sheets UI.
上计算如果您希望将字符串作为公式传递给单元格,请使用 Class 范围
中的setFormula(formula)
方法
注意:无需在公式前添加等号。
例子
假设公式字符串在A1中,而你想把公式放在B1中
function setFormula(){
var sheet = SpreadsheetApp.getActiveSheet();
var source= sheet.getRange('A1');
var formula = source.getValue();
var target = sheet.getRange('B1');
target.setFormula(formula);
}
相关
- Is there a way to evaluate a formula that is stored in a cell?
- How to evaluate a spreadsheet formula within a custom function?