nasserahmed96

Get data from Google sheet

Jan 6th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. from __future__ import print_function
  2. import pickle
  3. import os.path
  4. from googleapiclient.discovery import build
  5. from google_auth_oauthlib.flow import InstalledAppFlow
  6. from google.auth.transport.requests import Request
  7. import gspread
  8. from oauth2client.service_account import ServiceAccountCredentials
  9. import sys
  10. from googlesheetsNasserauth import sheetAuth
  11. #GLOBAL VARIABLES
  12. GOOGLE_SHEET_API_SCOPE = ['https://www.googleapis.com/auth/spreadsheets']
  13.  
  14. SCOPES = ['https://spreadsheets.google.com/feeds',
  15.          'https://www.googleapis.com/auth/drive']
  16.  
  17. #عنوان ال الشيت كله و يكون موجودا في share
  18. INPUT_SPREAD_SHEET = 'https://docs.google.com/spreadsheets/d/1kIkGpzV0VocwF6msFPL-C8d2zEhJJrQAAx2BG8HIe14/edit?usp=sharing'
  19.  
  20. #يمكن اخذه من عنوان الشيت
  21. INPUT_SPREAD_SHEET_ID = '1kIkGpzV0VocwF6msFPL-C8d2zEhJJrQAAx2BG8HIe14'
  22. #القيم المطلوبة و تتكون من اسم الشيت + الخلايا المطلوبة
  23. INPUT_SPREAD_SHEET_RANGE = 'Anime!A2:C9'
  24.  
  25.  
  26. #تفعيل الشيت
  27. creds = sheetAuth()
  28. service = build('sheets', 'v4', credentials = creds)
  29. sheet = service.spreadsheets()
  30.  
  31.  
  32.  
  33. def updateSheet():
  34.     power_dict = {'lvl0': 100,
  35.             'lvl1': 200,
  36.             'lvl2': 250
  37.             }
  38.     values = ['458', 'Hero', 'Gundam Pilot', 90,'lvl0' ]
  39.     spread_sheet = gc.open_by_url(INPUT_SPREAD_SHEET)
  40.     sheet = spread_sheet.worksheet('Anime')
  41.        
  42. #This function takes two arguments
  43. #colors: dictionary contains colors
  44. #range the cell range to change color
  45. def getSheetValues():
  46.     result = sheet.values().get(spreadsheetId = INPUT_SPREAD_SHEET_ID, range = INPUT_SPREAD_SHEET_RANGE).execute()
  47.     values = result.get('values', [])
  48.     if not values:
  49.         print('No data found')
  50.     else:
  51.         for row in values:
  52.             print(row)
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     try:
  57.         getSheetValues()
  58.     except:
  59.         print(sys.exc_info()[0:1])
Add Comment
Please, Sign In to add comment