Creating a google sheet from client code

Problem statement:
I want to be able to create and delete google sheets on the fly and not have to manually add them from the anvil Google API dashboard or manually create one in my google drive. Based on the Google developer python API documentation, this should be possible using the google-api-python-client

What I have tried:
I attempted to create a google sheet in a specified folder in my google drive from client code with the following line of code:

anvil.google.drive.app_files.my_folder.create_file(ā€˜filename’), but this creates a google doc not a google sheet.

I thought that this could be remedied by adding the google sheet file extension as follows:
anvil.google.drive.app_files.my_folder.create_file(ā€˜filename.GSheet’), but I still get a google doc (albeit with a .GSheet extension)

Question:
How do I remedy this? Is this a limitation in Anvil?

Best regards

solasoy

there’s a content_type keyword argument which defaults to text/plain
You can change this to be: ā€œapplication/vnd.google-apps.spreadsheetā€

sheet = app_files.my_folder.create_file('filename', content_type="application/vnd.google-apps.spreadsheet")

See mimetypes for google files:

Google Workspace and Google Drive supported MIME types  |  Google for Developers

3 Likes

This solves the problem - thanks!