Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

223 lines (119 sloc) 4.16 KB

More examples of gspread usage

Opening a Spreadsheet

You can open a spreadsheet by its title as it appears in Google Docs:

sh ='My poor gym results')

If you want to be specific, use a key (which can be extracted from the spreadsheet's url):

sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')

Or, if you feel really lazy to extract that key, paste the entire spreadsheet's url

sht2 = gc.open_by_url('')

Creating a Spreadsheet

Use :meth:`~gspread.Client.create` to create a new blank spreadsheet:

sh = gc.create('A new spreadsheet')

However, this new spreadsheet will be visible only to your script's account. To be able to access newly created spreadsheet you must share it with your email. Which brings us to…

Sharing a Spreadsheet

If your email is you can share the newly created spreadsheet with yourself:

sh.share('', perm_type='user', role='writer')

See :meth:`~gspread.models.Spreadsheet.share` documentation for a full list of accepted parameters.

Selecting a Worksheet

Select worksheet by index. Worksheet indexes start from zero:

worksheet = sh.get_worksheet(0)

Or by title:

worksheet = sh.worksheet("January")

Or the most common case: Sheet1:

worksheet = sh.sheet1

To get a list of all worksheets:

worksheet_list = sh.worksheets()

Creating a Worksheet

worksheet = sh.add_worksheet(title="A worksheet", rows="100", cols="20")

Deleting a Worksheet


Getting a Cell Value

Using A1 notation:

val = worksheet.acell('B1').value

Or row and column coordinates:

val = worksheet.cell(1, 2).value

If you want to get a cell formula:

cell = worksheet.acell('B1', value_render_option='FORMULA').value

# or

cell = worksheet.cell(1, 2, value_render_option='FORMULA').value

Getting All Values From a Row or a Column

Get all values from the first row:

values_list = worksheet.row_values(1)

Get all values from the first column:

values_list = worksheet.col_values(1)

Getting All Values From a Worksheet as a List of Lists

list_of_lists = worksheet.get_all_values()

Finding a Cell

Find a cell matching a string:

cell = worksheet.find("Dough")

print("Found something at R%sC%s" % (cell.row, cell.col))

Find a cell matching a regular expression

amount_re = re.compile(r'(Big|Enormous) dough')
cell = worksheet.find(amount_re)

Finding All Matched Cells

Find all cells matching a string:

cell_list = worksheet.findall("Rug store")

Find all cells matching a regexp:

criteria_re = re.compile(r'(Small|Room-tiering) rug')
cell_list = worksheet.findall(criteria_re)

Cell Object

Each cell has a value and coordinates properties:

value = cell.value
row_number = cell.row
column_number = cell.col

Updating Cells

Using A1 notation:

worksheet.update_acell('B1', 'Bingo!')

Or row and column coordinates:

worksheet.update_cell(1, 2, 'Bingo!')

A more complicated example: fetch all cells in a range, change their values and send an API request that update cells in batch:

cell_list = worksheet.range('A1:C7')

for cell in cell_list:
    cell.value = 'O_o'

# Update in batch
You can’t perform that action at this time.