Ok, ho finalmente utilizzato la richiesta batch. L'idea è di inviare più modifiche in una sola richiesta API.
In primo luogo, ho creato un elenco di dizionari, che verrà utilizzato come rows_map[R][C]
per ottenere il valore della cella nella riga R
e nella colonna C
.
rows_map = [
{
1: row['first_column']
2: row['second']
3: row['and_last']
}
for row i rows
]
tanto sono tutte le celle dal foglio di lavoro
query = gdata.spreadsheet.service.CellQuery()
query.return_empty = 'true'
cells = _api_client.GetCellsFeed(self._key, wksht_id=self._raw_events_worksheet_id, query=query)
e creare richiesta batch per modificare più celle alla volta.
batch_request = gdata.spreadsheet.SpreadsheetsCellsFeed()
Quindi posso modificare (o nel mio caso riscrivere tutti i valori) il foglio di calcolo.
for cell_entry in cells.entry:
row = int(cell_entry.cell.row) - 2
col = int(cell_entry.cell.col)
if 0 <= row < len(events_map):
cell_entry.cell.inputValue = rows_map[row][col]
else:
cell_entry.cell.inputValue = ''
batch_request.AddUpdate(cell_entry)
E inviare tutte le modifiche in una sola richiesta:
_api_client.ExecuteBatch(batch_request, cells.GetBatchLink().href)
NOTE:
richiesta batch sono possibili solo con le query cella. Non esiste un meccanismo di questo tipo da utilizzare con le query di elenco.
query.return_empty = 'true'
è obbligatorio. Altrimenti API restituirà solo celle che non sono vuote.
L'API ha un "aggiornamento batch", l'ho usato con Java, ma non guardo la versione python. – eddyparkinson