Here’s an example of an OfficeScripts function to check if a cell has borders on all sides, with the range address passed as a parameter to the function:
function checkCellHasAllSidesBorder(rangeAddress) { // Get the active worksheet var worksheet = context.workbook.worksheets.getActiveWorksheet(); // Get the range based on the provided address var range = worksheet.getRange(rangeAddress); // Load the border object to access its sides context.load(range.format.border, 'inside', 'outside'); // Execute the request and check if the cell has borders on all sides return context.sync() .then(function () { var cellBorder = range.format.border; // Check if the cell has borders on all sides return cellBorder.inside === "thin" && cellBorder.outside === "thin"; }); }
In this example, the checkCellHasAllSidesBorder
function takes a parameter rangeAddress
, which represents the address of the cell you want to check for borders. The function retrieves the active worksheet using context.workbook.worksheets.getActiveWorksheet()
. It then gets the range based on the provided address using worksheet.getRange(rangeAddress)
. The border object of the range is loaded using context.load(range.format.border, 'inside', 'outside')
. Finally, the function checks if the cell has borders on all sides by comparing the values of cellBorder.inside
and cellBorder.outside
to ensure they are set to “thin”.
To use this function, you can call it with the desired range address as an argument. For example:
checkCellHasAllSidesBorder("A1") .then(function (hasAllSidesBorder) { console.log("Cell has borders on all sides: " + hasAllSidesBorder); }) .catch(function (error) { console.log("Error occurred: " + error); });
Please note that this method assumes that the cell has the default “thin” border style applied on all sides. If the cell has a different border style or the border widths are modified, this method may not work as expected.
Keep in mind that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.