Skip to main content

logoCorrectSize.png

Adaptive Insights LLC.

Import Data

Notice the lines below containing .dataSource.getSetting.  These lines are referencing the static text values and parameterized text from the Designer Settings tab.

function importData(context) {
   // Step 1: Make use of passed in contextual information to create a rowset object. Here, I am simply assigning tableId, maxRows and columnNames to variables to be used elsewhere in this script.
   var rowset = context.getRowset();
   rowset.setSmartParsingEnabled(true);
   var tableId = rowset.getTableId();
   var columns = rowset.getColumns();
   var columnRemoteIds = getColumnRemoteIds(columns);
   var dataSource = context.getDataSource();
   var googleKey = dataSource.getSetting("Google API Key").getValue();
   var uberKey = dataSource.getSetting("Uber Server Token").getValue();
   var startAddress = dataSource.getSetting("Start Address").getValue();
   var endAddress = dataSource.getSetting("End Address").getValue();
   var startGeoCode = convertAddressToGeoCode(startAddress, googleKey);
   var endGeoCode = convertAddressToGeoCode(endAddress, googleKey);
   if (tableId === 'prices') {
       // Step 2: Create a https request that will return data - in this case, Yahoo Finance will return JSON.
       var url = 'https://api.uber.com/v1/estimates/pr...tart_latitude=' + startGeoCode[0] + '&start_longitude=' + startGeoCode[1] + '&end_latitude=' + endGeoCode[0] + '&end_longitude=' +
           endGeoCode[1] + '&server_token=' + uberKey;
       var method = 'GET';
       var headers = {
           'Content-Type': 'text/json'
       };
       var body = '';
       // Step 3: Send https request and receive response. Normally you would want to check that the response contains a success message first before looking at the rows.
       var response = ai.https.request(url, method, body, headers);
       // Step 4: Parse the https response body.
       var data = parseData(response.getBody());
       var rows = parseRows(data);
       // Step 5: Process each row to extract the cell values for each column and add them as an array to the rowset in the expected column order.   
       addRows(rowset, rows);
   }
   if (tableId === 'times') {
       // Step 2: Create a https request that will return data - in this case, Yahoo Finance will return JSON.
       var url = 'https://api.uber.com/v1/estimates/ti...tart_latitude=' + startGeoCode[0] + '&start_longitude=' + startGeoCode[1] + '&server_token=' + uberKey;
       var method = 'GET';
       var headers = {
           'Content-Type': 'text/json'
       };
       var body = '';
       // Step 3: Send https request and receive response. Normally you would want to check that the response contains a success message first before looking at the rows.
       var response = ai.https.request(url, method, body, headers);
       // Step 4: Parse the https response body.
       var data = parseData(response.getBody());
       var rows = parseTimeRows(data);
       // Step 5: Process each row to extract the cell values for each column and add them as an array to the rowset in the expected column order.   
       addRows(rowset, rows);
   }
}


function getColumnRemoteIds(columns) {
   var columnIds = [];
  for (var i = 0; i < columns.length; i++) {
       columnIds.push(columns[i].getId());
   }
   return columnIds;
}


function parseData(responseJSON) {
   return JSON.parse(responseJSON);
}


function parseRows(json) {
   return json.prices;
}


function parseTimeRows(json) {
   return json.times;
}


function addRows(rowset, rows) {
   var columnRemoteIds = getColumnRemoteIds(rowset.getColumns());
   for (var i = 0; i < rows.length; i++) {
       var cells = [];
       var row = rows[i];
       for (var x = 0; x < columnRemoteIds.length; x++) {
           var propName = columnRemoteIds[x];
           cells.push(row[propName]);
       }
       rowset.addRow(cells);
   }
}
  • Was this article helpful?