Home > database >  Why isn't the filter() method properly hiding unwanted rows?
Why isn't the filter() method properly hiding unwanted rows?

Time:07-15

For context, we have a spreadsheet template to organize all the different orders the company receives. The idea is that we just select the client and the quantity of each product, then we click the create quote sheet button on the custom menu. Everything is fine. The sheet is successfully copied with a unique reference code, the correct client details and the correct name. However the filtering of the products is giving me trouble. I tried using the filter() method however it simply does nothing. No errors or anything, it just doesn't filter. Here is what I think is the relevant code:

    // declare that the copy is now the active spreadsheet
    var sheetId = quoteName.getId();
    var openQuoteSheet = SpreadsheetApp.openById(sheetId);
    SpreadsheetApp.setActiveSpreadsheet(openQuoteSheet);  
    SpreadsheetApp.setActiveSheet(openQuoteSheet.getSheets()[0]);
    var allProducts = SpreadsheetApp.getActiveSheet();  

    // filter out the products that the client doesn't need
    var fullSheet = allProducts.getRange('D15:D48').getValues();
    Logger.log(fullSheet);
    fullSheet.filter(checkQuantity);
    openQuoteSheet = SpreadsheetApp.getActiveSpreadsheet();
    filteredProducts = fullSheet.filter(checkQuantity);
    function checkQuantity(amount){ return amount > 0;}
    Logger.log(filteredProducts);

CodePudding user response:

fullSheet is a 2 D array. Therefor each element of checkQuantity is a row of the array. I'm assuming you want to compare the first column of each row with 0.

Change:

function checkQuantity(amount){ return amount > 0;}

To:

function checkQuantity(amount){ return amount[0] > 0;}
  • Related