.getBorder() not showing borders set from other cells
Asked Answered
H

1

0

Example: If I set a right border in cell D2, then look at .getBorder().getLeft().getBorderStyle() in D3, there is no border, yet when I clear borders from cell D3, it clears the border that was set in D2, which tells me that it sees that there is a border there in some capacity.

If I set a border in D2, is there a way via GAS to tell if that border is there in D3?

edit: I am trying to tell if there is a border between two cells, regardless of what cell the border was set in. I.E., I set a right border in D2 and I want to see it programmatically from cell D3.

Heilbronn answered 28/7, 2020 at 0:7 Comment(3)
Although I'm not sure whether I could correctly understand about your question, I think that when the cell "D2" has the border at the right and the border of cell "D3" is removed by 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
If you think that this is a bug then you should report it through the issue tracker -> developers.google.com/apps-script/support#bugsFillander
There is no getBorder method for class RangeParadigm
F
0

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];
}
Fillander answered 8/8 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.