Skip to content

Instantly share code, notes, and snippets.

@superstrong
Created November 19, 2016 02:13
Show Gist options
  • Save superstrong/bc60593321d46fa06774310b18ed41d0 to your computer and use it in GitHub Desktop.
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
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);
}
@linglung
Copy link

linglung commented Apr 8, 2018

for (i = 0; i < dataSet.length; i++) {
TypeError: Cannot read property "length" from undefined

@lucasfmsarmento
Copy link

I'm getting the same error as @linglung. Can anyone help?

@Yagar-ete
Copy link

Yagar-ete commented Nov 7, 2022

@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