From 5e04c47ba5476cef9de4098c90013936d0ac43a4 Mon Sep 17 00:00:00 2001 From: alexiswl <8197659+alexiswl@users.noreply.github.com> Date: Wed, 8 May 2024 19:41:59 +1000 Subject: [PATCH] Dont set replace=True as this removes all formatting Formatting for portal_run_id must be set to 'TEXT' for number format --- .../cttso-ica-to-pieriandx-cdk/Changelog.md | 9 ++++++ .../layers/lambda_utils/gspread_helpers.py | 28 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/deploy/cttso-ica-to-pieriandx-cdk/Changelog.md b/deploy/cttso-ica-to-pieriandx-cdk/Changelog.md index be9cbe4..d3b123c 100644 --- a/deploy/cttso-ica-to-pieriandx-cdk/Changelog.md +++ b/deploy/cttso-ica-to-pieriandx-cdk/Changelog.md @@ -3,6 +3,15 @@ Changes in this log refer only to changes that make it to the 'main' branch and are nested under deploy/cttso-ica-to-pieriandx-cdk. +## 2024-05-08 + +> Author: Alexis Lucattini +> Email: [Alexis.Lucattini@umccr.org](mailto:alexis.lucattini@umccr.org) + +### Hotfixes + +* Excel interprets a few portal run ids as numbers i.e 2021121980805e78 + ## 2024-05-07 > Author: Alexis Lucattini diff --git a/deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/gspread_helpers.py b/deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/gspread_helpers.py index ed11304..991e081 100644 --- a/deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/gspread_helpers.py +++ b/deploy/cttso-ica-to-pieriandx-cdk/lambdas/layers/lambda_utils/gspread_helpers.py @@ -163,7 +163,14 @@ def append_df_to_cttso_lims(new_df: pd.DataFrame, replace=False): new_df = new_df.replace({pd.NaT: None}).replace({'NaT': None}).replace({np.NaN: ""}) if replace: - # Add an extra 1000 rows to the bottom of the page + # We have to resize the sheet, rather than reset/replace + # Since the replace method also removes the formats + sheet_obj.sheet.resize(1, 1) + # Add the header + sheet_obj.df_to_sheet(pd.DataFrame(data=None, columns=new_df.columns), index=False) + # Reformat the sheet so that the portal_run_id is interpreted as a string + sheet_obj.sheet.format("P", {"numberFormat": {"type": "TEXT"}}) + # Update the sheet with the data sheet_obj.df_to_sheet( pd.concat( [ @@ -171,11 +178,18 @@ def append_df_to_cttso_lims(new_df: pd.DataFrame, replace=False): pd.DataFrame(columns=new_df.columns, index=range(1000)) ] ), - index=False, replace=True, fill_value="" + index=False, replace=False, fill_value="" ) else: # Get existing sheet existing_sheet = sheet_obj.sheet_to_df(index=0) + # We have to resize the sheet, rather than reset/replace + # Since the replace method also removes the formats + sheet_obj.sheet.resize(1, 1) + # Add the header + sheet_obj.df_to_sheet(pd.DataFrame(data=None, columns=new_df.columns), index=False) + # Reformat the sheet so that the portal_run_id is interpreted as a string + sheet_obj.sheet.format("P", {"numberFormat": {"type": "TEXT"}}) # Update the sheet object with the list sheet_obj.df_to_sheet( pd.concat( @@ -185,7 +199,7 @@ def append_df_to_cttso_lims(new_df: pd.DataFrame, replace=False): pd.DataFrame(columns=existing_sheet.columns, index=range(1000)) ] ), - index=False, replace=True, fill_value="" + index=False, replace=False, fill_value="" ) @@ -353,6 +367,12 @@ def append_rows_to_deleted_lims(to_be_deleted: pd.DataFrame): # Get existing sheet existing_sheet = sheet_obj.sheet_to_df(index=0) + # Since the replace method also removes the formats + sheet_obj.sheet.resize(1, 1) + # Add the header + sheet_obj.df_to_sheet(pd.DataFrame(data=None, columns=existing_sheet.columns), index=False) + # Reformat the sheet so that the portal_run_id is interpreted as a string + sheet_obj.sheet.format("P", {"numberFormat": {"type": "TEXT"}}) # Update the sheet object with the list sheet_obj.df_to_sheet( pd.concat( @@ -362,7 +382,7 @@ def append_rows_to_deleted_lims(to_be_deleted: pd.DataFrame): pd.DataFrame(columns=existing_sheet.columns, index=range(1000)) ] ), - index=False, replace=True, fill_value="" + index=False, replace=False, fill_value="" )