Skip to content

Instantly share code, notes, and snippets.

@Logic-gate
Created July 17, 2019 12:33
Show Gist options
  • Save Logic-gate/0caf685f6843f9e18dd93426c64a23c9 to your computer and use it in GitHub Desktop.
Save Logic-gate/0caf685f6843f9e18dd93426c64a23c9 to your computer and use it in GitHub Desktop.
A helper script for gspread
import gspread
from oauth2client.service_account import ServiceAccountCredentials
class gHelper:
def __init__(self, json_creds):
self.scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
self.cred = ServiceAccountCredentials.from_json_keyfile_name(
json_creds, self.scope)
def auth(self):
client = gspread.authorize(self.cred)
return client
def get_worksheet_by_name(self, spreadsheet_name, worksheet_name):
"""
Only supports selecting by worksheet name
:param spreadsheet_name: String spreadsheet name
:param worksheet_name: String worksheet name
"""
self.sheet = self.auth().open(spreadsheet_name).worksheet(worksheet_name)
return self.sheet
def list_column_by_name(self, col):
"""
return list | entire column contating 'col'
:param col: String column name
"""
f = self.sheet.find(col)
return self.sheet.col_values(f.col)
def list_row_by_name(self, row):
"""
return list | entire row contating 'row'
only returns first result, not useful for duplicate rows
use find_row_by_condition instead.
:param row: String row name
"""
f = self.sheet.find(row)
return self.sheet.row_values(f.row)
def all_records(self):
return self.sheet.get_all_records()
def find_row_by_condition(self, string, *row_condition):
"""
TODO: string is not needed.
return tuple | (True|False, {row number: result})
:param string: String string to search
:params row_condition: Strings strings to match
"""
f = self.sheet.findall(string)
# print len(f), len(rows)
found = {}
if len(row_condition) >= 2:
for i in f:
r = self.sheet.row_values(i.row)
result = all(x in r for x in row_condition)
if result is True:
found.update({i.row: r})
return True, found
else:
return False, 'row_condition needs to be >= 2, found %s' % len(row_condition)
def update_cell_by_entry(self, old_value, new_value, row_condition):
"""
Given row_condtion, update old_value with new_value
:param old_value: String cell value to change
:param new_value: String new cell value
:param row_conditon: Tuple match conditions
"""
if row_condition[0] is True:
f = self.sheet.find(old_value)
if row_condition[1].keys()[0] == f.row:
self.sheet.update_cell(f.row, f.col, new_value)
return True
else:
return 'could not find exact match for "%s"' % old_value
else:
return 'row_condition returned False with "%s"' % row_condition[1]
def update_new_row_entry(self, *value):
"""
TODO: during testing all_records showed extra fields, check later
Update first empty row with values
:params value: String cell update values
"""
new_row = len(self.all_records()) + 2
for i, ii in zip(range(1, len(value) + 1), value):
self.sheet.update_cell(new_row, i, ii)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment