Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset checkboxes to false

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.

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?

like image 457
DevB1 Avatar asked Jul 05 '18 07:07

DevB1


People also ask

Is there a way to uncheck all boxes in Google Docs?

Select All Checkboxes With Spacebar Highlight a range of checkboxes. Press the spacebar to toggle them checked or unchecked.

How do you clear a check box?

To delete all checkboxes at a time, go to the Home tab > Editing group > Find & Select > Go To Special, select the Objects radio button, and click OK. This will select all the check boxes on the active sheet, and you simply press the Delete key to remove them. Note.

How do I uncheck all checkboxes in Word?

If you want to remove multiple check boxes, highlight all of the lines you want to change. Click on the arrow on the right side of the “Bullets” button. From the next window, select the “None” option in the “Bullet Library” section.

How do I uncheck in Excel?

Pressing the Ctrl key, you can click, or click-and-drag to deselect any cells or ranges within a selection. If you need to reselect any of those cells, continue holding the Ctrl key and reselect those cells (for Mac, use the Cmd key).


2 Answers

How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.

Modification points :

  • 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().
  • Put "false" to values, if values[i][j] is "true".
  • Put the modified values to the sheet using setValues().

Modified script :

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

Reference :

  • setValue()
  • setValues()

If this was not what you want, please tell me. I would like to modify it.

like image 76
Tanaike Avatar answered Sep 20 '22 05:09

Tanaike


Update:

You can now use range.uncheck() directly on the range


Alternatively, Since you want to uncheck everything in the range (and all of the range have checkboxes in them), just do:

sheet.getRange('A3:A').setValue(false);

without checking/looping.

like image 25
TheMaster Avatar answered Sep 23 '22 05:09

TheMaster