OfficeScripts to make API Get request and print response to Excel range

Here’s an example of an OfficeScript that makes an API GET request to a dummy JSON API site and prints the values to an Excel range:

function main(worksheet: ExcelScript.Worksheet) { // Make the API GET request let response = await fetch('https://jsonplaceholder.typicode.com/posts'); // Parse the response as JSON let jsonData = await response.json(); // Get the range where the data will be printed let range = worksheet.getRange("A1:B" + (jsonData.length + 1)); // Prepare the data to be printed let data = jsonData.map(function(item) { return [item.id, item.title]; }); // Set the values in the range range.setValues(data); }

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

To use this OfficeScript, follow these steps:

  1. Open Excel Online or Excel for the web.
  2. Open an existing workbook or create a new workbook.
  3. Click on the “Automate” tab in the ribbon.
  4. Click on the “Script Lab” button.
  5. In the Script Lab pane, create a new script or open an existing one.
  6. Replace the default code in the script editor with the code provided above.
  7. Click the “Run” button to execute the script.

This script makes a GET request to the “https://jsonplaceholder.typicode.com/posts” API endpoint, which returns a dummy JSON data set. The response is parsed as JSON, and the id and title values are extracted from each item. The data is then printed to the range starting from cell A1 in the active worksheet.

Please note that OfficeScripts are currently only supported in Excel for the web and Excel Online. They are not available in the desktop version of Excel. Additionally, the fetch function is used to make the API request, which is an asynchronous function and requires the await keyword to handle the response.

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