Created
April 17, 2018 17:59
-
-
Save mrprompt/829289e0c84296afa49562a5143a7615 to your computer and use it in GitHub Desktop.
Sample code to work with Google Spreadsheet and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
import pprint | |
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] | |
creds = ServiceAccountCredentials.from_json_keyfile_name('python-15f4d92f03f2.json', scope) | |
client = gspread.authorize(creds) | |
sheet = client.open('Horta').sheet1 | |
# get all | |
plantas = sheet.get_all_records() | |
pprint.pprint(plantas) | |
# get single row | |
result = sheet.row_values(2) | |
pprint.pprint(result) | |
# get single col | |
result = sheet.col_values(2) | |
pprint.pprint(result) | |
# get single cell | |
result = sheet.cell(2, 2) | |
pprint.pprint(result) | |
# update cel | |
sheet.update_cell(6, 2, 'Alface') | |
result = sheet.cell(6, 2).col_value | |
pprint.pprint(result) | |
row = ["I'm", "updating", "a", "spreadsheet", "from", "python"] | |
index = 3 | |
sheet.insert_row(row, index) | |
# row count | |
count = sheet.row_count | |
pprint.pprint(count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment