├── .gitignore ├── req.txt ├── readme.md └── __main__.py /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /creds/ 3 | __pycache__ 4 | -------------------------------------------------------------------------------- /req.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client 2 | google-auth-httplib2 3 | google-auth-oauthlib 4 | oauth2client -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Код для видео на Azzrael Code о работе с Google Sheets API 2 | 3 | Для каждого видео своя ветка - просто переключайся чтобы посмотреть код 4 | соотв. видео. 5 | 6 | В первом видео я покажу 7 | [Как читать Google Sheet с помощью Сервисного Аккаунта](https://youtu.be/hMl-0yiBMNs). 8 | 9 | Подробно о типах ключей в Google API (как их создавать и в чем разница) 10 | я рассказывал в видео: 11 | [Как создать проект в Google Cloud Platform](https://www.youtube.com/watch?v=WpB42nS1uWE) 12 | 13 | ### Полезные ссылки 14 | 15 | - https://console.cloud.google.com/ 16 | - https://github.com/googleapis/google-api-python-client#installation 17 | 18 | - https://developers.google.com/sheets/api/guides/concepts 19 | - https://developers.google.com/sheets/api/quickstart/python 20 | - https://developers.google.com/sheets/api/guides/authorizing 21 | 22 | ### Установка 23 | См прошлые видео в [Плейлисте Google API](https://www.youtube.com/watch?v=PjKMDtLuKPU&list=PLWVnIRD69wY4ane3amNJSFQfls1inhaub) 24 | Но в целом, скорее всего, достаточно: 25 | `pip install -r req.txt` 26 | Потом создаю Проект в Google Cloud Platform и получи Сервисный Аккаунт 27 | Скачай в папку `creds` файлик json с секретками и назови его `sacc1.json` 28 | Также потом пригодится api_kei в `cred/__init__.py` (см. видео) 29 | Затем расшарь свою таблицу в Google Sheets для емейла созданноего Сервисного Аккаунта с правами Редактор -------------------------------------------------------------------------------- /__main__.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import httplib2 4 | from googleapiclient.discovery import build 5 | from oauth2client.service_account import ServiceAccountCredentials 6 | 7 | import creds 8 | 9 | 10 | def get_service_simple(): 11 | return build('sheets', 'v4', developerKey=creds.api_key) 12 | 13 | 14 | def get_service_sacc(): 15 | """ 16 | Могу читать и (возможно) писать в таблицы кот. выдан доступ 17 | для сервисного аккаунта приложения 18 | 19 | sacc-1@privet-yotube-azzrael-code.iam.gserviceaccount.com 20 | 21 | :return: 22 | """ 23 | creds_json = os.path.dirname(__file__) + "/creds/sacc1.json" 24 | scopes = ['https://www.googleapis.com/auth/spreadsheets'] 25 | 26 | creds_service = ServiceAccountCredentials.from_json_keyfile_name(creds_json, scopes).authorize(httplib2.Http()) 27 | return build('sheets', 'v4', http=creds_service) 28 | 29 | 30 | # service = get_service_simple() 31 | service = get_service_sacc() 32 | sheet = service.spreadsheets() 33 | 34 | # https://docs.google.com/spreadsheets/d/xxx/edit#gid=0 35 | sheet_id = "xxx" 36 | 37 | # https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get 38 | # resp = sheet.values().get(spreadsheetId=sheet_id, range="Лист1!A1:A999").execute() 39 | 40 | # https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet 41 | resp = sheet.values().batchGet(spreadsheetId=sheet_id, ranges=["Лист1", "Лист2"]).execute() 42 | 43 | print(resp) 44 | --------------------------------------------------------------------------------