1

我有一个谷歌电子表格,有两列

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')

为了更好地理解,我在下面添加了 在此处输入图像描述 任何帮助

4

1 回答 1

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

如果我的理解是正确的,这个答案怎么样?不幸的是,gspread 似乎无法检索这些笔记。那么如何使用google-api-python-client呢?在这个答案中,单元格的注释是使用 google-api-python-client 的 Sheets API 中的电子表格.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和 `name 与注释一起。

结果:

{
  "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。

参考:

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

于 2019-09-08T21:59:14.197 回答