简体   繁体   English

如何在 gspread 中动态更新/插入谷歌工作表的行和列?

[英]How to update/insert rows and columns of google sheets dynamically in gspread?

I have a spreadsheet like below and i have an API which will give data as a list by date like below我有一个如下所示的电子表格,我有一个 API,它将按日期提供数据列表,如下所示

[05/06/2019', 10000, 14003, 140, $576.1786404]
[05/07/2019', 11000, 14003, 140, $576.1786404]
[05/08/2019', 50641, 14067, 357, $578.8120356]

在此处输入图片说明

All I have to do is update the data for a date if it already exists or insert a new row into the sheet of a new date.我所要做的就是更新某个日期的数据(如果它已经存在)在新日期的工作表中插入一个新行。

From the above list I have to update data of 05/06/2019 and I have to insert two rows of date 05/07/2019 and 05/08/2019从上面的列表中,我必须更新05/06/2019 的数据,并且必须插入两行日期05/07/201905/08/2019

I have below code and don't know to do next steps to implement我有下面的代码,不知道下一步要实施

gc = gspread.authorize(creds)

worksheet = gc.open("chethan testing").sheet1

cell_list = worksheet.range('A3:E3')
cell_values = [1,2,3,4,5]

for i, val in enumerate(cell_values):
    cell_list[i].value = val 

 worksheet.update_cells(cell_list)

Can somebody guide me on how to proceed with next steps.?有人可以指导我如何进行下一步吗?

Thanks in advance提前致谢

For the above requirement i did like below to get solved.对于上述要求,我确实喜欢在下面解决。

my_list = [['2018-11-10', '1000000000', '14003', '140', '576.1786404'], ['2018-11-11', '506541', '14067', '357', '578.8120356'], ['2018-11-12', '423175', '15250', '330', '627.4887'], ['2018-11-13', '274503', '11337', '240', '466.4812716'], ['2018-11-14', '285468', '11521', '194', '474.0522828']]

worksheet.values_update(
    'Sheet1!A3',
    params={
        'valueInputOption': 'RAW'
    },
    body={
        'values':my_list
    }
)

This will update the existing values and add the other values to new rows.这将更新现有值并将其他值添加到新行。

More you can find here https://gspread.readthedocs.io/en/latest/api.html你可以在这里找到更多https://gspread.readthedocs.io/en/latest/api.html

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM