如何自动添加/删除复选框的不同单元格是否包含值?

Bar*_*Guy 5 google-sheets google-apps-script

我正在使用 Google 表格中的新复选框功能。

在我的工作表中,我想要一个清单列,但希望复选框的数量与工作表中具有值的行数相匹配。
例如,假设 A 列包含公司员工的姓名(不时更改),B 列包含复选框,我希望一行中有一个复选框,仅当 A 列中的单元格包含值时.

下面是一个例子:

https://docs.google.com/spreadsheets/d/1YKxGzThlMXzJRSGLON8Do4dzJq6HEldcpNTbDzH3Wsw/edit?usp=sharing

我怎样才能做到这一点?

小智 9

不知道这是否会有所帮助。但我实现此目的的一种方法是使用条件格式。

例子。如果我在 A2:A 中有条目,并且我只想在 B2:B 中显示复选框,如果相应的“A”单元格中有数据,我会执行以下操作;

  1. 突出显示 B2:B 中的整个列
  2. 插入 -> 复选框(这会将复选框一直向下放置)
  3. 突出显示整个复选框列
  4. 格式 -> 条件格式 -> 自定义公式
  5. 输入以下自定义公式;=ISBLANK($A2)
  6. 将背景颜色和文字颜色更改为相同

然后这将评估“A”列,如果它是空白的,它将使背景和文本颜色相同(例如白色)并且所有复选框都将“消失”。当您在“A”列中输入信息时,复选框将“出现”

这是一种简单的解决方法,适用于大多数情况下的脚本解决方案。


Doo*_*omd 3

更新(更快)的批处理答案:
我之前的答案(见下文)是匆忙编写的,目的是说明如何根据另一个单元格的内容显示或隐藏新复选框。为了加快速度,以下新脚本将所有内容处理到一个数组中,并且在计算所有值之前不会更新单元格。它仍然不如内置的客户端响应那么快,但它可以在一两秒内处理数百或数千个名称(因此它非常适合能够粘贴数百个名称而不会陷入困境),我做到了尝试采用developers.google.com上此页面的“最佳实践” 。

假设:

步骤 1:
如果您已复制示例表,则可以跳过此步骤突出显示复选框列中的所有相关单元格,右键单击它们,然后Data Validation从菜单中选择“ ”。在弹出的对话框中,确保Checkbox从“条件”部分选择“ ”,然后单击“ Save”。

步骤 2:
如果您复制了示例工作表,则该脚本应该已经绑定到该工作表,否则:将以下脚本插入并保存到脚本编辑器“.”中Tools > Script Editor注意:您可能必须根据 Google 的安全权限批准脚本运行。

  function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("New Checkboxes"); //change this to the name of your sheet
  ui = SpreadsheetApp.getUi();

  //PICK ONE & comment out the other one:
  //var names = ss.getRange("names");//Use this if you are naming the range
  var names = ss.getRange("B3:B");//Use this if you are naming the ranges

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

  //PICK ONE & comment out the other one:
  //var checkboxes = ss.getRange("checkboxes"); //Use this if you are naming the range
  var checkboxes = ss.getRange("A3:A"); //Use this if you want to hard-code your range

  var cbRows = checkboxes.getHeight(); //Get # of rows in the ranges
  var 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] == "" || 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.setValues(newCBValues); // now that we have a completed array of our new checkbox values, let's edit the sheet with them!

}
Run Code Online (Sandbox Code Playgroud)

步骤 3:
返回电子表格,在姓名栏中插入、编辑、删除或输入一些姓名以查看更改。

就是这样!


旧(慢)答案:
如果您不介意在工作表中添加脚本,我想出了如何使用“新”复选框,但仅当您在名称列中有相应的名称时才显示这些框。出于脚本的目的,我确实为复选框列(“复选框”)和名称列(“名称”)创建了命名范围。如果您希望使用此脚本,您还必须创建相同的命名范围(或者只是将范围硬编码到脚本中 - 如果您要通过添加来频繁更改范围的大小,这不是一个好方法并删除名字。转到菜单Data > Named ranges...

不管怎样,我创建了一个更新的示例,您可以在此页面上演示:https://docs.google.com/spreadsheets/d/1MSnQ1_6Sy018lAoehzQ55QVrfJldoKS5EnWjOxYWxIs/edit#gid=1464332245

您需要制作此工作簿的副本,然后在从菜单将其添加到工作簿后授予自己使用以下脚本的权限Tools > Script Editor

function onEdit() {
  var app = SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet();

  var names = activeSheet.getRange("names"); //Hard Code the Range here if you don't want to use named ranges (example: "B2:B")
  var checkboxes = activeSheet.getRange("checkboxes"); //Hard Code the Range here if you don't want to use named ranges (example: "A2:A")

  var namesFirstRow = names.getRow();
  var namesLength = names.getLastRow();
  var namesCol = names.getColumn();
  var checkboxesCol = checkboxes.getColumn();

  for (var i=0; i<namesLength; i++) {
    var checkCell = activeSheet.getRange(namesFirstRow+i,checkboxesCol);
    var nameCell = activeSheet.getRange(namesFirstRow+i,namesCol);
    Logger.log(checkCell.getA1Notation() + "  " + nameCell.getA1Notation());

    if (nameCell == "" || nameCell.isBlank()) { //If There is no name in the name column (it's empty or just blank)
      checkCell.setValue(" "); // Make the checkbox column blank so no checkbox appears 
    }else{
      if (checkCell.getValue() === true || checkCell.getValue() === false) {
        //Do nothing since a value is already set
        //app.getUi().alert(checkCell.getA1Notation() + " is " + checkCell.getValue());
      }else{      
        checkCell.setValue("FALSE"); //Place an empty checkbox (FALSE) if a name value has not been set in the name column
      }
    }
  } 

}
Run Code Online (Sandbox Code Playgroud)

仅当您确保“复选框”范围已将数据验证选择为“复选框”条件时,这才有效...

说明:对于您需要复选框的列:突出显示列中的整个单元格范围(标题除外),右键单击所选列,选择“数据验证”,然后选择“复选框”作为条件。然后,所有“TRUE”和“FALSE”单元格都将被选中或取消选中复选框。此外,要集成脚本内的所有命名范围,请确保将此复选框列范围命名为“checkboxes”(按照上面的说明)。