From 75aab1347b97df39c988fcbe5fdf7f045ff4f8f6 Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Wed, 27 Sep 2023 21:41:53 +0800 Subject: [PATCH] sheets: add method to clear cells --- automon/integrations/google/sheets/client.py | 25 ++++++++++++ .../sheets/tests/test_google_sheets_clear.py | 40 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 automon/integrations/google/sheets/tests/test_google_sheets_clear.py diff --git a/automon/integrations/google/sheets/client.py b/automon/integrations/google/sheets/client.py index 8269ee4d..92cbe5c7 100644 --- a/automon/integrations/google/sheets/client.py +++ b/automon/integrations/google/sheets/client.py @@ -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() @@ -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, @@ -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, @@ -103,6 +127,7 @@ def update( valueInputOption: ValueInputOption = ValueInputOption.USER_ENTERED, values: list = None, ): + """update rows""" try: body = { diff --git a/automon/integrations/google/sheets/tests/test_google_sheets_clear.py b/automon/integrations/google/sheets/tests/test_google_sheets_clear.py new file mode 100644 index 00000000..1d7135f4 --- /dev/null +++ b/automon/integrations/google/sheets/tests/test_google_sheets_clear.py @@ -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()