Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Google query language #641

Open
hkalexling opened this issue Jan 1, 2019 · 3 comments
Open

Support for Google query language #641

hkalexling opened this issue Jan 1, 2019 · 3 comments

Comments

@hkalexling
Copy link

We can use Google Query Language to query the content of Google Sheets. It's a subset of SQL and I think it's much more flexible than the current _finder method. All we need to do is just sending a GET request to https://docs.google.com/spreadsheets/d/[id]/gviz/tq/tq=[query].

Here's a simple example where we query all the records in this test spreadsheet with age larger than 50:

import json
import requests

def query(q):
    parts = ['https://docs.google.com/spreadsheets/d/14xrSdZXaE0D83gjDXdyxjYttRSnhRGkz9a9qO_zip-E', '/gviz/tq']
    url = '/'.join(p.strip('/') for p in parts)
    res = requests.get(url, params={'tq': q}).text
    res = res.split('{', 1)[-1]
    res = '{' + res[::-1].split('}', 1)[-1][::-1] + '}'
    j = json.loads(res)

    if j.get('status') != 'ok':
        if j.get('errors'):
            raise QueryException(j.get('errors')[0].get('detailed_message'))
        else:
            raise QueryException(j.get('status'))

    table = []
    for r in j.get('table').get('rows'):
        row = []
        for c in r.get('c'):
            row.append(c.get('f') or c.get('v'))
        table.append(row)

    return table

print(query('select * where D > 50'))

The only drawback that I can think of is that we can only get the table content, but not the row/column indices, but I think it's good to have this option there for users need it.

@burnash If you think it's a good idea, I would be happy to submit a PR.

@burnash
Copy link
Owner

burnash commented Apr 17, 2020

I'm really sorry for the late reply. Yes, I think this is a good idea. Let me know if you are still interested in submitting a PR.

@ruchit2801
Copy link

ruchit2801 commented Apr 14, 2021

@burnash Has anyone submitted a PR for this? If not, can I work on it and send a PR?

@burnash
Copy link
Owner

burnash commented Apr 14, 2021

@ruchit2801 as far as I know, there's no PR for this. If you're interested, please go ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants