将复选框重置为 false
我有一个 Google 电子表格,其中 A 列的每一行都有复选框.我已经编写了一个脚本来在复选框被选中的所有行上执行一个函数,但我想在最后添加一个重置函数,以便在脚本运行后再次取消选中所有选中的框.
I have a Google spreadsheet where column A has checkboxes in each row. I have written a script to perform a function on all rows where the checkboxes are checked, but I want to add in at the end a reset function so that all checked boxes are unchecked again after the script is run.
我尝试过使用这样的 for 循环:
I've tried using a for loop like this:
var dataRange = sheet.getRange('A3:A');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == true) {
values[i].setValue(false);
}
}
}
但显然这不起作用,因为我收到错误.
But clearly this doesn't work as I get an error.
有谁知道如何做到这一点?
Does anyone know how this could be done?
这个修改怎么样?我认为针对您的情况有几种解决方案.因此,请将此视为其中之一.
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
- 问题的原因是
values[i].setValue(false);
.values[i]
是一个数组.请使用setValue()
的范围.- 但是在 for 循环中使用
setValue()
会导致更高的成本.所以在这个修改中,我使用了setValues()
.
- The reason of the issue is
values[i].setValue(false);
.values[i]
is an array. Please use the range forsetValue()
.- But to use
setValue()
in the for loop leads to higher cost. So in this modification, I usedsetValues()
.
var dataRange = sheet.getRange('A3:A'); var values = dataRange.getValues(); for (var i = 0; i < values.length; i++) { for (var j = 0; j < values[i].length; j++) { if (values[i][j] == true) { values[i][j] = false; // Modified } } } dataRange.setValues(values); // Added
参考:
- setValue()
- setValues()
如果这不是您想要的,请告诉我.我想修改一下.
If this was not what you want, please tell me. I would like to modify it.
- But to use
- 但是在 for 循环中使用