The documentation for the hyperlink field is "A hyperlink this cell points to, if any. This field is read-only. (To set it, use a =HYPERLINK formula.)". You're still setting the hyperlink field (although you're attempting to set it to a formula). That won't work, because the field is read only. To set a formula, set a value in userEnteredValue.formulaValue. That will set a formula on the server, and the hyperlink field will be populated as a result.
Answer from Sam Berlin on Stack OverflowThe documentation for the hyperlink field is "A hyperlink this cell points to, if any. This field is read-only. (To set it, use a =HYPERLINK formula.)". You're still setting the hyperlink field (although you're attempting to set it to a formula). That won't work, because the field is read only. To set a formula, set a value in userEnteredValue.formulaValue. That will set a formula on the server, and the hyperlink field will be populated as a result.
You can also use 'USER_ENTERED' if using batchUpdate:
sheets.spreadsheets.values.batchUpdate({
spreadsheetId,
valueInputOption: 'USER_ENTERED',
requestBody: {
data:[
range: *your range*
values:[['=HYPERLINK("google.com", "ciao")']]
],
},
})
This way you basically put there the formula and the api interprets as if the user entered the formula
I figured it out:
def addHyperlink(self, hyperlink, text, sheetId, rowIndex, colIndex):
requests = []
requests.append({
"updateCells": {
"rows": [
{
"values": [{
"userEnteredValue": {
"formulaValue":"=HYPERLINK({},{})".format(hyperlink, text)
}
}]
}
],
"fields": "userEnteredValue",
"start": {
"sheetId": sheetId,
"rowIndex": rowIndex,
"columnIndex": colIndex
}
}})
body = {
"requests": requests
}
request = self.service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body)
return request.execute()
links not worked!!! But add like note)
def addHyperlink(self, uri_1, summa_, sheetId, rowIndex, colIndex):
requests = []
requests.append({
"updateCells": {
"rows": [{
"values": [{
'userEnteredValue': {'numberValue': float(summa_)}, #
'effectiveValue': {'numberValue': float(summa_)},
'formattedValue': "р."+summa_,
'userEnteredFormat': {
'numberFormat': {'type': 'NUMBER', 'pattern': '[$р.-419]#,##0.00'},
'backgroundColor': {'red': 1, 'green': 1, 'blue': 0.6}, 'borders': {
'top': {
'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
'bottom': {
'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
'left': {
'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}},
'right': {
'style': 'SOLID', 'width': 1, 'color': {}, 'colorStyle': {'rgbColor': {}}}},
'horizontalAlignment': 'RIGHT', 'verticalAlignment': 'BOTTOM',
'textFormat': {
'foregroundColor': {'red': 0.06666667, 'green': 0.33333334, 'blue': 0.8},
'fontFamily': 'Arial', 'underline': True,
'foregroundColorStyle': {'rgbColor': {'red': 0.06666667, 'green': 0.33333334, 'blue': 0.8}},
'link': {'uri': uri_1}
},
'hyperlinkDisplayType': 'LINKED','backgroundColorStyle': {'rgbColor': {'red': 1, 'green': 1, 'blue': 0.6}}
}
, 'hyperlink': uri_1 , 'note': uri_1,
}
]
}], "fields": '*', "start": {
"sheetId": sheetId, "rowIndex": rowIndex, "columnIndex": colIndex}}})
body = {
"requests": requests}
request = self.service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body)
return request.execute()
How to Get Hyperlinks Using Google Sheets API v4 - Stack Overflow
How to Insert hyperlink in a spreadsheet file using Google Spreadsheet API - Stack Overflow
python - Get hyperlink from a cell in google Sheet api V4 - Stack Overflow
node.js - Extract url from hyperlink using google sheets api - Stack Overflow
Videos
Thanks for looking.
I am trying to read the hyperlinks from a variety of cells in my sheet. I have seen a variety of answers (here and here) to this question using the following syntax, but I can't get it to work.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json",scope)
client = gspread.authorize(creds)
service = discovery.build('sheets', 'v4', credentials=creds)
sheetID = "Sealed League Score Sheet"
RANGE_NAME = ['Score Sheet!D3:D3']
FIELDS = "sheets/data/rowData/values/hyperlink"
request = service.spreadsheets().get(spreadsheetId = sheetID, ranges = RANGE_NAME, includeGridData = 'true', fields = FIELDS)
response = request.execute() I just started using Sheets today so I could be missing some obvious core element required to actually perform these actions. For example, my suspicion is that the service
declaration doesn't make any sense and is the root of the problem, but I've been struggling to find any information on why you even need it. I get an HTTP 404 error in my console, which I'm assuming means it's not even looking in the right place to begin with
I was able to write and read to my spreadsheet using methods like playerName = client.open(sheetID).worksheet("Score Sheet").cell(1,1)
, but the Hyperlink issue has me seriously stumped
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.
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\("(.*?)","(.*?)"\)'
About your request body, I would like to propose the following modification.
From:
{
"values": [
[
"=HYPERLINK("#gid=MY_GID&range=A1", "test3")"
]
]
}
To:
{
"values": [
[
"=HYPERLINK(\"#gid=MY_GID&range=A1\", \"test3\")"
]
]
}
You can also use Template_literals
let url = 'xxxxxx'
let text = 'yyyyyy'
{
"values": [
[
`=HYPERLINK("${url}","${text}")`
]
]
}