为多列展开脚本

Expand script for multiple columns

我是新手,对这些错误深表歉意。每次其他列中的值为 0 时,我都尝试使用此脚本从列中删除复选框。脚本如下:

function onEdit() {var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); //change this to the name of your sheetui = SpreadsheetApp.getUi();

var names = ss.getRange("C1:C");

var namesValues = names.getValues(); //Get array of all the names

var checkboxes = ss.getRange("B1:B");

var cbRows = checkboxes.getHeight(); //Get # of rows in the rangesvar cbValues = checkboxes.getValues(); //Get array of all the checkbox column cell values//Logger.log(cbValues);

var newCBValues = new Array(cbRows); //Create an array to store all the new checkboxes values before we edit the actual spreadsheet

for (var row = 0; row < cbRows; row++) {
    newCBValues[row] = new Array(0); // Make the array 2 dimensional (even though it only has 1 column, it must be 2D).
    if (namesValues[row] == "0" || namesValues[row] == " ") { //If the name cell of this row is empty or blank then...
      newCBValues[row][0] = " "; //Set the value to one space (which will make the cell NOT true or false, and thus NOT display a checkbox).
      //Logger.log("newCBValues[" + row + "][0]: " + newCBValues[row][0]);
    }else{ //otherwise, if the name cell isn't blank...
      if (cbValues[row][0] === true) {
        newCBValues[row][0] = true; //Keep the checkbox checked if it's already checked
      }else{ //If the name cell isn't blank, and it's not true...
        newCBValues[row][0] = false; //Then Keep it or set it to False (an empty checkbox):
        
      }   
    }
  }
  checkboxes.setDataValidation(SpreadsheetApp.newDataValidation().requireCheckbox()).setValues(newCBValues);
  
}

If in var names = ss.getRange("C1:C") I ​​select 只有一列有效。但是当我想为更多列设置它时(例如C1:E)它不起作用。

希望你能帮助我。谢谢!!!!

编辑:

https://docs.google.com/spreadsheets/d/1MjIuZbON_nlaENqyARt_UnQZZ-ooZOMLjKmDM-nZl4M/edit#gid=1464332245

这是我正在尝试的文件。 Sheet 2. 你可以从应用程序脚本中看到如果我写的不是 var names = ss.getRange ("C1:C") (C1:E) 会发生什么变化。自己尝试(查看复选框列的差异)。谢谢!!!!

编辑 2:

This is the input (before running the script)

This is what I want (cell with 1 to have the checkboxes and cell with 0 not to have it)

根据您提供的示例输入输出情况,我认为您的目标如下。

  • 当“A”列和“B”列为 1 时,您想将复选框放在“D”列中。

在这种情况下,下面的示例脚本怎么样?根据您的显示脚本,在您的情况下,您可能希望通过脚本编辑器 运行 脚本。所以在本次修改中,没有使用事件对象。

示例脚本:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var values = sheet.getRange("A1:B" + sheet.getLastRow()).getValues();
  var rangeList = values.map((r, i) => r.every(e => e == 1) ? `D${i + 1}` : "").filter(String);
  sheet.getRangeList(rangeList).insertCheckboxes();
}

注:

  • 此示例脚本适用于您提供的示例电子表格。因此,当您更改电子表格时,脚本可能无法使用。请注意这一点。

参考文献:

已添加:

当我问我对In your goal, when the values of the columns "A" and "B" are 1, you want to put the checkbox to the column "D". Is my understanding correct?的理解是否正确时,你说Exactly!!!。但是,从您的以下回复中,

This was very useful for my example but as you also said, it does't work on my original file. I changed some values in the sheet to make it more similar to what I actually have

I want to add checkboxes on B where C, D and E have 1

如果要在列“C”、“D”和“E”为1时放置复选框,示例脚本如下。

示例脚本:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var lastRow = sheet.getLastRow();
  var values = sheet.getRange("C1:E" + lastRow).getValues();
  sheet.getRange("B1:B" + lastRow).removeCheckboxes();
  var rangeList = values.map((r, i) => r.every(e => e == 1) ? `B${i + 1}` : "").filter(String);
  sheet.getRangeList(rangeList).insertCheckboxes();
}