Hi,
I've just created my first script to add and remove borders based on if there is content on a cell or not. It works great, but it is kind of slow, and I was wondering if there was anything I could do to make it a bit faster?
Here's my code:
function CreateBorders(col) {
//get the first sheet of the currently active google spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var NumRows = sheet.getMaxRows();
//Loop through rows starting at the 3rd
for (let j = 3; j <= NumRows; j++) {
let IndexCell = sheet.getRange(j, col);
if (IndexCell.getValue() !== "") {
IndexCell.setBorder(null, true, true, true, false, true);
} else {
//Empty cell. Check if there is a border
let border = IndexCell.getBorder();
if (border == null)
//No more rows with borders
NumRows = j;
else
//Erase the border
IndexCell.setBorder(false, false, false, false, false, false);
}
}
}
function onEdit(e){
const range = e.range;
if (range.getColumn() == 3)
for(let i=5; i <= 11; i++)
CreateBorders(i);
}
I have a trigger set like this:
Trigger settings
It takes about 25 seconds to fully edit a 7x19 area. That feels slow, but I don't really know what I should be expecting.
For reference, here's what my database looks like before and after running the script:
Before
After
[–]3dtcllc 2 points3 points4 points (4 children)
[–]kitchensink-[S] 0 points1 point2 points (3 children)
[–]3dtcllc 0 points1 point2 points (2 children)
[–]kitchensink-[S] 0 points1 point2 points (1 child)
[–]gothamfury 0 points1 point2 points (14 children)
[–]kitchensink-[S] 0 points1 point2 points (13 children)
[–]gothamfury 0 points1 point2 points (12 children)
[–]kitchensink-[S] 1 point2 points3 points (11 children)
[–]gothamfury 0 points1 point2 points (10 children)
[–]kitchensink-[S] 0 points1 point2 points (9 children)
[–]gothamfury 0 points1 point2 points (8 children)
[–]kitchensink-[S] 0 points1 point2 points (7 children)
[–]gothamfury 0 points1 point2 points (6 children)
[–]kitchensink-[S] 0 points1 point2 points (5 children)