A note on how to edit Google Sheets using OAuth.
What you need here --Email address displayed on the console (eg [email protected]) --P12 key file downloaded above
Install the following with pip
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
#Authentication
f = file('/path/to/p12keyfile', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
'[email protected]', # Email address
key,
scope='https://spreadsheets.google.com/feeds https://docs.google.com/feeds',
token_uri='https://accounts.google.com/o/oauth2/token'
)
gs = gspread.authorize(credentials)
doc = gs.open('Worksheet name')
#Add Sheet
sheet = doc.add_worksheet('Sheet name', row=100, col=20)
#Sheet selection
sheet = doc.worksheet("Sheet name")
#Get value
val = sheet.acell('B1').value #When specifying a label, the method name is acell
val = sheet.cell(1,2).value #When specifying coordinates(line,Column)
#Setting / changing values
sheet.update_acell('B1', 'hoge') #When specifying the label, the method name is update_acell
sheet.update_cell(1, 2, 'hoge') #When specifying coordinates(line,Column)
#When there is a lot of change
cell_list = sheet.range('A1:C4')
'''
cell_list[0] : A1
cell_list[1] : B1
cell_list[2] : C1
cell_list[3] : A2
cell_list[4] : B2
:
'''
for cell in cell_list:
cell.value = 'fuga'
sheet.update_cells(cell_list)
If you don't hesitate to write your password.
import gspread
doc = spread.login('[email protected]', 'password')
sheet = doc.add_worksheet('Sheet name', row=100, col=10)
Recommended Posts