【问题标题】:Append rows to Google Sheets using gspread or googleapiclient使用 gspread 或 googleapiclient 将行附加到 Google 表格
【发布时间】:2020-01-22 13:27:06
【问题描述】:

任务:我有一个数据框,我想将该数据附加到 Google 表格中。该工作表具有与 DF 完全相同的列,并且具有我不想覆盖的现有数据,但只需使用来自 DF 的值在工作表末尾添加行。

到目前为止尝试过:

  1. 我使用以下代码遵循了有关 gspread 的文档:
    import json
    df2.to_json(orient = 'columns')
    values = df2.to_json(orient = 'columns')
    wks.append_row (values, value_input_option='USER_ENTERED')

结果:API错误:{ “错误”: { “代码”:400, "message": "'data.values[0]' (type.googleapis.com/google.protobuf.ListValue) 处的值无效,

  1. 我已经按照 Google 的 API 参考文档:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
!pip install --upgrade google-api-python-client

from pprint import pprint
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/spreadsheets']

credentials = ServiceAccountCredentials.from_json_keyfile_name('NAME.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("SHEETNAME").sheet1

service = discovery.build(wks,'v1', credentials)

spreadsheet_id = 'MYID'

value_input_option = INSERT_ROWS 

values = df2.to_json(orient = 'columns')

insert_data_option = values 

request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()

pprint(response)

结果:AttributeError:“ServiceAccountCredentials”对象没有属性“request”

问题:这些解决方案中的任何一个都是正确的方法吗?我该如何解决这些错误?还是有更简单的完全不同的选择?

谢谢!

【问题讨论】:

  • 您能分享您在 (2) 中使用的代码吗?是否要使用服务帐户附加行?
  • 是的,我已经设置了一个服务帐户。现在共享有问题的代码。谢谢!

标签: python-3.x pandas google-sheets google-api gspread


【解决方案1】:
  • 您希望将 df2 的值附加到 Google 电子表格。
  • 您希望通过服务帐户使用 gspread 来实现此目的。
  • 您已经能够通过服务帐户使用 Sheets API 获取和放置 Google 电子表格的值。

如果我的理解是正确的,那么这个答案呢?请将此视为几个可能的答案中的一个。

修改点:

  • 在您的情况下,使用df2.values.tolist() 代替df2.to_json(orient = 'columns') 怎么样?通过这种方式,值将转换为二维数组。
  • df2的值有多行时,建议使用values_append的方法,而不是append_row。因为append_row 用于一维数组。在这种情况下,当使用多行的值时,需要在循环中使用append_row。当values_append用于多行的值时,可以通过一个API调用来放置值。

修改脚本:

当你的脚本被修改时,请进行如下修改。

spreadsheetId = '###'  # Please set the Spreadsheet ID.
sheetName = 'Sheet1'  # Please set the sheet name.

client = gspread.authorize(credentials)
sh = client.open_by_key(spreadsheetId)
df2 = pd.DataFrame({'col1': ['a1', 'a2', 'a3'], 'col2': ['b1', 'b2', 'b3']})  # This is a sample value.
values = df2.values.tolist()
sh.values_append(sheetName, {'valueInputOption': 'USER_ENTERED'}, {'values': values})
  • df2 = pd.DataFrame({'col1': ['a1', 'a2', 'a3'], 'col2': ['b1', 'b2', 'b3']}) 是一个示例值。使用此选项时,会将值附加到工作表中。

参考资料:

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

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2019-03-24
    • 2017-06-26
    • 2020-08-29
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多