Skip to content

Commit

Permalink
fix bug in gsheet utility code
Browse files Browse the repository at this point in the history
  • Loading branch information
gargnitingoogle committed Oct 15, 2024
1 parent 7c86625 commit 1200bbf
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions perfmetrics/scripts/testing_on_gke/examples/utils/gsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1200bbf

Please sign in to comment.