Last active
November 24, 2019 04:17
-
-
Save nk-gears/34d740525bdf7f8b4a43db33ce6c787b to your computer and use it in GitHub Desktop.
Import Data from ITGlue using Google Apps Script
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
//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