Querying specific Google Sheets worksheet via URL, SQL-style - google-sheets

The Google sheet at the URL below contains two worksheets, Sheet1, Sheet2.
Google sheet:
https://docs.google.com/spreadsheets/d/1pgdF1hoid8m1Zj3KburwjZRGkydLh1Sum4DshUMaIeo/edit#gid=0
When I use the Query URL #1 below, it successfully returns a JSON file with the correct result for the query:
SELECT B where A contains "nut"
(the result being the string "crunch")
Query URL #1:
https://docs.google.com/spreadsheets/d/1pgdF1hoid8m1Zj3KburwjZRGkydLh1Sum4DshUMaIeo/gviz/tq?tq=SELECT%20B%20where%20A%20contains%20%22nut%22
Please note that the worksheet "sheet2" contains a different table as you can see at:
https://docs.google.com/spreadsheets/d/1pgdF1hoid8m1Zj3KburwjZRGkydLh1Sum4DshUMaIeo/edit#gid=1652705509
If I try to query that worksheet with the Query URL #2 shown below, it does not work (it should return a json file with the result "raccoon"). Instead of returning that json file, it only displays that worksheet. What am I doing wrong?
Thank you.
Query url #2:
https://docs.google.com/spreadsheets/d/1pgdF1hoid8m1Zj3KburwjZRGkydLh1Sum4DshUMaIeo/edit#gid=1652705509/gviz/tq?tq=SELECT%20B%20where%20A%20contains%20%22bird%22

The issue is /edit#gid=1652705509/ in the query string.
To target Sheet2, do this:
https://docs.google.com/spreadsheets/d/1pgdF1hoid8m1Zj3KburwjZRGkydLh1Sum4DshUMaIeo/gviz/tq?tq=SELECT%20B%20where%20A%20contains%20%22bird%22&sheet=Sheet2
Note the end parameter &sheet=Sheet2.

Related

GoogleSheet IMPORTHTML Link in Button

I use a googlesheet and IMPORTHTML to get the content of this page :
https://meteor.dsac.fr/documentation.php
It works fine, except the last column : the return value is "Consulter", however I would like to get the link instead (eg: https://meteor.dsac.aviation-civile.gouv.fr/meteor-externe/#communication/18280)
Is there a way to do that with that function?
Thanks
In the current stage, IMPORTHTML cannot retrieve the attribute of the tag. So, for example, how about the following sample formula?
Sample formula:
=QUERY({IMPORTHTML("https://meteor.dsac.fr/documentation.php","table",1),{"";IMPORTXML("https://meteor.dsac.fr/documentation.php","//td[4]/a/#href")}},"SELECT Col1,Col2,Col3,Col5")
In this sample formula, in order to retrieve the URLs instead of the value of "Consulter", IMPORTXML is used.
Testing:
When this formula is used, the following result is obtained.
References:
IMPORTHTML
IMPORTXML

Returning value of an item price in google sheets

I'm trying add easy updating prices into a google sheet.
I need the market price from
//*[#id="app"]/div/section[2]/section/div[1]/section[3]/div/section[1]/ul/li[1]/span[2]
https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English
I need it to display just the one number from the XPath to a cell, and I can't seem to figure out where I am going wrong. I've been using the IMPORTXML function and it won't return a value.
=IMPORTXML(A2,"//*[#id='app']/div/section[2]/section/div[1]/section[3]/div/section[1]/ul/li[1]/span[2]")
where A2 is the URL.
In your situation, it seems that the value of the market price cannot be directly retrieved from the URL of https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English. But, fortunately, it seems that that value can be directly retrieved from the endpoint of API. So, how about the following sample formula?
Sample formula:
=REGEXEXTRACT(JOIN(",",IMPORTDATA(A1)),"marketPrice:(.+?),")*1
or
=REGEXEXTRACT(QUERY(TRANSPOSE(IMPORTDATA(A1)),"WHERE Col1 matches 'marketPrice.+'"),"marketPrice:(.+)")*1
The cell "A1" has the URL of https://mpapi.tcgplayer.com/v2/product/242811/details.
In the case of https://www.tcgplayer.com/product/242811/pokemon-celebrations-celebrations-elite-trainer-box?Language=English, please use 242811 from the URL to the endpoint of API like https://mpapi.tcgplayer.com/v2/product/242811/details.
Result:
Note:
The value from the URL is JSON data. In this case, the following custom function can be also used. In this case, please copy and paste the following script to the script editor of Spreadsheet and save the script. And please put a custom function of =SAMPLE("url") to a cell.
const SAMPLE = url => JSON.parse(UrlFetchApp.fetch(url).getContentText()).marketPrice;
References:
IMPORTDATA
REGEXEXTRACT
Custom Functions in Google Sheets
it's not possible to scrape JS content into google sheets:

Google Sheets importxml query to remove one piece of data

I'm using the following query in my sheet to import total Spotify streams for artists. Example:
=IMPORTXML("https://chartmasters.org/spotify-streaming-numbers-tool/?artist_name=&artist_id=1uh2pZRWuOebEoQgFVKK7l&displayView=Disco","//tr[#class='careerTotals'][2]")
However it's returning one extra value I don't want ("EAS"). I would like to just have the artist name in A and the total streams in B. Any ideas? Thanks.
How about these modifications?
Modified formula:
=TRANSPOSE(IMPORTXML(A1,"//tr[#class='careerTotals'][2]/td[position()<3]"))
or
=QUERY(IMPORTXML(A1,"//tr[#class='careerTotals'][2]"),"SELECT Col1,Col2")
The URL of https://chartmasters.org/spotify-streaming-numbers-tool/?artist_name=&artist_id=1uh2pZRWuOebEoQgFVKK7l&displayView=Disco is put in the cell "A1".
At 1st modified script, the expected values are retrieved with xpath of //tr[#class='careerTotals'][2]/td[position()<3] and those are put to the columns using TRANSPOSE.
At 2nd modified script, the expected values are retrieved from the retrieved 3 values using QUERY.
Result:
This result is from the 1st modified formula. 2nd one is also the same result.
References:
TRANSPOSE
QUERY

How to reference a different spreadsheet in HLOOKUP?

I try to reference another spreadsheet in the HLOOKUP formula but fail. I'm doing this like:
=HLOOKUP(A2,"https://docs.google.com/spreadsheets/d/1qOzV/","report!A2:B2",2,0)
where http... is the URL of another spreadsheet, report!... is the name of the tab in another spreadsheet and the data range.
This kind of referencing of another spreadsheet's URL works with =importrange("https://docs.google.com/spreadsheets/d/1qOzV/","report!A2:B2"), as I realized from different examples.
But with HLOOKUP while doing so I get an error:
Wrong number of arguments to HLOOKUP. Expected between 3 and 4 arguments, but got 5 arguments.
It seems, that the spreadsheet URL is counted as separate parameter.
How do I correctly reference the URL, the sheet name and the data range?
PS: I tried to chain HLOOKUP and importrange, as mentioned in https://stackoverflow.com/a/39305031/1992004 - but fail on it, got just #REF.
My formula was
=HLOOKUP(A2,IMPORTRANGE("https://docs.google.com/spreadsheets/d/1qOzV"; "report!$A$2:$B$2"),2,0)
you are searching the content of cell A2 against imported range A2:B2 and if the match is found you returning the 1st row (of the whole dataset) of imported range after match eg. that's cell B2 of report!$A$2:$B$2. in other words range A2:B2 is one row so you cant return 2nd row from the one-row matrix, therefore valid formula is:
=HLOOKUP(A2,IMPORTRANGE("1I69LQ37hA5NjjJmuwmWkrDI_daiU_cnYDrUBpMm","report!$A$2:$B$2"),1,0)

Import selected rows with query on importrange

I'm trying to import select rows from a Google spreadsheet based on the value of a single cell in each row.
As such, I'm using the following:
=query(IMPORTRANGE("KEY","Form Responses 1!A:L"), "select * where J contains 'DENIED' ")
Wherein the KEY is an actual spreadsheet Key. I tested the importrange part, that is without the query, to confirm it works. It does. Furthermore, within the Google Spreadsheet itself I can query the sheet and get it to work.
The error I receive is:
#VALUE Error Unable to parse query string for query parameter 2: NO_COLUMN_J
(There is a column J.)
When you use an importrange as a dataset, you need to refer to the columns by number rather than letter. The formula also works without 'select *'. Try:
=query(IMPORTRANGE("KEY","Form Responses 1!A:L"),"where Col10 contains 'DENIED'")

Resources