Search code examples
google-sheetsgoogle-sheets-formulaline-breaksarray-formulasgoogle-sheets-query

How to remove extra line break/empty cell Google Sheets importrange/query


Here is my formula (I have edited the name of my sheet as I cannot allow access to it).

=QUERY(IMPORTRANGE("my sheet","Prices"),"select Col1 where Col4 contains '"& L1 &"' limit 1",1)

This is working perfectly for all entries except 1 of them where it is importing 2 cells (the first one blank, the second with the desired result). Since the cell below is filled it cannot overwrite it, and I don't want it to. What would cause this phantom empty cell and how do I prevent it from happening?


Solution

  • if the blank undesired cell is in the first column add and Col1 is not null like:

    =QUERY(IMPORTRANGE("my sheet", "Prices"), 
     "select Col1 
      where Col4 contains '"&L1&"'
        and Col1 is not null
      limit 1", 1)
    

    if you have actual line break issue use regex with \n like:

    =ARRAYFORMULA(REGEXREPLACE(QUERY(IMPORTRANGE("my sheet", "Prices"), 
     "select Col1 
      where Col4 contains '"&L1&"'
      limit 1", 1), "\n", ))