From 209f6ecc955c7e84b91093225e5c8ceb3cd434f7 Mon Sep 17 00:00:00 2001 From: Ittipat Lusuk Date: Tue, 23 Jun 2026 11:32:05 +0700 Subject: [PATCH] [sheet-service] updated push price slot to sheet --- main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 545e44d..db96b2e 100644 --- a/main.py +++ b/main.py @@ -2773,15 +2773,40 @@ def sync_grist_to_sheets(country_key): worksheet.batch_clear(["A2:ZZ"]) worksheet.update(values=sheet_rows, range_name="A2") print(f"[{SERVICE_NAME}] Updated {len(rows)} rows to sheet '{sheet_name}'.") + elif sheet_key == "price": rows = fetch_grist_table_data(doc_id, "Price") - if rows: sheet_rows = [r["fields"] for r in rows] worksheet.batch_clear(["A2:ZZ"]) worksheet.update(values=sheet_rows, range_name="A2") print(f"[{SERVICE_NAME}] Updated {len(rows)} rows to sheet '{sheet_name}'.") + + all_worksheets = spreadsheet.worksheets() + price_slot_pattern = re.compile(r"^PriceSlot[1-9]\d*$") + + for ws in all_worksheets: + ws_name = ws.title + + if price_slot_pattern.match(ws_name): + try: + # Data from Grist by sheet name ("PriceSlot1") is Table ID + slot_rows = fetch_grist_table_data(doc_id, ws_name) + + if slot_rows: + slot_sheet_rows = [r["fields"] for r in slot_rows] + ws.batch_clear(["A2:ZZ"]) + ws.update(values=slot_sheet_rows, range_name="A2") + print(f"[{SERVICE_NAME}] Updated {len(slot_rows)} rows to dynamic sheet '{ws_name}'.") + else: + # Clear sheet data if grist not have data (Keep only Header) + ws.batch_clear(["A2:ZZ"]) + print(f"[{SERVICE_NAME}] Cleared data in sheet '{ws_name}' (No data in Grist).") + + except Exception as slot_err: + print(f"[{SERVICE_NAME}] Error syncing dynamic sheet {ws_name}: {slot_err}") + elif sheet_key in ["new-layout", "new-layout-v2"]: print(f"Fetching multiple tables from doc: {doc_id}...") all_tables = get_all_grist_tables(doc_id)