How to Use API in Google Sheets
Integrating APIs into Google Sheets can greatly enhance the capabilities of your spreadsheets, enabling real-time data retrieval and automating workflows. In this guide, we'll explore how to use APIs in Google Sheets, making your data more dynamic and efficient.
APIs, or Application Programming Interfaces, allow different software applications to communicate and interact with each other. By leveraging APIs, you can fetch data from external sources into your Google Sheets, keeping your data up-to-date and eliminating manual data entry.
Understanding APIs and Google Apps Script
Before we dive into using APIs in Google Sheets, let's briefly understand APIs and Google Apps Script, the language used to interact with APIs.
An API is like a messenger that delivers requests and responses between applications. It defines the methods and formats required to communicate between software systems. Google Apps Script is a JavaScript-based scripting language built into the Google Apps platform, enabling you to automate tasks and manipulate data within Google Sheets.
Enable the Apps Script service
To start using APIs in Google Sheets, you'll first need to enable the Apps Script service. Here's how:
1. Click on Extensions in the menu, then Apps Script.
2. Click on Current project's triggers in the left-hand menu, then click Add Trigger.
3. In the Add Trigger dialog box, set the event source to From spreadsheet, choose the desired event type, and click Save.
Set up your project and fetch data
Now that the Apps Script service is enabled, let's set up a project and fetch data from an API. For this example, we'll use the JSONPlaceholder API, which provides fake data for testing and prototyping.
1. Click on Untitled project in the pane on the left, then click on Delete, and rename your project "APIExample".
2. In the editor, replace any existing code with the following function:
3. To execute the function, click on the(User Interface) button in the toolbar, then click Run, and select fetchData from the Function drop-down menu.
The Logger service will display the fetched data in the (View) > (Logs) menu. In this example, we've fetched data representing a JSON object (a single to-do item).
Changing the API data in your Google Sheets
Now that we've fetched data using an API, let's insert it into our Google Sheets.
In this section, we'll modify the fetchData function to write the fetched data into your Google Sheets. Before proceeding, clear any existing data in your Google Sheets and name the first five columns (A-E).
Write fetched data to Google Sheets
Modify the fetchData function as follows:
```javascript function fetchData() { var url = 'https://jsonplaceholder.typicode.com/todos/1'; var response = UrlFetchApp.fetch(url); var json = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.getRange('A2:E2').setValues([[json.userId, json.id, json.title, json.completed, json.updatedAt]]); } ```In this updated function, we're using the SpreadsheetApp service to access the active sheet and write the fetched data to the range A2:E2. After executing the function, you should see the fetched data in your Google Sheets.
Fetch and write multiple API entries
To fetch and write multiple API entries, you can modify the function to handle an array of data. Here's how to fetch and write the first 5 to-do items from the JSONPlaceholder API:
```javascript function fetchAndWriteData() { var url = 'https://jsonplaceholder.typicode.com/todos'; var response = UrlFetchApp.fetch(url); var data = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var range = 2; data.forEach(function(task) { sheet.getRange('A' + range + ':E' + range).setValues([[task.userId, task.id, task.title, task.completed, task.updatedAt]]); range++; }); } ```This function will fetch an array of to-do items and write them as rows in your Google Sheets, starting from row 2.
Now that you've learned how to use APIs in Google Sheets, you can harness the power of real-time data and automate workflows to boost productivity and efficiency. Embrace this newfound capability, and happy coding!