I made a GH script that uses python to retrieve data from Google Sheets using code from one Erik Rood. It works with a piece of json code to give authorization to the Google Sheets that sits in the same folder as the GH file. I had it working on Mac but i can’t get it to work on Windows. Here is the code:
# r: gsheets, pygsheets, pandas
#https://erikrood.com/Posts/py_gsheets.html
import pygsheets
import os
path = os.path.dirname(ghdoc.Path)
#https://discourse.mcneel.com/t/issues-installing-pandas-on-script-editor/166071/10
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
import pandas as pd
import scriptcontext as sc
from io import StringIO
#authorization
gc = sc.sticky['authorization']
naam = naam
index = 5
# Define a function to get CSV from a specific sheet tab
def get_csv_from_tab(sh, index):
wks = sh[index]
data = pd.DataFrame(wks.get_all_records())
#csv_buffer = StringIO()
return data.to_csv(sep=';', index=False)
#return csv_buffer.getvalue()
# Open the Google spreadsheet
sh = gc.open(naam)
# Manually select the tabs by index and store their CSVs in separate variables
Sheet1 = get_csv_from_tab(sh, 6)
Sheet2 = get_csv_from_tab(sh, 2)
Sheet3 = get_csv_from_tab(sh, 3)
Sheet4 = get_csv_from_tab(sh, 4)
Sheet5 = get_csv_from_tab(sh, 7)
Are there some obvious mistakes or common pitfalls that explain why it won’t work on Windows? I believe the error i was getting was that pygsheets wasn’t installed. Would that perhaps solve it? If so what is a sure way to do that? The windows comp is somewhere else and i would prefer to avoid a lot of trial and error. Just downloading the pygsheets package and putting it in the correct folder seemed easiest, but i couldn’t find the folder…
Hi Ehsan, i am running version 8 and the error says: 1. Error running script: unknown locale: en-US [4:1]
Seems like an easy fix but as it is happening on someone else’s computer my problem is also that i want to avoid a lot of trial and error. Do you have any suggestions?
if you are getting this unknown locale error it is due to the difference between en-US that Windows sometimes reports and en_US which is the valid locale name that python likes. I have a code snippet in the script editor that attempts to automatically correct this error and set the correct locale.
On the machine that has the problem:
Open Script Editor
Run RhinoCodeLogs command in Rhino
Check the report for Python default locale is ...
There should be a few report lines there that list the default and current locale and also if there has been any corrections made to the locales.
This is a report on my Windows 11 machine. Note that there are no messages about updating the locale since it is correctly reported as en_US by Windows
Info 12/9/2024 11:03:22 PM [RhinoCode] Python default locale is 'en_US.cp1252'
Info 12/9/2024 11:03:22 PM [RhinoCode] Python current locale is 'English_United States.1252'
Hi Ehsan, sorry, did not get a chance to try getting the locale on my clients computer yet. I do know it is a brand new PC running Windows 11, but i guess it might still be wrongly configured as en-US or something. However, that code snippet sounds very handy, since i am trying to fix things at a difference. Do you mind sharing it?