Friday, February 25, 2011

Code for Google Docs Portfolio


Update: this code doesn't work anymore. Google closed API access to quotes.

In addition to the yesterday's post, I'm publishing code. Use of code is free and unrestricted.


function colortickers() {
  // Color background of ticker columns depending on price and limits
  var price = 1;     // this will hold the answer
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = 2;
  while (price > 0) {
    price = sheet.getRange(row, 2).getValue();
    if (sheet.getRange(row, 2).getValue()) {
      price = sheet.getRange(row, 2).getValue();
      var low = sheet.getRange(row, 3).getValue();
      var high = sheet.getRange(row, 4).getValue();
      if (low > 0 && price < low) {
        sheet.getRange(row, 2).setBackgroundColor("red");
      } else if (high > 0 && price > high) {
        sheet.getRange(row, 2).setBackgroundColor("lightgreen");
      }
      else {sheet.getRange(row, 2).setBackgroundColor("white");}
    }
    row++;
  }
}


function gettickerprice(ticker) {
  // Function to get current price of stock
  var price = 0;     // this will hold the answer
  price = FinanceApp.getStockInfo(ticker).price
  return price;  // return the answer to the cell which has the formula
}

No comments: