Store and read data from Range objects using OfficeScripts

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:

  1. It gets the active worksheet using the getActiveWorksheet method.

  2. It defines a range (A1:B2) within the worksheet using the getRange method.

  3. It creates a 2D array called data with sample values that will be stored in the range.

  4. It assigns the data array to the values property of the range, effectively storing the data in the range.

  5. It reads the stored data from the range by accessing the values property of the range.

  6. 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.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech