Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppScript filter function not working for some values

updated: I have a spreadsheet with 3 columns that i am concerned with:

Group (Ferrous/Non-ferrous/etc.) RI No. (Unique identifier) Supplier (Sellers of chosen RI No.) The flow of data happens like this: I select Group (say, Ferrous) --> In RI No. column, an automatic dropdown is created, showing RI Nos. of all items under Ferrous category show up --> I select a particular RI No. from this drop-down --> A drop-down of all sellers selling that item show up. Now, I wrote a code which does this work perfectly, but it is unable make the list of Suppliers when I select an RI No. with a "-" in them. For example, from the RI No. list, if I select a value like "6181-1","Cu-18mm","CU-25x3","25_E.Cont","DOF", the Supplier drop-down list shoes zero options to choose from, even though it is expected to show 2 options for it. This problem does not occur for other values, like "6310-1" or even "TTK1 & TTK2" selected in the RI No. list.

Sample Spreadsheet https://docs.google.com/spreadsheets/d/1Xvz1NWiUqMWXik5k653slQhAXduwuQmq3_n0aJEhbRY/edit#gid=1605627126

Script:

var ws = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var wsopt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sorted items");
var opts = wsopt.getRange(2,1,wsopt.getLastRow()-1,13).getValues();
var firstlevelcolumn = 2;
var secondlevelcolumn = 5;
var thirdlevelcolumn= 6;
var wsopt2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("supplier details");
var opts2 = wsopt2.getRange(3,1,wsopt2.getLastRow()-1,12).getValues();


function onEdit(e)
{ var activecell= e.range;
  var val = activecell.getDisplayValue();
  var r= activecell.getRow();
  var c= activecell.getColumn();
  var wsName= activecell.getSheet().getName();
  if (wsName == "JPS")
  {
    if (wsName == "JPS" && c == firstlevelcolumn && r > 2)
    { applyFirstLevelValidation(val,r);}
    if (wsName == "JPS" && c == secondlevelcolumn && r > 2)
    { applySecondLevelValidation(val,r);}
  }//JPS
  else if (wsName == "DDNI")
  {
    if (wsName == "DDNI" && c == firstlevelcolumn && r > 2)
    {applyFirstLevelValidation(val,r);}
    if (wsName == "DDNI" && c == secondlevelcolumn && r > 2)
    { applySecondLevelValidation(val,r);}
  }//DDNI
}//close onEdiT

function applyFirstLevelValidation(val,r)
{
  if(val=== "")
    {
      ws.getRange(r,secondlevelcolumn).clearContent();
      ws.getRange(r,secondlevelcolumn).clearDataValidations();
      ws.getRange(r,thirdlevelcolumn).clearContent();
      ws.getRange(r,thirdlevelcolumn).clearDataValidations();
    } else
    {
      ws.getRange(r,secondlevelcolumn).clearContent();
      ws.getRange(r,secondlevelcolumn).clearDataValidations();
      ws.getRange(r,thirdlevelcolumn).clearContent();
      ws.getRange(r,thirdlevelcolumn).clearDataValidations();
      var filteredopts = opts.filter(function(o){return o[0]===val});
      var listToApply = filteredopts.map(function(o){return o[1]});
      var cell=ws.getRange(r,secondlevelcolumn);
      applyValidationToCell(listToApply,cell);
    }//close else
}//close applyFirstLevelValidation

function applySecondLevelValidation(val,r)
{
  if(val=== "")
    {
      ws.getRange(r,thirdlevelcolumn).clearContent();
      ws.getRange(r,thirdlevelcolumn).clearDataValidations();
    } else
    {
      ws.getRange(r,thirdlevelcolumn).clearContent();
      ws.getRange(r,thirdlevelcolumn).clearDataValidations();
      var firstlevelcolvalue = ws.getRange(r,firstlevelcolumn).getDisplayValue();
      var filteredopts2 = opts2.filter(function(o){return o[6]===firstlevelcolvalue && o[4]===val});
      var listToApply2 = filteredopts2.map(function(o){return o[0]});
      var cell2=ws.getRange(r,thirdlevelcolumn);
      applyValidationToCell(listToApply2,cell2);
    }//close else
}

function applyValidationToCell(list,cell)
{
  var rule = SpreadsheetApp.newDataValidation().requireValueInList(list).build();
  cell.setDataValidation(rule);
} ```
like image 762
Anushka Avatar asked May 05 '26 14:05

Anushka


1 Answers

Welcome!

I reviewed the code, and I was not able to find any issues with it. However, I made 2 copies to test, and in one of the copies I remove all the spaces in the "supplier details" from this:

enter image description here

To this:

enter image description here

And that seems to fix the issue: enter image description here

I'm guessing that the script cannot read the information properly when there are multiple spaces between the rows.

Update:

I modified the code to see the values passed by your filter:

var filteredopts2 = opts2.filter(function(o){return o[6]===firstlevelcolvalue && o[4]===val});

And I noticed that the filter was comparing Non Ferrouse from the JPS tab with Non- Ferrouse in the supplier details tab.

enter image description here

After changing all the values from the supplier details from:

enter image description here

To:

enter image description here

The issue got fixed.

like image 86
Giselle Valladares Avatar answered May 07 '26 18:05

Giselle Valladares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!