我有一系列动态数据,显示下拉列表中的数据。
数据:
我尝试使用这篇文章中的脚本。
function onEdit(e) {
const sheetName = "MONTHLY OVERVIEW";
const expectedRange = "C10:G1000";
const { range } = e;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName) return;
const r = sheet.getRange(expectedRange);
const startRow = r.getRow();
const endRow = startRow + r.getNumRows() - 1;
const startCol = r.getColumn();
const endCol = startCol + r.getNumColumns() - 1;
if (range.rowStart < startRow || range.rowEnd > endRow || range.columnStart < startCol || range.columnEnd > endCol) return;
r.setBorder(null, false, false, false, false, false);
const last = r.getDisplayValues().findIndex(rr => !rr.join(""));
sheet.getRange(startRow, startCol, last != -1 ? last : r.getNumRows(), endCol - startCol + 1).setBorder(true, true, true, true, true, false);
}
但是只有当我编辑最后一个动态数据下面的单元格时才会出现边框。
有没有办法让边框根据动态数据自动添加,并且底部边框前多一个空行,如上图所示?
代码:
function onEdit(e) {
const sheetName = "MONTHLY OVERVIEW";
const expectedRange = "C10:G1000";
const { range } = e;
const sheet = range.getSheet();
if (sheet.getSheetName() !== sheetName) return;
const r = sheet.getRange(expectedRange);
const startRow = r.getRow();
const endRow = startRow + r.getNumRows() - 1;
const startCol = r.getColumn();
const endCol = startCol + r.getNumColumns() - 1;
if (range.rowStart < startRow || range.rowEnd > endRow || range.columnStart < startCol || range.columnEnd > endCol) return;
r.setBorder(false, false, false, false, false, false);
const values = r.getDisplayValues();
let lastRow = 0;
for (let i = 0; i < values.length; i++) {
if (values[i].some(cell => cell.trim() !== "")) {
lastRow = i + 1;
}
}
if (lastRow > 0) {
const borderRange = sheet.getRange(startRow, startCol, lastRow + 1, endCol - startCol + 1);
borderRange.setBorder(true, true, true, true, true, false);
}
}