Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

checkbox - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...