英文:
Google sheets Apps Script: Border rows in order to keep columns with the same value visually separate
问题
function borderByTask() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var dataStartRow = 4;
var lastRow = sh.getLastRow();
var range_input = ss.getRange("F4:F" + lastRow).getValues(); // Updated range input
var data = range_input.filter(String); // Eliminate empty cells
// Clear previous bordering
sh.getRange('A4:S' + lastRow).setBorder(null, null, null, null, null, null, "black", SpreadsheetApp.BorderStyle.NONE); // Clear all borders
for (var i = 0; i < data.length - 1; i++) {
if (data[i][0] !== data[i+1][0]) {
// Border the bottom of the cells if task is different below
sh.getRange(i + dataStartRow, 2, 1, 18).setBorder(null, null, true, null, null, true, "black", SpreadsheetApp.BorderStyle.SOLID_THIN);
}
}
}
请尝试使用上面提供的代码,这个版本已经进行了一些修正,应该可以解决你遇到的问题。如果还有其他问题,请告诉我。
英文:
Column A | ... | Column F | ... |
---|---|---|---|
Cell | ... | T1234 | ... |
Cell | ... | T1234 | ... |
Cell | ... | T1234 | ... |
Cell | ... | T1234 | ... |
Cell | ... | T1234 | ... |
Cell | ... | T1234 | ... |
______________ | ____ | ______________ | ____ |
Cell | ... | T4321 | ... |
Cell | ... | T4321 | ... |
______________ | ____ | ______________ | ____ |
Cell | ... | T4567 | ... |
...
I want to use a script that will partition cells with equivalent column F values like the above example.
function borderByTask() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var dataStartRow = 4;
var lastRow = sh.getLastRow();
var range_input = ss.getRange("F4:F").getValues();
var data = range_input.filter(String); //eliminate empty cells
//clear previous bordering
sh.getRange('A4:S').setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.None);//Clear all borders
Logger.log(data.length);
for (var i = (dataStartRow - 1); i < data.length; i++) {
if (data[i][0] != data[i+1][0]) { // index 2 = 3rd column = C
Logger.log(data[i][0]);
//Border the bottom of the cells if task is different below
//setBorder(top, left, bottom, right, vertical, horizontal, color, style)
sh.getRange(i, 2, 1, 18).setBorder(false, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
}
}
}
**My problem is:
- TypeError: Cannot read properties of undefined (reading '0')
borderByTask @ buttonFunctions.gs:85 - Borders do not get placed on appropriate rows**
答案1
得分: 0
尽管我不确定我是否能正确理解你期望的结果,但以下示例脚本是否是你期望的结果?
示例脚本:
function borderByTask() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
sheet.getRange('A4:S').setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.None);//清除所有边框
var lastRow = sheet.getLastRow();
var values = sheet.getRange("F4:F" + lastRow).getDisplayValues();
var rows = [...values.reduce((m, [f], i) => m.set(f, i + 4), new Map()).values()];
rows.forEach(e => {
if (e == lastRow) return;
sheet.getRange(`A${e}:S${e}`).setBorder(null, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
});
}
测试:
运行此脚本时,将获得以下结果。
参考资料:
英文:
Although I'm not sure whether I could correctly understand your expected result, is the following sample script your expected result?
Sample script:
function borderByTask() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
sheet.getRange('A4:S').setBorder(false, false, false, false /*right*/, false /*vertical*/, false /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.None);//Clear all borders
var lastRow = sheet.getLastRow();
var values = sheet.getRange("F4:F" + lastRow).getDisplayValues();
var rows = [...values.reduce((m, [f], i) => m.set(f, i + 4), new Map()).values()];
rows.forEach(e => {
if (e == lastRow) return;
sheet.getRange(`A${e}:S${e}`).setBorder(null, false, true, false /*right*/, false /*vertical*/, true /*horizontal*/, "black" /*color*/, SpreadsheetApp.BorderStyle.SOLID_THIN);
});
}
Testing:
When this script is run, the following result is obtained.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论