SpreadsheetApp.Range.getBorder
is not included in https://developers.google.com/apps-script/reference/spreadsheet/range so we can't tell for sure what is the intended behavior.
As of August 8th, 2024, SpreadsheetApp.Range.getBorder
returns null
for cells having the default border setting.
The below sample code shows a way to handle this (using conditional operator )
/**
* Show a toast with information of the cell borders from the active range.
*
*/
function getBorders() {
const range = SpreadsheetApp.getActiveSheet().getActiveRange();
const rangeBorders = [];
if (range.getHeight() === 1 && range.getWidth() === 1) {
const cellBorder = range.getBorder();
rangeBorders.push([
cellBorder
? getCellBorderDetails(cellBorder)
/** if the value of cellBorder is falsy (i.e. null), return an array with an empty string. */
: [""]
]);
} else {
const borders = range.getBorders();
const rowBorders = [];
borders.forEach(row => {
row.forEach(cellBorder => {
rowBorders.push(getCellBorderDetails(cellBorder));
});
rangeBorders.push(rowBorders);
});
}
SpreadsheetApp.getActiveSpreadsheet().toast(JSON.stringify(rangeBorders));
}
function getCellBorderDetails(cellBorder) {
const cellBorderDetailsByPos = {
top: cellBorder.getTop(),
right: cellBorder.getRight(),
bottom: cellBorder.getBottom(),
left: cellBorder.getLeft()
}
const [top, right, bottom, left] = ['top', 'right', 'bottom', 'left'].map(pos => (
{
style: cellBorderDetailsByPos[pos].getBorderStyle(),
color: cellBorderDetailsByPos[pos].getColor().asRgbColor().asHexString()
}
));
return [top, right, bottom, left];
}
sheet.getRange("D3").setBorder(false, false, false, false, false, false)
, the left border of the cell "D2" is not removed while the bottom border of the cell "D2" is removed. If my understanding is not correct, please tell me. – Viradis