Skip to content

Commit

Permalink
sheets: add method to clear cells
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Sep 27, 2023
1 parent 21f1683 commit 75aab13
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions automon/integrations/google/sheets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,34 @@ def __init__(

@property
def values(self):
"""row values"""
if self.response:
try:
return self.response['values']
except Exception as e:
pass

def clear(
self,
range: str,
spreadsheetId: str = None,
**kwargs,
):
"""clear rows"""
try:

result = self.spreadsheets().values().clear(
spreadsheetId=spreadsheetId or self.config.spreadsheetId,
range=range or self.range,
**kwargs,
).execute()

print(f"{result.get('updatedCells')} cells updated.")
return result
except Exception as error:
print(f"An error occurred: {error}")
return error

def spreadsheets(self):
"""spreadsheet service"""
return self.service().spreadsheets()
Expand All @@ -62,6 +84,7 @@ def get(
fields: Fields or str = None,
**kwargs,
):
"""get rows"""
try:
self.response = self.spreadsheets().get(
spreadsheetId=spreadsheetId or self.config.spreadsheetId,
Expand All @@ -81,6 +104,7 @@ def get_values(
range: str = None,
**kwargs,
):
"""get values"""
try:
self.response = self.spreadsheets().values().get(
spreadsheetId=spreadsheetId or self.config.spreadsheetId,
Expand All @@ -103,6 +127,7 @@ def update(
valueInputOption: ValueInputOption = ValueInputOption.USER_ENTERED,
values: list = None,
):
"""update rows"""
try:

body = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest

import pandas as pd

from automon.integrations.google.sheets import GoogleSheetsClient

SHEET_NAME = 'Copy of Automated Count DO NOT EDIT'


class MyTestCase(unittest.TestCase):
def test_authenticate(self):
sheets_client = GoogleSheetsClient(
worksheet=SHEET_NAME,
)

if not sheets_client.authenticate():
return

sheets_client.get_values(
range=f'{SHEET_NAME}!A:Z'
)

sheet_values = sheets_client.values
sheet_columns = sheet_values[0]
sheet_data = sheet_values[1:]

df = pd.DataFrame(data=sheet_data, columns=sheet_columns)
df = df.dropna(subset=['url'])
# set df index to match google sheet index numbering
df.index = df.index + 2

sheets_client.clear(
range=f'{SHEET_NAME}!8:5',
)

pass


if __name__ == '__main__':
unittest.main()

0 comments on commit 75aab13

Please sign in to comment.