import urllib from google.oauth2.service_account import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"] def get_spreatsheed_service(credentials): return build("sheets", "v4", credentials=credentials, cache_discovery=False) def get_google_creds(): creds = None creds = Credentials.from_service_account_file("credentials.json", scopes=SCOPES) return creds def process_spreadsheet_data(rows): processed_data = [] for row in rows: processed_row = convert_spreadsheet_data(row) processed_data.append(processed_row) return processed_data def convert_spreadsheet_data(data): result = {} # Convert day to integer result['day'] = int(data['day']) # Convert time to a datetime object from datetime import datetime # Assuming Indonesian format with month names # Replace Indonesian month names with English if needed month_mapping = { 'Januari': 'January', 'Februari': 'February', 'Maret': 'March', 'April': 'April', 'Mei': 'May', 'Juni': 'June', 'Juli': 'July', 'Agustus': 'August', 'September': 'September', 'Oktober': 'October', 'November': 'November', 'Desember': 'December' } time_str = data['time'] for indo, eng in month_mapping.items(): time_str = time_str.replace(indo, eng) # Format: "Sabtu, Juli 13, 2024" -> "Saturday, July 13, 2024" # Removing the day of week to simplify parsing time_str = time_str.split(', ', 1)[1] # Remove "Sabtu, " result['time'] = datetime.strptime(time_str, '%B %d, %Y') # Convert percentage strings to floats # Handling format like "0,12%" -> 0.12 for key in ['plan', 'actual', 'gap']: # Replace comma with dot (European to US decimal notation) value = data[key].replace(',', '.') # Remove percentage sign value = value.rstrip('%') # Convert to float result[key] = float(value) / 100 # Divide by 100 to get the actual decimal value return result