Re-set checkboxes to true (dealing with blanks)- G

2019-09-21 15:14发布

I found the snippet below here: Re-set checkboxes to false - Google Apps Script

I'm interested in editing this to set false checkboxes to true, specifically adding to it to skip blank cells. Can't find anything helpful on skipping blanks.

function resetCheckBoxesAllSheets() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]

var dataRange = sheet.getRange('A4:Z100');
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;
    }
  }
}
dataRange.setValues(values);


}
}

1条回答
不美不萌又怎样
2楼-- · 2019-09-21 15:51

If you wish to flip flop the values of a set of checkboxes this will do it for a given column active range:

Note: capitalization counts and yes those are strings. If you have any doubts about it then use your script editor's debugger to see what is in your checkboxes. That's how I figured it out when I started playing with them.

function resetCheckBoxesAllSheets() {
  var ss = SpreadsheetApp.getActive();
  var sheet=ss.getActiveSheet()
  var dataRange = sheet.getActiveRange();
  var values = dataRange.getValues();
  for (var i=0;i<values.length;i++) {
    values[i][0]=values[i][0]?"FALSE":"TRUE";
  }
  dataRange.setValues(values);
}

So in your particular case, assuming everything else in your function works then try this:

function resetCheckBoxesAllSheets() {
  var ss = SpreadsheetApp.getActive();
  var allsheets = ss.getSheets();
  for (var s in allsheets){
    var sheet=allsheets[s]
    var dataRange = sheet.getRange('A4:Z100');
    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]) {
          values[i][j] = "FALSE";
        }
      }
    }
    dataRange.setValues(values); 
  }
}
查看更多
登录 后发表回答