[sheet-service] updated push price slot to sheet
This commit is contained in:
parent
bfc1cc797a
commit
209f6ecc95
1 changed files with 26 additions and 1 deletions
27
main.py
27
main.py
|
|
@ -2773,15 +2773,40 @@ def sync_grist_to_sheets(country_key):
|
||||||
worksheet.batch_clear(["A2:ZZ"])
|
worksheet.batch_clear(["A2:ZZ"])
|
||||||
worksheet.update(values=sheet_rows, range_name="A2")
|
worksheet.update(values=sheet_rows, range_name="A2")
|
||||||
print(f"[{SERVICE_NAME}] Updated {len(rows)} rows to sheet '{sheet_name}'.")
|
print(f"[{SERVICE_NAME}] Updated {len(rows)} rows to sheet '{sheet_name}'.")
|
||||||
|
|
||||||
elif sheet_key == "price":
|
elif sheet_key == "price":
|
||||||
rows = fetch_grist_table_data(doc_id, "Price")
|
rows = fetch_grist_table_data(doc_id, "Price")
|
||||||
|
|
||||||
if rows:
|
if rows:
|
||||||
sheet_rows = [r["fields"] for r in rows]
|
sheet_rows = [r["fields"] for r in rows]
|
||||||
worksheet.batch_clear(["A2:ZZ"])
|
worksheet.batch_clear(["A2:ZZ"])
|
||||||
worksheet.update(values=sheet_rows, range_name="A2")
|
worksheet.update(values=sheet_rows, range_name="A2")
|
||||||
print(f"[{SERVICE_NAME}] Updated {len(rows)} rows to sheet '{sheet_name}'.")
|
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"]:
|
elif sheet_key in ["new-layout", "new-layout-v2"]:
|
||||||
print(f"Fetching multiple tables from doc: {doc_id}...")
|
print(f"Fetching multiple tables from doc: {doc_id}...")
|
||||||
all_tables = get_all_grist_tables(doc_id)
|
all_tables = get_all_grist_tables(doc_id)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue