Created
November 19, 2016 02:13
-
-
Save superstrong/bc60593321d46fa06774310b18ed41d0 to your computer and use it in GitHub Desktop.
Retrieve JSON data from the BaseCRM API and add it to a Google Sheet. This example retrieves all users. Stands on the shoulders of https://gist.github.com/varun-raj/5350595a730a62ca1954
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBaseUsers() { | |
var options = { | |
"contentType" : "application/json", | |
"headers" : { | |
"Accept": "application/json", | |
"Authorization": "Bearer <TOKEN>" | |
} | |
} | |
var response = UrlFetchApp.fetch("https://api.getbase.com/v2/users", options); | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheets = ss.getSheets(); | |
var sheet = ss.getSheetByName("baseUsers"); // specific sheet name; alternatively use ss.getActiveSheet() | |
var dataAll = JSON.parse(response.getContentText()); // | |
var dataSet = dataAll.items; // "items" is the key containing relevant objects | |
var rows = [], | |
data; | |
for (i = 0; i < dataSet.length; i++) { | |
data = dataSet[i]; | |
rows.push([data.data.id, data.data.name, data.data.email]); //your JSON entities here | |
} | |
// [row to start on], [column to start on], [number of rows], [number of entities] | |
dataRange = sheet.getRange(2, 1, rows.length, 3); | |
dataRange.setValues(rows); | |
} |
I'm getting the same error as @linglung. Can anyone help?
@linglung for anyone struggling item is the name of the key that contain all the objects in your JSON (not the same for all APIs). Or that the URL to fetch is not breaking line and not returning anything to data (so length indefined).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for (i = 0; i < dataSet.length; i++) {
TypeError: Cannot read property "length" from undefined