示例表,它展示了我正在尝试做的事情。基本上,只要 A 列中的值发生变化,我就需要应用水平边框,但我只想将其应用于 visible 行。
我使用了这个函数,假设所有行都可见,该函数可以很好地工作,但如果您过滤掉或隐藏值发生变化的行之一,则该函数不起作用:
function myFunction() {
const sheetNames = ["Grade 6","Grade 7","Grade 8","Algebra 1","Algebra 2","Geometry"]; // Please set the sheet names.
const ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(sheetName => {
const sheet = ss.getSheetByName(sheetName);
const range = sheet.getRange("A2:A" + sheet.getLastRow());
const { ranges } = range.getValues().reduce((o, [a], i) => {
if (i == 0) {
o.temp = a;
} else if (i > 0 && o.temp != a) {
o.ranges.push(`A${i + 2}:BY${i + 2}`);
o.temp = a;
}
return o;
}, { ranges: [], temp: "" });
sheet.getRange("A2:BY").setBorder(false, null, false, null, null, false);
sheet.getRangeList(ranges).setBorder(true, null, null, null, null, null, "black", SpreadsheetApp.BorderStyle.SOLID);
});
}
我希望边框出现在 visible 行之间,即使在过滤或隐藏时也是如此(例如,在“所需输出”表中,其中包含“C”的行被过滤)。有谁有任何相当简单的方法来帮助实现这一目标?我对脚本还很陌生,所以我试图以一种允许我自己编辑的方式将其拼凑在一起。
function myFunction() {
const sns = ["Sheet0", "Sheet1"];
const cs = ["#ffffff", "#cccccc"]
const ss = SpreadsheetApp.getActive();
sns.forEach(sn => {
const sh = ss.getSheetByName(sn);
const range = sh.getRange("B3:B" + sh.getLastRow());
range.getValues().flat().reduce((o, a, i) => {
if (i == 0) {
o.pastA.push(a);
o.pastC.push(0);
sh.getRange(`A${i + 3}:P${i + 3}`).setBackground(cs[o.pastC[o.pastC.length - 1]])
}
if (i > 0 && o.pastA[o.pastA.length - 1] != a) {
sh.getRange(`A${i + 3}:P${i + 3}`).setBackground(cs[(o.pastC[o.pastC.length - 1] + 1) % 2]);
o.pastA.push(a);
o.pastC.push(((o.pastC[o.pastC.length - 1] + 1) % 2))
} else {
sh.getRange(`A${i + 3}:P${i + 3}`).setBackground(cs[(o.pastC[o.pastC.length - 1])]);
}
return o;
}, { ranges: [], pastA: [], pastC: [] });
});
}