gspread-pandas icon indicating copy to clipboard operation
gspread-pandas copied to clipboard

Add functionality in df_to_sheet to append data to an existing sheet

Open timdmulligan opened this issue 2 years ago • 4 comments

Hi Diego! Currently in gspread_pandas.spread.Spread().df_to_sheet() you are limited to either wiping the sheet or overwriting existing rows using the replace parameter. How feasible would it be to add an option to append to an existing sheet instead of just overwriting when replace = False? Based on some googling, I think the answer probably lies in values_append within gspread.

If you can get me pointed in the right direction I'll see if I can put together a PR.

timdmulligan avatar Apr 08 '22 14:04 timdmulligan

For now, you could use a workaround:

spread = Spread('example')
spread.df_to_sheet(df, sheet="sheet", start=(spread.get_sheet_dims("sheet")[0]+1,1), headers=False)

HgCNO2 avatar May 04 '22 13:05 HgCNO2

Hey! sorry the notifications for this must have slipped through my e-mail. The above workaround def should work well when the data starts in A1 and the column order is the same, but it gets complicated when it doesn't.

It seems like an append_to_sheet method would be a good addition, we just need to make sure that we address the following corner cases:

  • does the data fill all available columns/rows or is the worksheet bigger than the data?
  • does the data start at A1 or elsewhere?
  • are the columns and their order still the same or has it changed?

I imagine the goal of append over replace is simply to reduce the number of API calls and amount of data sent over the wire, which might complicate things because we might still need to fetch data to figure out the answers to the above questions...

aiguofer avatar May 11 '22 15:05 aiguofer

@aiguofer wondering if there is any updated on this? would indeed be ace if the package would be extend with a append_to_sheet for the very reasons you mentioned above. thanks

mrkobk avatar Oct 19 '22 08:10 mrkobk

Agreed, append and prepend (for adding rows to table sorted in reverse chronological order) functionality would be very useful.

I imagine the goal of append over replace is simply to reduce the number of API calls and amount of data sent over the wire, which might complicate things because we might still need to fetch data to figure out the answers to the above questions...

Yes, reading data from spreadsheet into dataframe, updating, then dumping enlarged dataframe into worksheet is a clunky solution, and gets worse as the dataset grows in size.

Yes, would probably need to fetch last row of data (for append) or first row of data (for prepend) to figure out where to write, and of course could have optional parameters for user to specify certain characteristics of data to reduce the number of parameters the function needs to determine on its own.

jb261 avatar Dec 11 '22 16:12 jb261