gspread.exceptions.APIError 超出网格限制 - 使用 Python 向 gsheet 添加新行和数据框
gspread.exceptions.APIError exceeds grid limits - Adding new rows and dataframe into gsheet using Python
我有一个 google sheet 需要使用 Python 从 excel sheet 每周更新一次。问题是,在某种程度上,我能够 运行 第一次尝试或几次随机尝试 上的此代码,然后我将开始收到此错误:
错误:
gspread.exceptions.APIError: {'code': 400, 'message': "Range ('test sheet'!A2155:M2987) exceeds grid limits. Max rows: 2154, max columns:
13", 'status': 'INVALID_ARGUMENT'}
附加信息:
- 此 google sheet 也由其他用户更新,所以我不想覆盖任何数据 - 我只想在 之后插入我的数据框 [=49] =] 最后一个非空白行。
- 通常在最后一个非空行之后没有行。image here
代码中的 file
指的是 excel 文件:它是一个变量,我将在其中将 file_1
更改为 file_2
、file_3
, file_4
...等每周
- 收到错误后,我认为问题是因为 在非空白行 之后没有任何行(如图像文件中所示)- 因此我'我已经尝试 append_rows、insert_rows、add_rows 到 sheet,但其中 none 有效。
However, if I tried adding a row beneath the non-blank row manually into the sheet, it works.
- 运行 加上same/different excel 文件还是没有解决问题
- 我已经尝试搜索类似的问题,但找不到。
我很沮丧和困惑,为什么 它可以同时工作而不能工作 。
- 有没有什么方法可以避免这个错误?
- 为什么会这样?
代码:
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
import os.path
from pathlib import Path
import xlsxwriter
import gspread
from gspread_dataframe import set_with_dataframe
import pygsheets
file = Path('C:\Users\Username\Desktop\Folder 1\Weekly Process\file_1')
open(file)
df_total = pd.DataFrame()
sheets = pd.ExcelFile(file).sheet_names
for sheet in sheets:
df = pd.read_excel(file,sheet_name=sheet, usecols = ['User ID','History','City','Score','Score_category'])
df_total = df_total.append(df)
df = df_total.reindex(columns = ['Column A','User ID','History','Column D','Column E',\
'Column F','Column G','Column H','City','Column J','Column K','Score','Score_category'])
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('My Project.json', scope)
client = gspread.authorize(creds)
sheet = client.open('ABC sheet')
sheet_instance = sheet.get_worksheet(0)
df = df.assign(Column A='Column A', Column D='Column D',\
Column E='Column E', Column F='Column F', Column G='Column G'\
Column H='Column H', Column J='Column J', Column K='Column K')
cells = sheet_instance.get_all_values()
end_row = len(cells)
print('To insert data AFTER this row:',end_row)
set_with_dataframe(sheet_instance, df, row=end_row + 1, include_column_header=False)
print('Done')
添加行后,重新初始化 sheet 对象,如下所示:
sheet.add_rows(push_table.shape[0])
appended_sheet = spreadsheet.worksheet(wks_name)
并使用set_with_dataframe中的新对象。例如:
gd.set_with_dataframe(worksheet=appended_sheet, dataframe=push_table, include_index=False, include_column_header=False, row= endrow+1, resize=False)
我有一个 google sheet 需要使用 Python 从 excel sheet 每周更新一次。问题是,在某种程度上,我能够 运行 第一次尝试或几次随机尝试 上的此代码,然后我将开始收到此错误:
错误:
gspread.exceptions.APIError: {'code': 400, 'message': "Range ('test sheet'!A2155:M2987) exceeds grid limits. Max rows: 2154, max columns: 13", 'status': 'INVALID_ARGUMENT'}
附加信息:
- 此 google sheet 也由其他用户更新,所以我不想覆盖任何数据 - 我只想在 之后插入我的数据框 [=49] =] 最后一个非空白行。
- 通常在最后一个非空行之后没有行。image here 代码中的
file
指的是 excel 文件:它是一个变量,我将在其中将file_1
更改为file_2
、file_3
,file_4
...等每周- 收到错误后,我认为问题是因为 在非空白行 之后没有任何行(如图像文件中所示)- 因此我'我已经尝试 append_rows、insert_rows、add_rows 到 sheet,但其中 none 有效。
However, if I tried adding a row beneath the non-blank row manually into the sheet, it works.
- 运行 加上same/different excel 文件还是没有解决问题
- 我已经尝试搜索类似的问题,但找不到。
我很沮丧和困惑,为什么 它可以同时工作而不能工作 。
- 有没有什么方法可以避免这个错误?
- 为什么会这样?
代码:
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
import os.path
from pathlib import Path
import xlsxwriter
import gspread
from gspread_dataframe import set_with_dataframe
import pygsheets
file = Path('C:\Users\Username\Desktop\Folder 1\Weekly Process\file_1')
open(file)
df_total = pd.DataFrame()
sheets = pd.ExcelFile(file).sheet_names
for sheet in sheets:
df = pd.read_excel(file,sheet_name=sheet, usecols = ['User ID','History','City','Score','Score_category'])
df_total = df_total.append(df)
df = df_total.reindex(columns = ['Column A','User ID','History','Column D','Column E',\
'Column F','Column G','Column H','City','Column J','Column K','Score','Score_category'])
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('My Project.json', scope)
client = gspread.authorize(creds)
sheet = client.open('ABC sheet')
sheet_instance = sheet.get_worksheet(0)
df = df.assign(Column A='Column A', Column D='Column D',\
Column E='Column E', Column F='Column F', Column G='Column G'\
Column H='Column H', Column J='Column J', Column K='Column K')
cells = sheet_instance.get_all_values()
end_row = len(cells)
print('To insert data AFTER this row:',end_row)
set_with_dataframe(sheet_instance, df, row=end_row + 1, include_column_header=False)
print('Done')
添加行后,重新初始化 sheet 对象,如下所示:
sheet.add_rows(push_table.shape[0])
appended_sheet = spreadsheet.worksheet(wks_name)
并使用set_with_dataframe中的新对象。例如:
gd.set_with_dataframe(worksheet=appended_sheet, dataframe=push_table, include_index=False, include_column_header=False, row= endrow+1, resize=False)