I was trying to get the information about the borders of a range, doing so I tried the getBorders() function, but received the following error:
Exception: Unexpected error while getting the method or property getBorders on object SpreadsheetApp.Range.
Here is the code below:
function test(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pg = ss.getActiveSheet();
var range = pg.getRange(1,1,10,5);
var border = range.getBorders();
}
For a workaround I needed to do a getBorder() for each cell on the range:
function getBorders(range){
var pg = range.getSheet();
var fr = range.getRow();
var fc = range.getColumn();
var lr = range.getLastRow();
var lc = range.getLastColumn();
var row = []
var finalArray = []
for(var r = fr;r<=lr;r++){
for(var c = fc;c<=lc;c++){
var exactRange = pg.getRange(r,c);
if(exactRange.isPartOfMerge()){
//For some reason I found that trying to get the border of an Merged cell is impossible
var hexTop = "#000000";
var hexLeft = "#000000";
var hexRight = "#000000";
var hexBottom = "#000000";
} else {
var bottomColor = exactRange.getBorder().getBottom().getColor();
var topColor = exactRange.getBorder().getTop().getColor();
var leftColor = exactRange.getBorder().getLeft().getColor();
var rightColor = exactRange.getBorder().getRight().getColor();
var bcolorType = bottomColor.getColorType();
var tcolorType = topColor.getColorType();
var rcolorType = rightColor.getColorType();
var lcolorType = leftColor.getColorType();
const getHexColor= (color,type) => {
switch(type){
case SpreadsheetApp.ColorType.RGB:
var hex = color.asRgbColor().asHexString();
break
default:
var hex = "#000000"
break
}
return hex;
}
var hexTop = getHexColor(topColor , tcolorType)
var hexLeft = getHexColor(leftColor , lcolorType)
var hexRight = getHexColor(rightColor , rcolorType)
var hexBottom = getHexColor(bottomColor , bcolorType)
}
row.push([[hexTop],[hexLeft],[hexRight],[hexBottom]]);
}
finalArray = finalArray.concat([linha]);
row= []
}
return finalArray;
}
I was expecting the getBorders() function to return to me an array of the function for each cell just like I did in the workaround, obviously not with the separation for the color values for top, left, right and bottom, which I would do where I would use this array