added parameter onlydigits

This commit is contained in:
alejandro 2023-05-11 16:11:25 +02:00
parent 6d0650b6c6
commit f414043d1c
1 changed files with 29 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import argparse,json,sys
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from os import remove
import re
import base64
@ -21,7 +22,7 @@ Version = {__version__}
Manual execution
python3 pandora_googlesheets.py --creds_json/creds_base64 <file credentials> --name <name document> --sheet <name-sheet> --cell <Number cell> --row <number-row> --column <number-column>
./pandora_googlesheets --creds_json/creds_base64 <file credentials> --name <name document> --sheet <name-sheet> --cell <Number cell> --row <number-row> --column <number-column>
"""
@ -33,13 +34,16 @@ parser.add_argument('--cell', help='To collect the value of a cell.')
parser.add_argument('--row', help='To collect the value of a row.',type=int)
parser.add_argument('--column', help='To collect the value of a column.',type=int)
parser.add_argument('--sheet', help='To indicate the name of the document sheet, put it in quotation marks and count spaces and capital letters.',type=str)
parser.add_argument('--onlydigits', help='To parse the value of the cell if its not a digit',default=0)
args = parser.parse_args()
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
def convert_to_number(s):
cleaned_value = re.sub("[^0-9]", "", s)
return int(cleaned_value) if cleaned_value else 0
## authenticate with file json input
if args.creds_json is not None and args.creds_base64 == None:
@ -56,14 +60,34 @@ else:
print("You need to use the --creds_json or creds_base 64 parameter to authenticate. You can only select one.")
sys.exit()
client = gspread.authorize(creds)
try:
client = gspread.authorize(creds)
except Exception as e:
print("Error authenticating with credentials:", e)
sys.exit()
sheet = client.open(args.name) # Open the spreadhseet
worksheet = sheet.worksheet(args.sheet) # Open worksheet
try:
sheet = client.open(args.name) # Open the spreadsheet
except gspread.exceptions.SpreadsheetNotFound as e:
print(f"Error: Spreadsheet '{args.name}' not found.")
sys.exit()
try:
worksheet = sheet.worksheet(args.sheet) # Open worksheet
except gspread.exceptions.WorksheetNotFound as e:
print(f"Error: Worksheet '{args.sheet}' not found.")
sys.exit()
if args.cell is not None and args.row==None and args.column==None :
val = worksheet.acell(args.cell).value
if int(args.onlydigits)==1:
try:
val = convert_to_number(val)
except ValueError as e:
print(e)
elif args.row is not None and args.column==None and args.cell == None:
@ -77,5 +101,4 @@ else:
print("To search for data in a cell use the --cell parameter, for data in a column --column and in a row --row, only one of these parameters can be used at a time.")
sys.exit()
print(val)