Search code examples
google-sheetsgoogle-query-language

Using google query to download parts of a published sheet


This works:

curl 'https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/pub?gid=911257845&single=true&output=csv'

however I want to only pick up rows where count > 300.

The query before encoding would be

select * where F > 300

After encoding

select%20*%20where%20F%3E300

So the url becomes

https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/pub?gid=911257845&output=csv&tq=select%20*%20where%20F%3E300

The line above works retrieves a file, but it returns the whole file, and doesn't filter.

Note that a published web sheet has the form

https://docs.google.com/spreadsheets/d/e/KEY/pub?gid=GID

https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/pub?gid=911257845

This works. Adding &output=csv to it (no space before the &) works, and it downloads as a csv file. This opens in excel and shows the data in the table.

I tried this:

https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/pub?gid=911257845&output=csv&tq=select%20*%20where%20F%3E%20300

and

https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/gviz/tq?gid=911257845&output=csv&tq=select%20*%20where%20F%3E300

and get errors -- resource not available.

The page above should be public for people who want to try.

This may be an issue between publishing a sheet, and sharing a whole spread sheet to anyone who has the link.

I've created a new page that uses importrange() that slurps up the page from the main sheet, and that one is public.

https://docs.google.com/spreadsheets/d/1-lqLuYJyHAKix-T8NR8wV8ZUUbVOJrZTysccid2-ycs/edit?usp=sharing


Solution

  • How about this modification?

    Modification points :

    • When it uses query, please use like https://docs.google.com/spreadsheets/d/### file ID ###/gviz/tq?gid=###&tq=### query ###.
    • When select%20*%20where%20%F%3E300 is decoded, it is select * where %F>300.
      • select * where F > 300 is select%20%2a%20where%20F%20%3e%20300.
    • In order to output CSV, please use tqx=out:csv.
    • Please share the Spreadsheet.
      • On Google Drive
        • On the Spreadsheet file
        • right-click -> Share -> Advanced -> Click "change" at "Private - Only you can access"
        • Check "On Anyone with the link"
        • Click "Save"
        • At "Link to share", copy URL.
          • Retrieve file ID from https://docs.google.com/spreadsheets/d/### file ID ###/edit?usp=sharing

    Modified curl command :

    curl 'https://docs.google.com/spreadsheets/d/### file ID ###/gviz/tq?gid=911257845&tq=select%20%2a%20where%20F%20%3e%20300&tqx=out:csv'
    

    Reference :

    If I misunderstand your question, I'm sorry.

    Edit :

    The following 2 URLs are the comparison between your URL and my answer. The URL of my answer was matched to your URL.

    1. Your URL

    https://docs.google.com/spreadsheets/d/e/2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc/gviz/tq?gid=911257845&output=csv&tq=select%20*%20where%20F%3E300
    

    When above URL is separated,

    1. https://docs.google.com/spreadsheets/d/e/
      • e/ is not required.
    2. 2PACX-1vS3iBtVf4i_won5zAN9NGPqhcd6CcTb-4QHxpisSjCmlgV95B6mFmZvtMaC9GPvD7m8kD-6XLkVAhfc
      • This is not the file ID of spreadsheet.
    3. /gviz/tq
    4. gid=911257845
    5. output=csv
    6. tq=select%20*%20where%20F%3E300

    2. In my answer matched to your URL

    https://docs.google.com/spreadsheets/d/### file ID ###/gviz/tq?gid=###&tqx=out:csv&tq=### query ###
    

    When above URL is separated,

    1. https://docs.google.com/spreadsheets/d/
    2. ### file ID ###
      • You can see the detail of the file ID of spreadsheet at here.
    3. /gviz/tq
    4. gid=###
      • You can use gid=911257845.
    5. tqx=out:csv
      • This has to be used instead of output=csv.
    6. tq=### query ###
      • You can use tq=select%20*%20where%20F%3E300.

    Note :

    • Each number corresponds.
    • And please share the Spreadsheet as follows. This is difference from "Publish to the web" on Spreadsheet.
      • On Google Drive
        • On the Spreadsheet file
        • right-click -> Share -> Advanced -> Click "change" at "Private - Only you can access"
        • Check "On Anyone with the link"
        • Click "Save"
        • At "Link to share", copy URL.