Skip to content

Commit

Permalink
update update_sheets.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ceroberoz committed Aug 22, 2024
1 parent d54a03c commit 8cdf3b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/update-sheets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
run: pip install -r requirements.txt

- name: Update Google Sheets
env:
SHEET_ID: ${{ secrets.GOOGLE_SHEET_ID }}
run: python update_sheets.py
env:
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }}
SHEET_ID: ${{ secrets.SHEET_ID }}
50 changes: 35 additions & 15 deletions update_sheets.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import os
import json
import datetime
import pandas as pd
from google.oauth2 import service_account
from googleapiclient.discovery import build

# Set up credentials
gcp_sa_key = os.environ.get('GCP_SA_KEY')
if not gcp_sa_key:
raise ValueError("GCP_SA_KEY environment variable is not set")

try:
service_account_info = json.loads(gcp_sa_key)
except json.JSONDecodeError:
raise ValueError("GCP_SA_KEY is not a valid JSON string")

creds = service_account.Credentials.from_service_account_info(
eval(os.environ['GCP_SA_KEY']),
service_account_info,
scopes=['https://www.googleapis.com/auth/spreadsheets']
)

# Build the Sheets API service
service = build('sheets', 'v4', credentials=creds)

# Get the Sheet ID from environment variable
SHEET_ID = os.environ['SHEET_ID']
SHEET_ID = os.environ.get('SHEET_ID')
if not SHEET_ID:
raise ValueError("SHEET_ID environment variable is not set")

# Read the existing merged CSV file
csv_file = 'public/merged.csv'
Expand All @@ -34,19 +46,27 @@
spreadsheetId=SHEET_ID,
range=f'{sheet_name}!A1:Z'
).execute()
except:
except Exception as e:
print(f"Error clearing sheet: {e}")
# If sheet doesn't exist, add a new one
service.spreadsheets().batchUpdate(
spreadsheetId=SHEET_ID,
body={'requests': [{'addSheet': {'properties': {'title': sheet_name}}}]}
).execute()
try:
service.spreadsheets().batchUpdate(
spreadsheetId=SHEET_ID,
body={'requests': [{'addSheet': {'properties': {'title': sheet_name}}}]}
).execute()
except Exception as e:
print(f"Error creating new sheet: {e}")
raise

# Update sheet with new data
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=f'{sheet_name}!A1',
valueInputOption='RAW',
body={'values': values}
).execute()

print("Google Sheets updated successfully with merged data!")
try:
service.spreadsheets().values().update(
spreadsheetId=SHEET_ID,
range=f'{sheet_name}!A1',
valueInputOption='RAW',
body={'values': values}
).execute()
print("Google Sheets updated successfully with merged data!")
except Exception as e:
print(f"Error updating sheet: {e}")
raise

0 comments on commit 8cdf3b7

Please sign in to comment.