Google Apps Script - and why do I need it?

The last time I needed a single spreadsheet (changing over time) from time to time to generate the statistics in the second sheet. Because of that, not only was about the cyclical generation, but the analysis was important for me that they are wrought in colors, bold - just easy to read. I would add that that these sheets are holding in Google Drive.

I was able to rely on macros in MS Word, but I do not know them and do not hurry me to their knowledge. I thought it perfect for me in this situation would be JavaScript and intuition did not fail me. It turned out that the Google Cloud can access Google Apps Script, which written in help Google Apps Script is based upon JavaScript. Thus after a few moments I had the first version of my statistics, and the next sprint final release.
And now a simple example that will bring the possibility of this tool.

Start by creating a spreadsheet with the objective to calculate the profit from the sale of shares:

SymbolNumberBuySellProfit
PGE1001819-
LTS2003941-

Sheet ready, so here we go: Tools->Script Editor, we create the project, then the file with the script and finally write in Google Apps Script. We will do three things: calculate the profit for each stock, we will give the background color and bold the font.
function calcProfit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var outputData = [];
  var profitSum = 0;
  
  // Read data
  var inputData = sheet.getDataRange().getValues();
  for (var i = 1; i <= inputData.length - 1; i++) {
    var number = inputData[i][1];
    var buy = inputData[i][2];
    var sell = inputData[i][3];
    
    var profit = (sell - buy) * number;
    
    var outputValues = [];
    outputValues.push(profit);
    outputData.push(outputValues);
  }
  
  // Write data
  var outputRange = sheet.getRange(2, 5, outputData.length, 1);
  outputRange.setBackground("lightgray");
  outputRange.setFontWeight("bold");
  outputRange.setValues(outputData);
}
Run the script and get the result:

SymbolNumberBuySellProfit
PGE1001819100
LTS2003941400

Because the sheet and the script is in a cloud, it can be shared with others.

Komentarze

Popularne posty z tego bloga

Java ESL program for connecting to the FreeSWITCH

AngularJS example MotoAds more advanced than the tutorial on angularjs.org

Java program for connecting to the FreeSWITCH XML-RPC