Search code examples
pythongoogle-sheetsgoogle-accountgspread

How to get Google Spreadsheet cell link with gspread


There is a single Google spreadsheet document. After selecting the very first A1 cell from a right-click pull-down menu I choose: Get Link to this Cell command.

The URL link is now stored in a memory and it can be pasted into any text editor. The copied cell's URL link would look like this:

https://docs.google.com/spreadsheets/d/f8s9HO0sidw9qIGwuqYq1wFFb9QWWpuFIXE3flYcgBG1/edit?ts=7559594f#gid=2879414198&range=A1

Question: How to get the the same cell's link using gspread in Python?


Solution

  • First you'll need to generate OAuth2 credentials: http://gspread.readthedocs.io/en/latest/oauth2.html

    You'll need to create a new authorization object in your Python using the OAuth2 credentials:

    gc = gspread.authorize(credentials)
    

    Then you need to create a connection to the worksheet:

    # If you want to be specific, use a key (which can be extracted from
    # the spreadsheet's url)
    yoursheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
    
    # Or, if you feel really lazy to extract that key, paste the entire url
    yoursheet = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')
    

    Then you can grab the cell you want

    val = yoursheet.acell('B1').value
    

    All code was pulled from the gspread Github basic usage with minor tweaks https://github.com/burnash/gspread

    Cheers!