Skip to content

Instantly share code, notes, and snippets.

@nk-gears
Last active November 24, 2019 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nk-gears/34d740525bdf7f8b4a43db33ce6c787b to your computer and use it in GitHub Desktop.
Save nk-gears/34d740525bdf7f8b4a43db33ce6c787b to your computer and use it in GitHub Desktop.
Import Data from ITGlue using Google Apps Script
//Configs
var ITGLUE_END_POINT="https://api.eu.itglue.com";
var ITGLUE_API_KEY = "<TOKEN>"
function getITGlueHeaders(){
var actualKey=ITGLUE_API_KEY;
var headers = {
"Content-Type":"application/vnd.api+json",
"x-api-key": actualKey
};
return headers;
}
function getITGlueData(url){
var options = { "method":"GET",
"contentType" : "application/json",
"headers": getITGlueHeaders(),
"muteHttpExceptions": false,
};
var response = UrlFetchApp.fetch(url, options).getContentText();
var jsonResponse = JSON.parse(response);
return jsonResponse;
}
// Get Resources
function getOrgListByPage(pageNo) {
var resource = '/organizations'
var apiUrl=ITGLUE_END_POINT + resource + "?page[size]=50&page[number]=" + pageNo;
var jsonResponse = getITGlueData(apiUrl);
return jsonResponse
}
function getOrgList() {
var initialPage=1;
const fragment = getOrgListByPage(initialPage)
if (fragment.links.next) {
return fragment.data.concat(getOrgListFull(fragment.meta["next-page"]));
} else {
return fragment.data;
}
}
function importDataToSheet(dataSet,sheet,rowOffset) {
var rows = [];
var colArray=[];
for (i = 0; i < dataSet.length; i++) {
var data = dataSet[i];
var actualData=[];
for(colItem in data){
var colValue=data[colItem]
actualData.push(colValue);
if(i==0) colArray.push(colItem); // Sheet Column
}
rows.push(actualData);
}
dataRange = sheet.getRange(1, 1, 1, colArray.length); // 3 Denotes total number of entites
dataRange.setValues([colArray]);
var lastRow = sheet.getLastRow() + 1;
dataRange = sheet.getRange(rowOffset+2, 1, rows.length, colArray.length); // 3 Denotes total number of entites
dataRange.setValues(rows);
}
var sheetName="organizations";
var worksheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var dataSet=getOrgList();
//Transform the data
//write your tranformations here. This is just an example
var jsonData=dataSet.data.map(function(vOrg){
return {id:vOrg.id,name:vOrg.name};
});
importDataToSheet(jsonData,worksheet);
//repeat this for other resources
/*
sheetName="configurations";
worksheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
dataSet=getConfigurationList();
importDataToSheet(worksheet);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment