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);
}
function getForecast() {
var spreadsheet = SpreadsheetApp.getActive();
if (!spreadsheet.getSheetByName("Forecast")){
var foreSheet = spreadsheet.insertSheet("Forecast");
} else {
var foreSheet = spreadsheet.getSheetByName("Forecast");
foreSheet.clear({contentsOnly:true});
}
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();
foreSheet.getRange("A1:E1").setFontWeight("bold");
//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 ]);
}
var chart = foreSheet.newChart()
.setChartType(Charts.ChartType.LINE)
.addRange(range)
.setPosition(3, 7, 0, 0)
.build()
foreSheet.insertChart(chart);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
29 | |
13 | |
12 | |
10 | |
9 | |
9 | |
9 | |
7 | |
7 | |
6 |