将复选框重置为 false [英] Reset checkboxes to false

查看:42
本文介绍了将复选框重置为 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 for setValue().
      • But to use setValue() in the for loop leads to higher cost. So in this modification, I used setValues().
      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.

        这篇关于将复选框重置为 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆