使用 pygsheets / google 表进行批量更新的问题/问题

Issues / question with batch update with pygsheets / google sheets

好吧,我是 python 的新手,但是...我真的很喜欢它。一段时间以来,我一直在努力解决这个问题,并认为有人可以提供比我知道的更多的帮助。

所以我想做的是使用 pygsheets 并通过一个 api 调用与多个调用组合批处理更新。我一直在寻找示例或想法,发现如果您取消 link 和 link 它会这样做吗?我试过了,它只加快了一点点,然后我看了看,你可以使用 update.values 与 update.value。我已经让它与这样的东西一起工作 wk1.update_values('A2:C4',[[1,2,3],[4,5,6],[7,8,9]]) 但是如果您希望更新位于特定的单元格位置而不是像 a2:c4 这样的范围怎么办?感谢您提前提出任何建议。

https://pygsheets.readthedocs.io/en/latest/worksheet.html#pygsheets.Worksheet.update_values https://pygsheets.readthedocs.io/en/latest/sheet_api.html?highlight=batch_updates#pygsheets.sheet.SheetAPIWrapper.values_batch_update

import pygsheets

gc = pygsheets.authorize() # This will create a link to authorize 

#  Open spreadsheet  

GS_ID = ''
File_Tab_Name = 'File1'
Main_Topic = 'Main Topic'
Actual_Company_Name = 'Company Name'
Street = 'Street Address'
City_State_Zip = 'City State Zip'
Phone_Number = 'Phone Number'


# 2. Open spreadsheet by key
sh = gc.open_by_key(GS_ID)

sh.title = File_Tab_Name
wk1 = sh[0]
wk1.title = File_Tab_Name
#wk1.update_values('A2:C4',[[1,2,3],[4,5,6],[7,8,9]])  
wk1.update_values([['a1'],['h1'],['i3']],[[Main_Topic],[Actual_Company_Name],[Street]])   ### is this possible
#wk1.unlink()
#wk1.title = File_Tab_Name
#wk1.update_value("a1",Main_Topic)  ###Topic
#wk1.update_value("h1",Actual_Company_Name)  ###Company Name
#wk1.update_value("i3",Street)  ###Street Address
#wk1.update_value("i4",City_State_Zip)  ###City State Zip
#wk1.update_value("i5",Phone_Number)   ### Phone Number
#wk1.link() # will do all the updates

不幸的是,pygsheets 没有批量更新多个范围的方法。相反,您可以使用 gspread.

gspread 有 batch_update 方法,您可以在其中一次更新多个单元格或范围。

示例:

代码:

import gspread

gc = gspread.service_account()
sh = gc.open_by_key("insert spreadsheet key here").sheet1
sh.batch_update([{
    'range': 'A1:B1',
    'values': [['42', '43']],
}, {
    'range': 'A2:B2',
    'values': [['44', '45']],
}])

输出:

参考文献:

据我了解,您想要批量更新值。您可以使用 update_values_batch 函数。

wks.update_values_batch(['A1:A2', 'B1:B2'], [[[1],[2]], [[3],[4]]])
# or
wks.update_values_batch([((1,1), (2,1)), 'B1:B2'], [[[1,2]], [[3,4]]], 'COLUMNS')
# or
wks.update_values_batch(['A1:A2', 'B1:B2'], [[[1,2]], [[3,4]]], 'COLUMNS')

参见 doc here

注意:将 pygsheets 更新到最新版本或从 gitub 安装

pip install --upgrade https://github.com/nithinmurali/pygsheets/archive/staging.zip