Created
March 16, 2025 21:41
-
-
Save Cristopheer96/678e5bcb3de7bd59db04ccb8250dcc45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gspread | |
from google.oauth2.service_account import Credentials | |
SCOPES = [ | |
"https://www.googleapis.com/auth/spreadsheets", | |
"https://www.googleapis.com/auth/drive", | |
] | |
SERVICE_ACCOUNT_FILE = ( | |
"/Users/cristopher/Desktop/gsheet_api/gentle-dominion-453920-r8-f2a7cc04be1f.json" | |
) | |
def get_gspread_client(): | |
credentials = Credentials.from_service_account_file( | |
SERVICE_ACCOUNT_FILE, scopes=SCOPES | |
) | |
client = gspread.authorize(credentials) | |
return client | |
def open_sheet(sheet_name: str, worksheet_index: int = 0): | |
client = get_gspread_client() | |
sheet = client.open(sheet_name).get_worksheet(worksheet_index) | |
return sheet | |
def get_all_records(sheet): | |
return sheet.get_all_records() | |
def get_cell(sheet, row: int, col: int): | |
return sheet.cell(row, col).value | |
def update_cell(sheet, row: int, col: int, value): | |
sheet.update_cell(row, col, value) | |
def append_row(sheet, row_values: list): | |
sheet.append_row(row_values) | |
def print_menu(): | |
""" ----Muestra el menude opciones disponibles. ---""" | |
print("\n--- Menú de Opciones ---") | |
print("1. Imprimir todos los registros") | |
print("2. Consultar valor de una celda") | |
print("3. Actualizar valor de una celda") | |
print("4. Agregar una nueva fila") | |
print("5. Salir") | |
def main(): | |
print("=== NALA TESTS - GSHEETS ===\n") | |
sheet_name = input("Ingrese el nombre del Google Sheet: ").strip() | |
try: | |
worksheet = open_sheet(sheet_name) | |
except Exception as e: | |
print( | |
"Error al abrir el Google Sheet. Verifique el nombre y que la cuenta de servicio tenga permisos de acceso.", | |
e, | |
) | |
return | |
while True: | |
print_menu() | |
opcion = input("Seleccione una opción (1-5): ").strip() | |
if opcion == "1": | |
records = get_all_records(worksheet) | |
print("\n--- Todos los registros ---") | |
if records: | |
for record in records: | |
print(record) | |
else: | |
print("El sheet está vacío.") | |
print("-------------------------") | |
elif opcion == "2": | |
try: | |
row = int(input("Ingrese el número de fila de la celda (1-indexado): ")) | |
col = int( | |
input("Ingrese¡el número de columna de la celda (1-indexado): ") | |
) | |
except ValueError: | |
print("Error: Ingrese números válidos para fila y columna.") | |
continue | |
current_value = get_cell(worksheet, row, col) | |
print(f"El valor actual en la celda ({row},{col}) es: {current_value}") | |
elif opcion == "3": | |
try: | |
row = int(input("Ingrese el número de fila de la celda (1-indexado): ")) | |
col = int( | |
input("Ingrese el número de columna de la celda (1-indexado): ") | |
) | |
except ValueError: | |
print("Error: Ingrese números válidos para fila y columna.") | |
continue | |
current_value = get_cell(worksheet, row, col) | |
print(f"El valor actual en la celda ({row},{col}) es: {current_value}") | |
confirm = input("¿Desea actualizar esta celda? (S/N): ").strip().lower() | |
if confirm == "s": | |
new_value = input("Ingrese el nuevo valor: ") | |
update_cell(worksheet, row, col, new_value) | |
print( | |
f"El valor en la celda ({row},{col}) ha sido actualizado a: {get_cell(worksheet, row, col)}" | |
) | |
else: | |
print("No se realizó ningún cambio en la celda.") | |
elif opcion == "4": | |
row_values = input( | |
"Ingrese los valores de la nueva fila, separados por comas: " | |
).split(",") | |
row_values = [valor.strip() for valor in row_values] | |
append_row(worksheet, row_values) | |
print("Nueva fila agregada exitosamente.") | |
elif opcion == "5": | |
print("Saliendo del programa.") | |
break | |
else: | |
print("Opcinn no válida. Por favor seleccione una opción del 1 al 5.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment