Google Sheets + Apps Script: create datalist only if column contain a specific word

Viewed 50

Hi just wondering if someone could help me modify this code (Thanks Tanaike) so it would search in Column 'G' a specific word then create a datalist from row that match the word in the variable specWord

  <div class="form-group col-md-5">
          <label for="test">test</label>
          <input type="text" id="test1" class="form-control" list="test" name="test" 
           onchange="test4()">
          <datalist id="test"  >
            <?
            var sheet   = SpreadsheetApp.getActive().getSheetByName("info");
            var specWord = "Apple"; /// Specific Word to look for
            var lastRow = sheet.getLastRow();
            var myRange = sheet.getRange("E2:G"+lastRow);
            var data    = myRange.getValues();
            for (var i = 0; i < data.length; ++i) { ?>
            <option value="<?!= data[i] ?>">
            <? } ?>
          </datalist>

so the datalist would be created from the 3 item matching "Apple" in column 'G'

enter image description here

Thanks

1 Answers

When you want to use the rows filtered with the value of specWord in the column "G", how about the following modification?

From:

var data = myRange.getValues();

To:

var data = myRange.getValues().filter(([,,g]) => g == specWord);

Note:

  • If above modification occurs an error, please modify it to var data = myRange.getValues().filter(function([,,g]) {return g == specWord});

Reference:

Related