Re-set checkboxes to true (dealing with blanks)- Google Apps Script

99封情书 提交于 2019-12-14 03:25:56

问题


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:


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); 
  }
}


来源:https://stackoverflow.com/questions/54795712/re-set-checkboxes-to-true-dealing-with-blanks-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!