Using link to download a google drive document

I’m trying to have a clickable link which results in a google sheets document being downloaded.

Code as follows:

from anvil import *
import anvil.server
import google.auth, google.drive, google.mail
from google.drive import app_files

class form1 (form1Template):
  def __init__(self, **properties):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**properties)

  def link_1_click (self, **event_args):
    self.link_1.url = app_files.file1.get_url()

Any advice how to use the url property in this context?

Thank you!

Ah, this is really close to working! The url property tells the link where to go when it is clicked, so you need to set it before the link is clicked. Then you don’t need the link_1_click event handler.

I think you probably want something like this:

from anvil import *
import anvil.server
import google.auth, google.drive, google.mail
from google.drive import app_files

class form1 (form1Template):
  def __init__(self, **properties):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**properties)
    self.link_1.url = app_files.file1.get_url()

Does that work?

Thank you! I unfortunately get the error in output:

AttributeError: ‘Sheet’ object has no attribute ‘get_url’
at form1, line 10
called from main, line 30

In Services --> Google API --> Access files from your google drive:
I can see the correct file which is read only.

Do you know what else I am missing?
Thank you!

Ah, yes, sorry. You can get URLs for real files in Google Drive, but not for Google Sheets. This is because they’re not real files, so they can’t be downloaded through Anvil in the way that you can, say, export them to Excel through the Google Sheets interface.

If you want to link people to a Google Sheet, the best bet is to use the standard Google Sheets interface to do it. You’ll want to set the file to be read-only for anyone with the link, then paste that URL into the url property of the link on your Anvil form.

Will that work for your use-case?

Ok, I have made it a google drive document instead and now it is fully downloadable as hoped - thank you very much!