OfficeScripts to make API POST request and print results

Here’s an example of an OfficeScript that makes an API POST request to a dummy JSON API site and prints the response status in the console:

 
function main() {
  // Define the request payload
  let payload = {
    title: "OfficeScripts",
    body: "Making API requests",
    userId: 1
  };

  // Make the API POST request
  let response = UrlFetchApp.fetch('https://jsonplaceholder.typicode.com/posts', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify(payload)
  });

  // Print the response status in the console
  console.log(response.getResponseCode());
}

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 POST request to the “https://jsonplaceholder.typicode.com/posts” API endpoint with a sample payload. The payload is sent as JSON data in the request body. The response status code is then printed in the console using console.log().

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 UrlFetchApp service is used to make the API request, and the response status code is obtained using getResponseCode().

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