Python Forum

Full Version: Python + Google Sheet | Best way to update specific cells in a single Update()?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am learning how to use Python + Google Sheet API to update GoogleSheet

I read the developers guide on spreadsheets().values().update()
As shown in the image below, I only want to update specific cells, not a range.
How can I update only cells I want?

https://ibb.co/qp8jpq3

ssName = sheet_ELCSV.title + '!'
    cell_range = 'A2,B2,D2,A3,K3'
    
    values = (
        ('Sell','-2.17','27760.0476'),
        ('Fee', '-2.38285229')
    )
    value_range = {
        'majorDimension' : 'ROWS',
        'values': values
    }
    service.spreadsheets().values().update(
        spreadsheetId = ssID,
        valueInputOption = 'USER_ENTERED',
        range = ssName + cell_range,
        body = value_range
    ).execute()
Good day!

The below codes serve to update one or many cells in Google Sheet.
Between the two codes below, which would be most effective in using the least Write Request?
This is to minimize the Usage Limit for calling Google Sheet API.
I assume that if TH_MatchedRows = 3, then Code 1 will have 3 Requests, while Code 2 will always have 1 request regardless of TH_MatchedRows size.
Correct me if I'm wrong.

for i in TH_MatchedRows:
        sheet_THCSV.update_cell(i,9,OH_CurRow) 
OR

 TempList = []
 for i in TH_MatchedRows:
    TempList.append(OH_CurRow)

ssName = sheet_THCSV.title + '!'
cell_range = 'I' + str(TH_MatchedRows[-1]) + ':I' + str(TH_MatchedRows[0])
values = TempList
value_range = {
    'majorDimension' : 'COLUMNS',
    'values': [values]
}
service.spreadsheets().values().update(
    spreadsheetId = ssID,
    valueInputOption = 'USER_ENTERED',
    range = ssName + cell_range,
    body = value_range
).execute()
Tyvm