I tried looking at the answers of similar questions but wasn't able to get the code working. I'm having trouble understanding how to populate gspread of similar dictionaries within a list.
Below is my data structure that I want to export:
sheet_data = [{
"timestamp": "09-04-2019",
"value": "10.0",
"company_name": "Xbox",
"product": "Buy"
},
{
"timestamp": "09-03-2019",
"value": "2.0",
"company_name": "something",
"product": "Sell"
}]
Below is what I've tried and works now. My remaining question is, I am manually inputting the cell_range = worksheet.range('A2:D3'), how can I do it so it updates the sheet to whatever cell is available with the given data. Since the amount of data I have in sheet_data will change in future updates.
header = ['timestamp', 'value', 'company_name', 'product']
worksheet.add_rows(len(sheet_data))
cell_range = worksheet.range('A2:D3')
flat_sheet_data = []
for row in sheet_data:
for column in header:
flat_sheet_data.append(row[column])
for i, cell in enumerate(cell_range):
cell.value = flat_sheet_data[i]
worksheet.update_cells(cell_range)
link to image of what I want to accomplish on spreedsheet using above data structure: https://i.sstatic.net/EQLEI.png
my data structure that I want to export:, do you want to retrieve the values from Google Spreadsheet as an object ofsheet_data? But in your script, it seems that the range of "A2:C10" is updated. So can I ask you about your goal? By the way, can you provide a sample Spreadsheet and sample result values you expect? I think that it will help users think of the solution. Of course, please remove your personal information. By the way, have you already been able to get and put values for Spreadsheet using gspread?sheet_datato Spreadsheet. I could understand like this. Is my understanding correct? If my understanding is correct, I have one more question. When I saw your image andsheet_data, it seems that the headers of Spreadsheet and the keys ofsheet_dataare different. How about this?row(i.e. a whole dictionary representing a row) to cell.value? Shouldn't you insteadcell.value = row['value']and just iterate over the row that needsvalue? Then do the same for the other rows, instead of doing the whole block at once. Or, alternatively, simply have 4 assignment statements, assigning the correct value to the correct cell in order - but that'll be a bit messy.