[英]Show & Hide Rows Based on Check Boxes
I will begin by saying that I am incredibly inexperienced with coding, so any help would be greatly appreciated!我首先要说的是,我对编码非常缺乏经验,因此将不胜感激任何帮助!
I am trying to show/hide rows based on whether a checkbox is ticked or not.我试图根据是否勾选复选框来显示/隐藏行。 For example, I have a checkbox in A5 - If it is unchecked, I want it to hide rows 6:18, and if it is checked, I want it to show rows 6:18.
例如,我在 A5 中有一个复选框 - 如果未选中,我希望它隐藏 6:18 行,如果选中,我希望它显示 6:18 行。
The same goes for a checkbox in A19, to hide/show rows 20:26 respectively. A19 中的复选框也是如此,分别隐藏/显示 20:26 行。 A27 for rows 28:37 etc.
A27 用于行 28:37 等。
I have found a previous forum post, in which I will link the code below, however the first checkbox is hiding ALL rows (Eg. if A5 is unchecked, it is hiding rows 6:18, 20:26 AND 28:37. When it is then checked, it is only showing rows 6:18.我找到了以前的论坛帖子,我将在其中链接下面的代码,但是第一个复选框隐藏所有行(例如,如果未选中 A5,则隐藏行 6:18、20:26 和 28:37。当然后检查它,它只显示 6:18 行。
function onEdit(e) {
var cfg = { // Please set this object.
A5: {startRow: 6, endRow: 18},
A19: {startRow: 20, endRow: 26},
A27: {startRow: 28, endRow: 37},
};
var activeRange = e.range.getA1Notation();
var ranges = Object.keys(cfg);
if (cfg[activeRange]) {
var sheet = e.source.getActiveSheet();
var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues();
values.forEach(function(e, i) {
if (e[0]) {
sheet.showRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow);
} else {
sheet.hideRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow);
}
});
}
}
As discussed, the first checkbox is hiding ALL rows (Eg. if A5 is unchecked, it is hiding rows 6:18, 20:26 AND 28:37. When it is then checked, it is only showing rows 6:18.如前所述,第一个复选框隐藏所有行(例如,如果未选中 A5,则隐藏行 6:18、20:26 和 28:37。然后选中时,它仅显示行 6:18。
If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何? Please think of this as just one of several answers.
请认为这只是几个答案之一。
var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues();
values.forEach(function(e, i) {
if (e[0]) {
var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues().filter(String); // Modified
values.forEach(function(e, i) {
if (e[0] === true) { // Modified
If I misunderstood your question and this was not the result you want, I apologize. 如果我误解了您的问题,而这不是您想要的结果,我深表歉意。
Tanaike code did not work for me if the rows were not empty. 如果行不为空,Tanaike代码对我不起作用。 This worked for me:
这对我有用:
function onEdit(e) { var cfg = { // Please set this object. A5: {startRow: 6, endRow: 18}, A19: {startRow: 20, endRow: 26}, A27: {startRow: 28, endRow: 37}, }; var activeRange = e.range.getA1Notation(); var sheet = e.source.getActiveSheet(); var ranges = Object.keys(cfg); var cells = [] for(var j = 0; j < ranges.length; j++){ cells.push(sheet.getRange(ranges[j])) } if (cfg[activeRange]) { cells.forEach(function(cell, i) { if (cell.getValue()) { sheet.showRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow); } else { sheet.hideRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow); } }); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.