Search code examples
xpathgoogle-sheetsarray-formulasgoogle-sheets-querygoogle-sheets-formula

Google Sheets ImportXML - Insert dynamic query if cell is filled


I'm using Google Sheets as a scraper for youtube videos. I would like to make the sheet even more dynamic, but it doesn't seem to work.

For example, I would like to write "How to get my baby to sleep" as a search query, therefore I would write this query into H6. If H6 is filled now, I would like to dynamically insert this query into the importXML function.

This is what I've already had, but I can't get this to work.

=ARRAYFORMULA("https://www.youtube.com"&QUERY(QUERY(UNIQUE(
 IMPORTXML("https://www.youtube.com/results?search_query=**{{dynamicInsertion}}**","//a/@href")),
 "where Col1 contains '/watch?v='"),"limit 50"))

Solution

  • try:

    =ARRAYFORMULA("https://www.youtube.com/"&QUERY(IMPORTXML(
     "https://www.youtube.com/results?search_query="&H6, "//a/@href"), 
     "where Col1 matches '/channel.+|/watch.+|/user.+|/results.+' 
      order by Col1 desc"))
    

    enter image description here


    UPDATE:

    =ARRAYFORMULA("https://www.youtube.com/"&QUERY(UNIQUE(IMPORTXML(
     "https://www.youtube.com/results?search_query="&H6, "//a/@href")), 
     "where Col1 matches '/channel.+|/watch.+|/user.+|/results.+' 
      order by Col1 desc limit 50"))
    

    0