Search code examples
google-apps-scriptfull-text-search

Restrict TextFinder to a specific range: TextFinder startFrom not starting from


I'm trying to use TextFinder, but in my spreadsheet, I don't want to return the header row or column 1 in my results. I see that TextFinder has a "startFrom" method that's supposed to let you define "The cell range after which the search should start" ... but I can't seem to make it work.

var tf=ss
       .createTextFinder(text)
       .useRegularExpression(true)
       .ignoreDiacritics(true)
       .startFrom(ss.getRange("B2"));
var all=tf.findAll();

What am I doing wrong? I get results, they just still include matches from row 1 and column A.


Solution

  • Assuming ss is spreadsheet, you're creating a text finder for the entire spreadsheet. startFrom(range) only says

    The cell range after which the search should start

    If you use getCurrentMatch() or findNext() it'll return the next range matching your text criteria starting from that range(i.e., Sheet1!B2). If you want a textFinder specific to a range, then create the text finder on that range:

    var tf = ss
        .getSheetByName('Sheet1')
        .getRange('B2:B')
        .createTextFinder(text)