Update multiple columns in Google Sheet using Python gspread library
1 min readJul 11, 2019
Some times we need to update multiple cells from different rows and columns. To update multiple cells, we should use update_cells API. It will help us to reduce the running time of our program tremendously.
Example usage is as follows
Import necessary libraries
import gspread
from gspread.models import Cell
from oauth2client.service_account import ServiceAccountCredentials
import string as string
import random
Execute the code
#Update multiple cell values using gspread# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('Sheet-Update-Secret.json', scope)
client = gspread.authorize(creds)cells = []
for i in range(10):
for j in range(10):
cells.append(Cell(row=i+1, col=j+1, value=''.join(random.choices(string.printable, k=10))))
# Find a workbook by id and open the first by name
# Make sure you use the right name here.
sheet = client.open_by_key("Sheet Id").worksheet("Data Set 3")sheet.update_cells(cells)
We will get output as follows.