Here’s a sample OfficeScript that demonstrates how to read various properties of a range in Excel:
function readRangeProperties() { // Get the active worksheet var worksheet = context.workbook.getActiveWorksheet(); // Get a range to read properties from var range = worksheet.getRange("A1:C3"); // Read range properties var rangeAddress = range.getAddress(); var rangeValues = range.getValues(); var rangeNumberFormat = range.getNumberFormat(); var rangeFontColor = range.getFormat().getFont().getColor(); var rangeBackgroundColor = range.getFormat().getFill().getColor(); // Logging the range properties console.log("Range Address: " + rangeAddress); console.log("Range Values: "); console.log(rangeValues); console.log("Range Number Format: " + rangeNumberFormat); console.log("Range Font Color: " + rangeFontColor); console.log("Range Background Color: " + rangeBackgroundColor); }
In this example, the readRangeProperties
function performs the following steps:
It gets the active worksheet using the
getActiveWorksheet
method.It defines a range (
A1:C3
) within the worksheet using thegetRange
method.It reads various properties of the range, such as the address, values, number format, font color, and background color.
The range properties are obtained using appropriate methods like
getAddress
,getValues
,getNumberFormat
,getFont().getColor
, andgetFill().getColor
.Finally, it logs the range properties to the console.
You can call the readRangeProperties
function to see the range properties in the console output.
Please note that this example assumes that you have a workbook open and a worksheet active in Excel for the web or Excel Online, where you can execute the OfficeScript.
Additionally, remember to set up the necessary context and load the OfficeScript runtime to ensure the script executes successfully.