Here’s a sample OfficeScript that demonstrates how to store data in Range objects and read data from them:
function storeAndReadDataFromRange() { // Get the active worksheet var worksheet = context.workbook.getActiveWorksheet(); // Get a range to store data var range = worksheet.getRange("A1:B2"); // Data to be stored in the range var data = [ ["John", 30], ["Jane", 35] ]; // Store the data in the range range.values = data; // Read the data from the range var storedData = range.values; // Logging the stored data console.log(storedData); }
In this example, the storeAndReadDataFromRange
function performs the following steps:
It gets the active worksheet using the
getActiveWorksheet
method.It defines a range (
A1:B2
) within the worksheet using thegetRange
method.It creates a 2D array called
data
with sample values that will be stored in the range.It assigns the
data
array to thevalues
property of the range, effectively storing the data in the range.It reads the stored data from the range by accessing the
values
property of the range.Finally, it logs the stored data to the console.
You can call the storeAndReadDataFromRange
function to see the stored data 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.