From 9be12832774e40d2bacbc5bc892cf886b5e51feb Mon Sep 17 00:00:00 2001 From: Nitin Garg Date: Fri, 11 Oct 2024 05:39:42 +0000 Subject: [PATCH] fix bug in gsheet utility code --- .../testing_on_gke/examples/utils/gsheet.py | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py b/perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py index 0dabbdbb51..bd0ba6099f 100644 --- a/perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py +++ b/perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py @@ -91,41 +91,41 @@ def _using_local_service_key_file(localServiceAccountKeyFile: str): # Open a read-write gsheet client. client = _get_sheets_service_client(localServiceAccountKeyFile) - def _read_from_range(cell_range: str): - """Returns a list of list of values for the given range in the worksheet.""" - gsheet_response = ( - client.spreadsheets() - .values() - .get(spreadsheetId=gsheet_id, range=f'{worksheet}!{cell_range}') - .execute() - ) - return gsheet_response['values'] if 'values' in gsheet_response else [] - - def _write_at_address(cell_address: str, data): - """Writes a list of tuple of values at the given cell_cell_address in the worksheet.""" - client.spreadsheets().values().update( - spreadsheetId=gsheet_id, - valueInputOption='USER_ENTERED', - body={'majorDimension': 'ROWS', 'values': data}, - range=f'{worksheet}!{cell_address}', - ).execute() - - data_in_first_column = _read_from_range('A1:A') - num_rows = len(data_in_first_column) - data_in_first_row = _read_from_range('A1:1') - original_header = tuple(data_in_first_row[0]) if data_in_first_row else () - new_header = data['header'] - - # Insert header in the file, if needed. - if not original_header or not original_header == new_header: - # Append header after last row. - _write_at_address(f'A{num_rows+1}', [new_header]) + def _read_from_range(cell_range: str): + """Returns a list of list of values for the given range in the worksheet.""" + gsheet_response = ( + client.spreadsheets() + .values() + .get(spreadsheetId=gsheet_id, range=f'{worksheet}!{cell_range}') + .execute() + ) + return gsheet_response['values'] if 'values' in gsheet_response else [] + + def _write_at_address(cell_address: str, data): + """Writes a list of tuple of values at the given cell_cell_address in the worksheet.""" + client.spreadsheets().values().update( + spreadsheetId=gsheet_id, + valueInputOption='USER_ENTERED', + body={'majorDimension': 'ROWS', 'values': data}, + range=f'{worksheet}!{cell_address}', + ).execute() + + data_in_first_column = _read_from_range('A1:A') + num_rows = len(data_in_first_column) + data_in_first_row = _read_from_range('A1:1') + original_header = tuple(data_in_first_row[0]) if data_in_first_row else () + new_header = data['header'] + + # Insert header in the file, if needed. + if not original_header or not original_header == new_header: + # Append header after last row. + _write_at_address(f'A{num_rows+1}', [new_header]) + num_rows = num_rows + 1 + + # Append given values after the last row. + _write_at_address(f'A{num_rows+1}', data['values']) num_rows = num_rows + 1 - # Append given values after the last row. - _write_at_address(f'A{num_rows+1}', data['values']) - num_rows = num_rows + 1 - if serviceAccountKeyFile.startswith('gs://'): localServiceAccountKeyFile = download_gcs_object_locally( serviceAccountKeyFile