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:
- Open Excel Online or Excel for the web.
- Open an existing workbook or create a new workbook.
- Click on the “Automate” tab in the ribbon.
- Click on the “Script Lab” button.
- In the Script Lab pane, create a new script or open an existing one.
- Replace the default code in the script editor with the code provided above.
- 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()
.