Last active
July 18, 2023 10:31
-
-
Save Alexander671/90df04a1249c9ac67640f0756636fab0 to your computer and use it in GitHub Desktop.
This file contains 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
from pprint import pprint | |
import httplib2 | |
import apiclient.discovery | |
from oauth2client.service_account import ServiceAccountCredentials | |
# Файл, полученный в Google Developer Console | |
CREDENTIALS_FILE = 'creds.json' | |
# ID Google Sheets документа (можно взять из его URL) | |
spreadsheet_id = '1w6Cx3osDQJGzu7yS7fKeNXyLNGgkTBkl0AlLlq2fBRk' | |
# Авторизуемся и получаем service — экземпляр доступа к API | |
credentials = ServiceAccountCredentials.from_json_keyfile_name( | |
CREDENTIALS_FILE, | |
['https://www.googleapis.com/auth/spreadsheets', | |
'https://www.googleapis.com/auth/drive']) | |
httpAuth = credentials.authorize(httplib2.Http()) | |
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth) | |
# Пример чтения файла | |
values = service.spreadsheets().values().get( | |
spreadsheetId=spreadsheet_id, | |
range='A1:E10', | |
majorDimension='COLUMNS' | |
).execute() | |
pprint(values) | |
# Пример записи в файл | |
values = service.spreadsheets().values().batchUpdate( | |
spreadsheetId=spreadsheet_id, | |
body={ | |
"valueInputOption": "USER_ENTERED", | |
"data": [ | |
{"range": "B3:C4", | |
"majorDimension": "ROWS", | |
"values": [["This is B3", "This is C3"], ["This is B4", "This is C4"]]}, | |
{"range": "D5:E6", | |
"majorDimension": "COLUMNS", | |
"values": [["This is D5", "This is D6"], ["This is E5", "=5+5"]]} | |
] | |
} | |
).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment