Wednesday, 22 March 2017

Google Sheets and Google Script calling SAP HANA Express

Of course, you will need to create a Google Spreadsheet. Once you are there, you need to create a Google script. It sounds complicated, but it is as easy as going into Tools -> Script editor.

SAP HANA Tutoerials, SAP HANA Materials, SAP HANA Certifications

This will create a script that will be bound to your Google Spreadsheet and only to this Spreadsheet file. This means you will not see it as a separate executable script in your Google Drive and the behavior you program here will not show in other spreadsheets unless they are a copy of this Spreadsheet.

First, you need to create the custom menu. This is what I used for my spreadsheet:

function onOpen() {
//Adding the menu
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Get Employees', functionName: 'getEmployees'},
    {name: 'Get Expenses', functionName: 'getExpenses'},
    {name: 'Forecast', functionName: 'getForecast'},
  spreadsheet.addMenu('Expense Management', menuItems);

All of those functions are similar, so I will only go into the details of the most important one, the Forecast call, in a more generic fashion.

We want to create a brand new spreadsheet, so we skip validations of the current sheet and wheher it has data or not.  So this is how our function starts:

function getForecast() {
  var spreadsheet = SpreadsheetApp.getActive();
  if (!spreadsheet.getSheetByName("Forecast")){
      var foreSheet = spreadsheet.insertSheet("Forecast");
    } else {
      var foreSheet = spreadsheet.getSheetByName("Forecast");

We are now going to call the our REST API, parse the data and present it in the cells in our sheet:

var forecastURL = "https://host:port/HANAGoogleSheets/expenses.xsodata/Forecast?$format=json&$orderby=PERIOD";
var options = { 'contentType' : "application/json" }; //you may also need 'validateHttpsCertificates' : false if you haven't installed properly signed certificates

var request = UrlFetchApp.getRequest(forecastURL, options);
Logger.log(request); //This is like console.log()

var response = UrlFetchApp.fetch(forecastURL, options);
var dataAll = JSON.parse(response.getContentText());
var data = dataAll.d.results; //Check the structure 

//Appending the tile - yes, you could get this dynamically from the metadata
foreSheet.appendRow([ "Month", "Year", "Period", "Amount in USD", "Value Type" ]);

//Adding some format - you could also do this dynamically  with foreSheet.getLastColumn() and foreSheet.getLastRow();


//Looping and appending the data
  for (i in data){
    foreSheet.appendRow([ data[i].month, data[i].year, data[i].PERIOD, data[i].amount, data[i].VALUE_TYPE ]);

Simply because everybody likes charts and they aid data visualization, you may want to add this code, where range are the columns you want taken into account for your LINE chart.

  var chart = foreSheet.newChart()
     .setPosition(3, 7, 0, 0)


You could also create a PIE chart full of colors too!

SAP HANA Tutoerials, SAP HANA Materials, SAP HANA Certifications

No comments:

Post a Comment