简体   繁体   English

从 Google 电子表格 (gspread) 中的单元格获取 href 标记内的链接

[英]Get link inside href tag from cell in Google Spreadsheet (gspread)

I am using the Python module gspread to try and extract a link inside an href tag from a cell of a Google spreadsheet.我正在使用 Python 模块 gspread 尝试从 Google 电子表格的单元格中提取 href 标记内的链接。 I have tried the following, and noted their problems:我尝试了以下方法,并注意到了他们的问题:

  1. worksheet.acell ('B5').value : Gets cell text, not link inside href tag. worksheet.acell ('B5').value :获取单元格文本,而不是 href 标记内的链接。
  2. worksheet.acell ('B5', value_render_option='FORMULA').value : Gets cell text, not link inside href tag. worksheet.acell ('B5', value_render_option='FORMULA').value :获取单元格文本,而不是链接到 href 标记内。
  3. worksheet.acell('B5').input_value : Returned none. worksheet.acell('B5').input_value :没有返回。 Also, deprecated.此外,已弃用。

How can I correctly get a link inside href tags from a cell in a Google spreadsheet?如何从 Google 电子表格中的单元格中正确获取 href 标签内的链接?

In order to retrieve a hyperlink of a cell, it is required to use the method of spreadsheets.get in Sheets API using the fields.为了检索单元格的超链接,需要使用表格 API 中的电子表格.get 方法使用字段。 Unfortunately, I couldn't find this method in gspread .不幸的是,我在gspread中找不到这种方法。 So in this answer, I would like to propose the following flow.所以在这个答案中,我想提出以下流程。

  1. Retrieve the access token.检索访问令牌。
    • I think that in this case, the script of your authorization for gspread can be used.我认为在这种情况下,可以使用您对gspread的授权脚本。
  2. Request to the method of spreadsheets.get in Sheets API using requests module.使用requests模块向 Sheets API 中的电子表格方法请求。
  3. Retrieve the hyperlink.检索超链接。

Sample script:示例脚本:

import requests
import urllib.parse


spreadsheetId = "###"  # Please set the Spreadsheet ID.
cellRange = "Sheet1!A1"  # Please set the range with A1Notation. In this case, the hyperlink of the cell "A1" of "Sheet1" is retrieved.

client = gspread.authorize(credentials)  # I think that this is also used in your script for using gsperad.

# 1. Retrieve the access token.
access_token = client.auth.token

# 2. Request to the method of spreadsheets.get in Sheets API using `requests` module.
fields = "sheets(data(rowData(values(hyperlink))))"
url = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheetId + "?ranges=" + urllib.parse.quote(cellRange) + "&fields=" + urllib.parse.quote(fields)
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})

# 3. Retrieve the hyperlink.
obj = res.json()
link = obj["sheets"][0]['data'][0]['rowData'][0]['values'][0]['hyperlink']
print(link)
  • This sample script retrieves the hyperlink in the cell "A1" on "Sheet1".此示例脚本检索“Sheet1”上单元格“A1”中的超链接。

Note:笔记:

  • Recently, Google Spreadsheet got to be able to have multiple hyperlinks in a cell.最近,Google 电子表格能够在一个单元格中包含多个超链接。 But in the current stage, unfortunately, it seems that those links cannot be retrieved using Sheets API.但不幸的是,在当前阶段,似乎无法使用表格 API 检索这些链接。 I believe that this will be resolved in the future update.我相信这将在未来的更新中得到解决。
  • So, in this sample script, when one hyperlink is set in one cell, this script can retrieve the hyperlink.因此,在此示例脚本中,当在一个单元格中设置一个超链接时,此脚本可以检索超链接。 So please be careful this.所以请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM