Filter using wildcard

616 views
Skip to first unread message

Scott Bennett

unread,
Mar 4, 2021, 2:23:01 PM3/4/21
to Google Apps Script Community
Does anyone know why this is not working?  It works for specific values, but will not work when I try to use the wildcard. I am trying to make it so it does not return any thing that starts with AP.

function filterCourses() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let srcSheet = ss.getSheetByName('Data');
  let destSheet = ss.getSheetByName('Filtered Data')
  let srcSheetData = srcSheet.getRange(2,1,srcSheet.getLastRow(),srcSheet.getLastColumn()).getValues();
  let newArray = srcSheetData.filter(row=> row[5]!='AP*')
  destSheet.getRange(2,1,newArray.length,newArray[0].length).setValues(newArray)

Scott Bennett

unread,
Mar 4, 2021, 2:33:15 PM3/4/21
to Google Apps Script Community
I found a work around, but would like input about the wildcard not working
function filterCourses() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let srcSheet = ss.getSheetByName('Data');
  let destSheet = ss.getSheetByName('Filtered Data')
  let srcSheetData = srcSheet.getRange(2,1,srcSheet.getLastRow(),srcSheet.getLastColumn()).getValues();
  let newArray = srcSheetData.filter(row=> row[5].startsWith('AP')!=true)
  destSheet.getRange(2,1,newArray.length,newArray[0].length).setValues(newArray)
}

Martin Hawksey

unread,
Mar 4, 2021, 2:40:06 PM3/4/21
to Google Apps Script Community
Hi Scott,

No native wildcard filtering on Javascript arrays, but as you've found there are other ways to approach this. If you do want to use a wildcard you can use a regular expression. For more examples including using regex this is a good post on Stackoverflow https://stackoverflow.com/a/12696051

Best
Martin

Bennett, Scott

unread,
Mar 4, 2021, 2:44:04 PM3/4/21
to google-apps-sc...@googlegroups.com
Thank you Martin, I was stumped.  


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/06bcca6e-6eab-4da0-a309-f8ded41b8601n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages