Created
March 15, 2018 15:02
-
-
Save emorisse/2d4219cd77dec541aaf8181aa29cba55 to your computer and use it in GitHub Desktop.
Outline borders of groups of cells in google sheets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var menu = [{name: "Borders for nonempty cells", functionName: "borders"}]; | |
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu); | |
} | |
function borders() { | |
var range = SpreadsheetApp.getActiveSheet().getRange("C2:O20"); | |
range.setBorder(false, false, false, false, false, false); | |
var values = range.getValues(); | |
var borders = range.getValues(); | |
for (var i = 0; i < values.length; i++) { | |
for (var j = 0; j < values[i].length; j++) { | |
if (values[i][j] > 0) { | |
var top = true | |
var left = true | |
var bottom = true | |
var right = true | |
if (i >=1 && values[i-1][j] > 0) { | |
top = false | |
} | |
if (i < (values.length +1) && values[i+1][j] > 0) { | |
bottom = false | |
} | |
if (j >= 1 && values[i][j-1] > 0) { | |
left = false | |
} | |
if (j < (values[i].length +1) && values[i][j+1] > 0) { | |
right = false | |
} | |
borders[i][j] = [ top, left, bottom, right ] | |
} | |
} | |
} | |
var vertical = true | |
var horizontal = true | |
for (var i = 0; i < values.length; i++) { | |
for (var j = 0; j < values[i].length; j++) { | |
if (values[i][j] > 0) { | |
top = borders[i][j][0] | |
left = borders[i][j][1] | |
bottom = borders[i][j][2] | |
right = borders[i][j][3] | |
range.getCell(i + 1, j + 1).setBorder(top, left, bottom, right, vertical, horizontal); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a boundary bug somewhere. The selected range needs to be at least one row more than the range to work on.