I need to download an excel sheet from Google Docs via Gspread and then multiple times I'll need to read the values of different cells in 'A1' notation. Thus, I can't just get the spreadsheet and then call val = worksheet.acell('B1').value
, because the script will freeze out of too many API calls. My solution for now:
def download_hd_sheet():
worksheet = gc.values().get(spreadsheetId=excel_id, range='variables', valueRenderOption='FORMULA').execute()['values']
df = pd.DataFrame(worksheet)
writer = pd.ExcelWriter("Variables.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False, header=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
writer.save()
book = openpyxl.load_workbook('Variables.xlsx', data_only=False)
global hd_sheet
hd_sheet = book.active
So far what I'm doing is:
- I download the values from the worksheet.
- Transform it (list of lists) into a pandas dataframe.
- Then I write the df to a .xlsx file.
- I read the .xlsx file to a global variable
It seems to me that I am doing so many things just to achieve something that can be done in two lines. Please, let me know what would be more effective than the above.