|
| 1 | +import gspread |
| 2 | +import socket |
| 3 | +import httplib2 |
| 4 | +from oauth2client.service_account import ServiceAccountCredentials |
| 5 | +from typing import (Any, Dict, Iterator, List, |
| 6 | + Optional, Sequence, Set, Tuple, Union) |
| 7 | +import pprint |
| 8 | + |
| 9 | +"""This module requires a creditials.json file before getting started. |
| 10 | +The following link provides a step by step tutorial on how to get startd. |
| 11 | +https://towardsdatascience.com/accessing-google-spreadsheet-data-using-python-90a5bc214fd2""" |
| 12 | + |
| 13 | +class google_sheet: |
| 14 | + """ |
| 15 | + This module was designed for the testing team to automate tests by |
| 16 | + ultizing google sheets API to record data. The following attributes that this |
| 17 | + class needs is the file_name and the tab_number define. |
| 18 | + """ |
| 19 | + def __init__(self, creditals, file_name, tab_number): |
| 20 | + self.scope = ['https://spreadsheets.google.com/feeds', |
| 21 | + 'https://www.googleapis.com/auth/drive'] |
| 22 | + self.creditals = \ |
| 23 | + ServiceAccountCredentials.from_json_keyfile_name(creditals, self.scope) |
| 24 | + self.gc = gspread.authorize(self.creditals) |
| 25 | + self.file_name = file_name |
| 26 | + self.tab_number = tab_number |
| 27 | + self.spread_sheet = self.open_google_sheet() |
| 28 | + self.worksheet = self.open_worksheet(self.tab_number) |
| 29 | + self.row_index = 1 |
| 30 | + |
| 31 | + def open_google_sheet(self): |
| 32 | + """Open Google Spread Sheet""" |
| 33 | + sheet = self.gc.open(self.file_name) |
| 34 | + return sheet |
| 35 | + |
| 36 | + def open_worksheet(self, tab_number: int )-> classmethod: |
| 37 | + """Open individual worksheet within a googlesheet""" |
| 38 | + return self.spread_sheet.get_worksheet(tab_number) |
| 39 | + |
| 40 | + def create_worksheet(self, tab_name: int): |
| 41 | + """Create a worksheet with a title for a tab. This method need |
| 42 | + to have an existing spreadsheet for it to work.""" |
| 43 | + try: |
| 44 | + self.spread_sheet.add_worksheet(tab_name, rows="1000", cols="26") |
| 45 | + except gspread.exceptions.APIError: |
| 46 | + print('Work Sheet already exists') |
| 47 | + |
| 48 | + def write_header(self, header: List): |
| 49 | + """Write Header to first row, this method as determines if the first row |
| 50 | + has a header that identical to the header the user is writing too.""" |
| 51 | + header_list = self.worksheet.row_values(1) |
| 52 | + print(header_list) |
| 53 | + if header_list != header: |
| 54 | + self.worksheet.insert_row(header, self.row_index) |
| 55 | + |
| 56 | + def write_to_row(self, data: List): |
| 57 | + """Write data into a row in a List[] format, this method skips the first |
| 58 | + row of the worksheet due to headers. """ |
| 59 | + try: |
| 60 | + self.row_index += 1 |
| 61 | + self.worksheet.insert_row(data, index= self.row_index) |
| 62 | + except socket.gaierror: |
| 63 | + pass |
| 64 | + except httplib2.ServerNotFoundError: |
| 65 | + print('UNABLE TO CONNECT TO SERVER!!, CHECK CONNECTION') |
| 66 | + except Exception as error: |
| 67 | + print(error.traceback) |
| 68 | + |
| 69 | + def delete_row(row_index: int): |
| 70 | + """Delete Row from google sheet""" |
| 71 | + self.worksheet.delete_row(row_index) |
| 72 | + |
| 73 | + def update_cell(self, row: int, column: int, single_data: any): |
| 74 | + """Update an individual cell according to a row and column. Note that |
| 75 | + this function only updates one individual cell""" |
| 76 | + self.worksheet.update_cell(row, column, data) |
| 77 | + |
| 78 | + def update_cells(self, cells, data): |
| 79 | + cell_list = self.worksheet.range(cells) |
| 80 | + self.worksheet.update_cells(data) |
| 81 | + |
| 82 | + def get_all_data(self)-> Dict[str, any]: |
| 83 | + """Get all the Data recorded from worksheet.""" |
| 84 | + return self.worksheet.get_all_records() |
| 85 | + |
| 86 | + def get_column(self, column_number: int)-> int: |
| 87 | + return self.worksheet.col_values(column_number) |
| 88 | + |
| 89 | + def get_index_row(self): |
| 90 | + """Check for the next available row to write too.""" |
| 91 | + row_index = len(self.get_column(1)) |
| 92 | + print("Row Index: ", row_index) |
| 93 | + return row_index |
| 94 | + |
| 95 | + def update_row_index(self): |
| 96 | + """Update self.row_index instance variable""" |
| 97 | + self.row_index = self.get_index_row() |
| 98 | + |
| 99 | + |
| 100 | + def get_all_sheets(self): |
| 101 | + """List all tabs in the spreadsheets""" |
| 102 | + worksheets = self.spread_sheet.worksheets() |
| 103 | + return worksheets |
| 104 | + |
| 105 | + def get_sheet_by_name(self, title: str): |
| 106 | + #worksheet = sh.worksheet("January") |
| 107 | + try: |
| 108 | + worksheet = self.spread_sheet.worksheet(title) |
| 109 | + return worksheet |
| 110 | + except gspread.exceptions.WorksheetNotFound: |
| 111 | + raise 'Worksheet does not exist!!, create a \ |
| 112 | + worksheet first using the create_worksheet() function.' |
| 113 | + |
| 114 | + def token_check(): |
| 115 | + if self.creditals.access_token_expired: |
| 116 | + self.gc.login() |
0 commit comments