Here’s an example of how you can achieve this:
function checkCellColorIsBlue(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 fill object to access its color context.load(range.format.fill, 'color'); // Execute the request and check if the cell color is blue return context.sync() .then(function () { var cellColor = range.format.fill.color; // Check if the cell color is blue (RGB value: 0, 0, 255) return cellColor.red === 0 && cellColor.green === 0 && cellColor.blue === 255; }); }
In this example, the checkCellColorIsBlue
function takes a parameter rangeAddress
, which represents the address of the cell you want to check for the color. 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 fill object of the range is loaded using context.load(range.format.fill, 'color')
. Finally, the function checks if the cell color is blue by comparing the RGB values of the cell color with cellColor.red === 0 && cellColor.green === 0 && cellColor.blue === 255
.
To use this function, you can call it with the desired range address as an argument. For example:
checkCellColorIsBlue("A1") .then(function (isBlue) { console.log("Cell color is blue: " + isBlue); }) .catch(function (error) { console.log("Error occurred: " + error); });
Please note that this workaround relies on the RGB color values of the cell and assumes the exact RGB value of blue (0, 0, 255) to determine if the cell color is blue. If the cell color has a different shade of blue or is set using a different color model, 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.