gpt4 book ai didi

python - 如何使用 python 读取(或解析)谷歌电子表格列注释(评论)

转载 作者:行者123 更新时间:2023-12-01 07:18:51 25 4
gpt4 key购买 nike

我有一个 Google 电子表格,有两列

id , name
1 aaaa

并且有一个关于列名称和第1行的注释,这意味着在单元格(2,2)中我能够读取电子表格两列的数据,但无法读取该单元格的注释。我正在使用 gspread 库从电子表格中读取数据。

import gspread
from gspread_dataframe import get_as_dataframe

scope = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'jasonfileNlocation.json',
scope)
google_spreadsheet_connection = gspread.authorize(credentials)

wks = google_spreadsheet_connection.open("spreadsheet_name")
worksheet = wks.get_worksheet(0)
df = get_as_dataframe(worksheet, evaluate_formulas=True, index='false')

为了更好地理解我添加了下面的图片 enter image description here如有任何帮助,我们将不胜感激

最佳答案

  • 您想要检索电子表格上单元格的注释。
  • 您希望使用服务帐户和 Python 来实现此目的。
  • 在您的脚本中,ServiceAccountCredentials用作from oauth2client.service_account import ServiceAccountCredentials .
  • 您已经能够使用 Sheets API 和 gspread 为电子表格添加和获取值。

如果我的理解是正确的,这个答案怎么样?不幸的是,gspread 似乎无法检索这些笔记。那么使用 google-api-python-client 怎么样? ?在此答案中,使用 Google-api-python-client 的 Sheets API 中的 Spreadsheets.get 方法检索单元格的注释。

修改后的脚本:

在此修改中,您的脚本已被修改。

import gspread
import httplib2
from apiclient import discovery
from gspread_dataframe import get_as_dataframe
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'jasonfileNlocation.json',
scope)
google_spreadsheet_connection = gspread.authorize(credentials)
wks = google_spreadsheet_connection.open("spreadsheet_name")
worksheet = wks.get_worksheet(0)
df = get_as_dataframe(worksheet, evaluate_formulas=True, index='false')

# I added below script.
service = discovery.build(
'sheets', 'v4', http=credentials.authorize(httplib2.Http()))

spreadsheet_id = '###' # Please set the Spreadsheet ID here.
ranges = ['Sheet1!A2:B'] # For example, when you want to retrieve the note from the cells "A2:B" of "Sheet1", please use this.

fields = 'sheets(data(rowData(values(note,userEnteredValue))))'
request = service.spreadsheets().get(
spreadsheetId=spreadsheet_id, ranges=ranges, fields=fields)
response = request.execute()
print(response)
  • 我可以理解ID是“A”列的值,您想要 EMPID 的值和`名字和注释。

结果:

{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{"userEnteredValue": {"numberValue": 1}},
{"note": "sample note1", "userEnteredValue": {"stringValue": "name1"}}
]
},
{
"values": [
{"userEnteredValue": {"numberValue": 2}},
{"note": "sample note2", "userEnteredValue": {"stringValue": "name2"}}
]
},
,
,
,

]
}
]
}
]
}
  • 索引rowData是行号。
  • 索引values是列号。

注意:

  • 在此示例中,检索“Sheet1”工作表中单元格“B2”的注释。如果您想检索其他单元格,请修改此项。
  • 在此修改后的脚本中,还可以使用 gspread。

引用文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

关于python - 如何使用 python 读取(或解析)谷歌电子表格列注释(评论),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com