简体   繁体   中英

How to compare strings in Google App Scripts

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);
    }
  }
}

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, ie tickers[i] , you could use the a double bracketed index, ie tickers[i][0]

Related

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM