是否可以在脚本中使用标准的 GoogleSheets 函数?
Is it possible to use a standard GoogleSheets function in a script?
我想在脚本中写这样的东西。
max(A1:A10)
但是当我尝试时,我收到一条错误消息:Missing ) after argument list.
有正确的方法吗?
谢谢。
如果您希望代码是动态的,可以将函数从脚本写入 google 工作表。或者,您可以直接计算。
var ss = SpreadsheetApp.getActiveSheet();
// Gets full range of all data
// Useful, but not for the method I'll proceed with...
var ss_Range = ss.getDataRange()
var ss_A1Range = ss_Range.getA1Notation();
var ss_Data = ss_Range.getValues();
// Alternatively, access your last row:
var last_r = ss.getRange(ss.getLastRow() + 1
var last_c = ss.getRange(ss.getLastColumn()+1)
// Write your computation as a function:
// Say for column A
// Write your computation beneath the last row (or wherever)
ss.getRange(1,last_r+1).setValue('=Today(A2:A' + last_r + ')') // sorry my string formatting has what to be improved upon
// Compute yourself:
var max_val = Math.max(ss_Range.getValues("A2:A"+last_r).getValues())
ss.getRange(1,last_r+1).setValue(max_val);
您可以使用 SpreadsheetApp 的功能来做到这一点 class [1]。
您可以在链接到电子表格的脚本中使用以下代码,从 "Sheet3" 中的 A 列获取最大值并将其设置在 B1 单元格中:
function test3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet3");
var values = sheet.getRange("A1:A10").getValues();
var max = Number(values[0][0]);
for(var i=1; i<values.length; i++) {
var val = Number(values[i][0]);
if(val > max) {
max = val;
}
}
sheet.getRange("B1").setValue(max);
}
[1] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
不,无法在您的脚本中直接使用 sheet 函数。
但是对于这个特定的用例(我假设)您想要从给定范围 (A1:A10) 中找到最大数量,您可以执行以下操作:
var sheet = SpreadsheetApp.getActive().getSheetByName("[NAME-OF-SHEET]");
var values = sheet.getRange("A1:A10").getValues();
// flatten values array
values = Array.prototype.concat.apply([], values);
// find maximum
var max = Math.max.apply(null, values);
我想在脚本中写这样的东西。
max(A1:A10)
但是当我尝试时,我收到一条错误消息:Missing ) after argument list.
有正确的方法吗?
谢谢。
如果您希望代码是动态的,可以将函数从脚本写入 google 工作表。或者,您可以直接计算。
var ss = SpreadsheetApp.getActiveSheet();
// Gets full range of all data
// Useful, but not for the method I'll proceed with...
var ss_Range = ss.getDataRange()
var ss_A1Range = ss_Range.getA1Notation();
var ss_Data = ss_Range.getValues();
// Alternatively, access your last row:
var last_r = ss.getRange(ss.getLastRow() + 1
var last_c = ss.getRange(ss.getLastColumn()+1)
// Write your computation as a function:
// Say for column A
// Write your computation beneath the last row (or wherever)
ss.getRange(1,last_r+1).setValue('=Today(A2:A' + last_r + ')') // sorry my string formatting has what to be improved upon
// Compute yourself:
var max_val = Math.max(ss_Range.getValues("A2:A"+last_r).getValues())
ss.getRange(1,last_r+1).setValue(max_val);
您可以使用 SpreadsheetApp 的功能来做到这一点 class [1]。
您可以在链接到电子表格的脚本中使用以下代码,从 "Sheet3" 中的 A 列获取最大值并将其设置在 B1 单元格中:
function test3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet3");
var values = sheet.getRange("A1:A10").getValues();
var max = Number(values[0][0]);
for(var i=1; i<values.length; i++) {
var val = Number(values[i][0]);
if(val > max) {
max = val;
}
}
sheet.getRange("B1").setValue(max);
}
[1] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
不,无法在您的脚本中直接使用 sheet 函数。
但是对于这个特定的用例(我假设)您想要从给定范围 (A1:A10) 中找到最大数量,您可以执行以下操作:
var sheet = SpreadsheetApp.getActive().getSheetByName("[NAME-OF-SHEET]");
var values = sheet.getRange("A1:A10").getValues();
// flatten values array
values = Array.prototype.concat.apply([], values);
// find maximum
var max = Math.max.apply(null, values);