1

I have a solution for filtering on this question.

This works perfectly with a column that has string values. When I try to filter a column with numeric values it will not work. I'm assuming it is because .setHiddenValues() will not accept numeric values. I could be wrong.

Let me explain my scenario:

The user inputs a value on an HTML interface, let's say 6634. The HTML calls my function on .gs and passes the numeric value the user inputted.

google.script.run                                                                            //Executes an Apps Script JS Function
 .withSuccessHandler(updateStatus)                                                           //function to be called upon successfull completion of Apps Script function
 .withFailureHandler(failStatus)
 .withUserObject(button)                                                                     //To pass the event element object
 .projectSearch2(projectID);           //Apps Script JS Function
return;

The function (on the linked question above) will take that value and bump it up against the values in a column deleting the value if it is found. What I am left with is an array of values that I do not want filtered.

function projectSearch2(projectID){
    var ss = SpreadsheetApp.getActive();
    var monthlyDetailSht = ss.getSheetByName('Data Sheet');
    var monLastCN = monthlyDetailSht.getLastColumn();
    var monLastRN = monthlyDetailSht.getLastRow();    
    var data = monthlyDetailSht.getRange(1,1,1,monLastCN).getValues();//Get 2D array of all values in row one
    var data = data[0];//Get the first and only inner array
    var projectIDCN = data.indexOf('Project Id') + 1;

    //Pull data from columns before filtering
    var projectIDData = monthlyDetailSht.getRange(2,projectIDCN,monLastRN,1).getValues();

    //Reset filters if filters exist
    if(monthlyDetailSht.getFilter() != null){monthlyDetailSht.getFilter().remove();}

    //Start Filtering
    var projectIDExclCriteria = getHiddenValueArray(projectTypeData,projectID); //get values except for    
    var rang = monthlyDetailSht.getDataRange();
    var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues(projectIDExclCriteria).build();//Create criteria with values you do not want included.
    var filter = rang.getFilter() || rang.createFilter();// getFilter already available or create  a new one

    if(projectID != '' && projectID != null){
      filter.setColumnFilterCriteria(projectIDCN, projectIDFilter);
    }


};

function getHiddenValueArray(colValueArr,visibleValueArr){
  var flatUniqArr = colValueArr.map(function(e){return e[0];})
  .filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
  return flatUniqArr;
}

That array is used in .setHiddenValues() to filter on the column. Nothing is filtered however. This works for all columns that contain string values, just not columns with numeric values. At this point I'm lost.

Attempted Solutions:

  • Convert user variable to string using input = input.toString(). Did not work.
  • manually inserted .setHiddenValues for projectIDExclCriteria. Like this: var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues([1041,1070,1071,1072]).build(); That succeeded so I know the issue is before that.
  • Step before was calling getHiddenValueArray. I manually inserted like so: var projectIDExclCriteria = getHiddenValueArray(projectIDData,[6634]); It is not working. Is the issue with that getHiddenValueArray function not handling the numbers properly?

Here is a solution. Changing the following:

.filter(function(e,i,a){return (a.indexOf(e.toString())==i &&    visibleValueArr.indexOf(e.toString()) ==-1); })

To:

.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); })  

That works! Thank you Tanaike. The next question is will this impact columns that are not numeric. I have tested that and it works as well.

pnuts
  • 58,317
  • 11
  • 87
  • 139
DanCue
  • 619
  • 1
  • 8
  • 17
  • 1
    I think that about ``a.indexOf(e.toString())``, if ``e`` is number and ``e`` is included in ``a``, ``a.indexOf(e.toString())`` is ``-1`` by ``e.toString()``. So how about trying to modify to ``return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1)``? But I'm not sure about the detail values. So if this ``getHiddenValueArray()`` still didn't work, in order to confirm your issue, can you provide a sample value of "colValueArr" and "visibleValueArr"? And if there is the issue at ``setColumnFilterCriteria()``, can you provide a sample spreadsheet? – Tanaike Aug 01 '18 at 00:33
  • 2
    Or if you want to use ``return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1)``, how about trying to modify from ``colValueArr.map(function(e){return e[0];})`` to ``colValueArr.map(function(e){return e[0].toString();})``? – Tanaike Aug 01 '18 at 00:38
  • @Tanaike Your recommendation in your first comment worked. I have updated my question to include the solution. Do you want to submit an answer so I can choose it? – DanCue Aug 01 '18 at 11:56
  • Thank you for replying it. I posted it as an answer. Could you please confirm it? – Tanaike Aug 01 '18 at 23:12
  • Possible duplicate of [How to filter an array of arrays](https://stackoverflow.com/questions/40849369/how-to-filter-an-array-of-arrays) – Rubén Feb 18 '19 at 18:20

1 Answers1

3

How about this modification?

From :

.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })

To :

.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); }) 

Note :

  • In this modification, the number and string can compared using each value.
  • If you want to use return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1), you can achieve it by modifying from colValueArr.map(function(e){return e[0];}) to colValueArr.map(function(e){return e[0].toString();}).
    • In this modification, colValueArr.map(function(e){return e[0].toString();}) converts the number to string, so the number is used as a string.

Reference :

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I can confirm this works with both numeric and string data. Question though. If I would like to search for the word "apple" in a column that has phrases such as "I ate an apple" this will not return that row. How can I get it to work that way? – DanCue Aug 02 '18 at 15:09
  • also, is there a negative to using either of these over the other? – DanCue Aug 02 '18 at 15:14
  • 1
    @Danilo Cuellar Thank you for your response. I'm really sorry for the inconvenience. Although I read your question again, I cannot understand about your other 2 issues from your reply. So in order to correctly understand about your issues, can you provide the sample situation including the input and output values you want as a new question? By this, it will help many users including me think of the solution. I would like to solve your issues. But the reason that I cannot understand about your other 2 issues is my poor skill. I'm really sorry for this situation. – Tanaike Aug 03 '18 at 00:08
  • how can I edit this to handle case sensitivity? I went with `colValueArr.map(function(e){return e[0].toString();})` approach but it does not capture if it looks for "apple" in "I ate an Apple" – DanCue Aug 03 '18 at 13:14
  • Here is a [new question](https://stackoverflow.com/questions/51673945/search-array-of-strings-that-have-commas-within) as requested. – DanCue Aug 03 '18 at 13:41
  • 1
    @DanCue Thank you for your response. I saw it just now. An answer has already been posted. I think that the answer is a good solution. – Tanaike Aug 03 '18 at 23:25
  • Yes, I liked the solution. I'm going to post another questions for something similar. Hopefully someone with the same requirements will stumble on these questions and find them useful. – DanCue Aug 03 '18 at 23:31
  • I just did a thorough review of the results and it is not returning all of the expected values. `colValueArr` has 943 values while `visibleValueArr` has 288. This is supposed to return the 655 `colValueArr` values that are not in `visibleValueArr`. It's only returning 467. Any thoughts on why? – DanCue Aug 10 '18 at 15:56
  • @DanCue I'm really sorry for my poor English skill. I couldn't understand what you want to do. Can you provide more detail information? – Tanaike Aug 10 '18 at 23:19
  • I think I figured it out. I had an issue with numbers missing. It's resolved now. Thank you. – DanCue Aug 11 '18 at 02:56
  • @DanCue Thank you for replying it. I'm sorry I couldn't help. – Tanaike Aug 11 '18 at 06:21