In this API instructional exercise for beginners, you'll figure out how to interface with APIs utilizing Google Apps Script, to recover information from an outsider and show it in your
10.7k
By Kate Angelou
In this API instructional exercise for beginners, you'll figure out how to interface with APIs utilizing Google Apps Script, to recover information from an outsider and show it in your Google Sheet.
IT will tells you the best way to utilize Google Apps Script to interface with a basic API to recover some information and show it in Google Sheets:
Prerequisites
The goal here is to take an in-depth look at deploying API's over google spreadsheet using script editor. To find script editor , go to tools > script editor
You will find a script editor console here, which will have a predefined function. On the right side, you will have the option of a file in which you can insert as many files as you want.
In addition to JavaScript, if you want to do a custom HTML, you will get the option.
Now let's see by assigning Api inside the function,
var url = "YOUR_API_URI"
var response = UrlFetchApp.fetch(url)
var data = response.getContentText()
Here I have created a custom function, you can use below methods for HTTP request, like Ajax or HTTP request of Javascript. Once you send request you will get the response which you can parse same as like javascript methods using
var result = JSON.parse(data);
This is where you need to console. You can use this function that will show you the response Logger.log(var_name);
Logger.log() is same as like console.log(), But here it won't depicts respond in your browser.
It will return result in a window which will be below at your editor named as Execution Log.
After parsing, you now have to link the spreadsheet in which to print the result. For which use below method
var ss = SpreadsheetApp.getActiveSheet();
Create a header if you want to assign a header, And append to spreadsheet
var headerRow = ["Title", "Impressions", "Clicks", "Revenue","Date","Time"] ss.appendRow(headerRow)
Now run the loop by printing the results you want.