小编use*_*457的帖子

使用 gspread 或 googleapiclient 将行追加到 Google 表格

任务:我有一个数据框,我想将该数据附加到 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')
Run Code Online (Sandbox Code Playgroud)

结果:APIError:{“错误”:{“代码”:400,“消息”:“'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 …
Run Code Online (Sandbox Code Playgroud)

google-api google-sheets python-3.x pandas gspread

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

google-api ×1

google-sheets ×1

gspread ×1

pandas ×1

python-3.x ×1