Reputation: 2886
I have managed to open and edit an existing google sheet using the Google API but I fail when I try to create a new one:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
#
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()
pprint(response)
The above creates a file but when I visit the link given in the terminal, I need to grant access! The output of the terminal is below:
{'properties': {'autoRecalc': 'ON_CHANGE',
'defaultFormat': {'backgroundColor': {'blue': 1,
'green': 1,
'red': 1},
'padding': {'bottom': 2,
'left': 3,
'right': 3,
'top': 2},
'textFormat': {'bold': False,
'fontFamily': 'arial,sans,sans-serif',
'fontSize': 10,
'foregroundColor': {},
'italic': False,
'strikethrough': False,
'underline': False},
'verticalAlignment': 'BOTTOM',
'wrapStrategy': 'OVERFLOW_CELL'},
'locale': 'en_US',
'timeZone': 'Etc/GMT',
'title': 'Untitled spreadsheet'},
'sheets': [{'properties': {'gridProperties': {'columnCount': 26,
'rowCount': 1000},
'index': 0,
'sheetId': 0,
'sheetType': 'GRID',
'title': 'Sheet1'}}],
'spreadsheetId': '1bSzXveNHTFDuk6Idj5snsZQVp0RAR-ksb5s_CZusQus',
'spreadsheetUrl': 'https://docs.google.com/spreadsheets/d/1bSzXveNHTFDuk6Idj5snsZQVp0RAR-ksb5s_CZusQus/edit'}
Is there a way to parse the spreadsheetId or even better.. its folder (destination) and grant access to a specific user (by email)?
Upvotes: 0
Views: 318
Reputation: 6737
You can use this Method: spreadsheets.create to create a spreadsheet.
There is an available python code that you can use.
"""
BEFORE RUNNING:
---------------
1. If not already done, enable the Google Sheets API
and check the quota for your project at
https://console.developers.google.com/apis/api/sheets
2. Install the Python client library for Google APIs by running
`pip install --upgrade google-api-python-client`
"""
from pprint import pprint
from googleapiclient import discovery
# TODO: Change placeholder below to generate authentication credentials. See
# https://developers.google.com/sheets/quickstart/python#step_3_set_up_the_sample
#
# Authorize using one of the following scopes:
# 'https://www.googleapis.com/auth/drive'
# 'https://www.googleapis.com/auth/drive.file'
# 'https://www.googleapis.com/auth/spreadsheets'
credentials = None
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
# TODO: Add desired entries to the request body.
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()
# TODO: Change
code below to process the response
dict:
pprint(response)
For authorization, you can use the use one of the following OAuth scopes:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
For further information about authorization, you can visit this Auth Guide
Upvotes: 1