0

I am trying to add the name of every individual "ticker" pulled from my spreadsheet to the "ownedticker" array, as well as update the quantity and total price of the stock in other arrays "ownedq" and "ownedtp", respectively.

However, this code doesn't work for some reason. Appreciate the help!

function pullTransactions() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var transactions = ss.getSheetByName("TransactionHistory"); //Transaction History

var rows = transactions.getDataRange();
var numRows = rows.getNumRows();

var tickers = transactions.getSheetValues(3, 3, (numRows-2), 1);
var ppss     = transactions.getSheetValues(3, 5, (numRows-2), 1);
var qs       = transactions.getSheetValues(3, 7, (numRows-2), 1);

var ownedticker = [];
var ownedq  = [];
var ownedtp = [];

for (var i = 0; i <= tickers.length; i++) {
    var ticker = tickers[i];
    var pps    = ppss[i];
    var q      = qs[i];
    
    var check = ownedq[i];
    
    for (var j = 0; j <= ownedticker.length; j++) {
      if (ownedticker[j]) {
        ownedq[j] += q;
        ownedtp[j] += (q*pps);
      }
    }
    
    if (check == ownedq[i]){
      ownedticker.push(ticker);
      ownedq.push(q);
      ownedtp.push(pps*q);
    }
  }
}
Rubén
  • 24,097
  • 9
  • 55
  • 116
  • What do you mean by `doesn't work`? Are you getting any error? Would you consider providing a copy of the spreadsheet, showing your desired output? – Iamblichus Nov 10 '20 at 09:26

1 Answers1

1

The reason is that getSheetValues returns an Array of Arrays were each inner array holds the values (number, string, Date, booleans) for each row and your code is comparing Arrays (not strings).

You could use Array.prototype.flat to convert the Array of Arrays into an Array or instead of a single bracketed index, i.e. tickers[i], you could use the a double bracketed index, i.e. tickers[i][0]

Related

Rubén
  • 24,097
  • 9
  • 55
  • 116