Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get hyperlink from a cell in google Sheet api V4

I want to get the hyperlink of a cell (A1, for example) in Python. I have this code so far. Thanks

properties = {
    "requests": [
    {
        "cell": {
            "HyperlinkDisplayType": "LINKED"
        },
        "fields": "userEnteredFormat.HyperlinkDisplayType"
    }

    ]
    }

result = service.spreadsheets().values().get(
    spreadsheetId=spreadsheet_id, range=rangeName, body=properties).execute()
values = result.get('values', [])
like image 736
Max García Avatar asked Dec 23 '22 13:12

Max García


2 Answers

How about using sheets.spreadsheets.get? This sample script supposes that service of your script has already been able to be used for spreadsheets().values().get().

Sample script :

spreadsheetId = '### Spreadsheet ID ###'
range = ['sheet1!A1:A1'] # This is a sample.
result = service.spreadsheets().get(
    spreadsheetId=spreadsheetId,
    ranges=range,
    fields="sheets/data/rowData/values/hyperlink"
).execute()

If this was not useful for you, I'm sorry.

like image 70
Tanaike Avatar answered Jan 10 '23 16:01

Tanaike


It seems to me like this is the only way to actually get the link info (address as well as display text):

result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheetId, range=range_name, 
        valueRenderOption='FORMULA').execute()
values = results.get('values', [])

This returns the raw content of the cells which for hyperlinks look like this for each cell:

'=HYPERLINK("sample-link","http://www.sample.com")'

For my use I've parsed it with the following simple regex:

r'=HYPERLINK\("(.*?)","(.*?)"\)'
like image 27
Eran Avatar answered Jan 10 '23 15:01

Eran