每当C列值发生变化时,我想在行下方添加一条边框线。
我很长时间没有接触到宏,这是我放在一起的脚本,但它没有按预期工作。谁知道问题出在哪里?谢谢!
function underline() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
for (var i = 1; i < 10; i++) {
if (sheet.getRange(spreadsheet.getCurrentCell().getRow() + i, 3) != sheet.getRange(spreadsheet.getCurrentCell().getRow() + i+1, 3)) {
sheet.getRange(spreadsheet.getCurrentCell().getRow() + i, 1, 1, sheet.getMaxColumns()).activate();
spreadsheet.getActiveRangeList().setBorder(null, null, true, null, null, null, '#000000', SpreadsheetApp.BorderStyle.SOLID);
}
}
};
答案 0 :(得分:0)
您的原始代码有点令人费解,使用单个if
语句做了很多事情。我冒昧地将复杂的比特分成一个独特的函数findOriginal(cell)
。以下代码添加了一行:
function findOriginal(cell) { //offsets given cell until finds new content
while (cell.getValue() == cell.offset(1,0).getValue()) cell = cell.offset(1,0);
return cell;
}
function underline() {
var spreadsheet = SpreadsheetApp.getActive(),
cell = spreadsheet.getSelection().getCurrentCell();
cell = findOriginal(cell);
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(cell.getRow(), 1, 1, sheet.getLastColumn()) // setting the range to border
.setBorder(null, null, true, null, null, null,
'#000000', SpreadsheetApp.BorderStyle.SOLID); // you wrote this line
};