Skip to content

Instantly share code, notes, and snippets.

@superstrong
Created November 19, 2016 02:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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);
}
@mathiasrw
Copy link

Instead of having a predefined sheet name, just return rows after the for-loop so the user can descide where the data should be located...

@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