2

I have a blog built using Jekyll and hosted on Github. In my blog posts, some times I need to share some downloadable files stored on google drive.

At present, I want to know, is there a way to allow file download from google drive only after filling and submitting a google form.

Thank you

The_Learner
  • 589
  • 1
  • 6
  • 16

2 Answers2

2

With Apps Script, you can create a function that will run when the form is submitted, and will do the following actions:

  • Get the email address of the user that submitted the form.
  • Share the desired file with this email address (this action will automatically send the user an email with a link to the file).
  • Optionally, you can send a customized email to the user with the file download link.

In order to make this function run when the form is submitted, you have to install an onFormSubmit trigger. To do that, while on the script bound to your Form, create the function (let's call it shareLinkOnFormSubmit) and install the trigger, either manually or programmatically (by copying and running this function — you should change the form id and the function name).

  • Next, the function shareLinkOnFormSubmit could be something similar to the sample below — check inline comments:
function shareLinkOnFormSubmit(e) {
  var formResponse = e.response;
  var email = formResponse.getRespondentEmail(); // Get form respondent email address
  var fileId = "your-file-id"; // Get file id (change accordingly)
  var file = DriveApp.getFileById(fileId);
  file.addEditor(email); // Share the file with the respondent (edit access).
  var downloadUrl = file.getDownloadUrl(); // Get download URL (only works for non G-Suite documents)
  MailApp.sendEmail(email, "Your file link", downloadUrl); // Send email with download URL
}

Notes:

  • Event objects are used to get the respondent email. Beware, this email will only be populated if the option Collect email addresses is selected.
  • In the sample, edit access is given to the user. Use addCommenter or addViewer instead of addEditor if you don't want the user to be able to edit this.
  • The method getDownloadUrl() only works for non G-Suite documents. For these type of documents, you should build the download link as in this answer:

https://www.googleapis.com/drive/v3/files/{your-file-id}/export&mimeType={your-mime-type}

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
1

Yes, there are a number of way of doing it. The most straightforward way is to share the file as anyone with the link can view.

Add the file id to the following direct download link:: https://drive.google.com/uc?export=download&id=YOUR FILE ID

You can then create a bit ly link to make it a little more presentable

In the presentation settings of the form, you can then add a message and the download link.

enter image description here

There are obvious drawbacks to this such as the ability of the person submitting the form to share the link to other people.

If you wanted to prevent this from happening you can write a script to add read only permissions for the person submitting the form to be able to download the file.

James D
  • 3,102
  • 1
  • 11
  • 21
  • thank you very much for your answer. I don't want to add the download link to the presentation settings of the form because I don't want to create separate forms for each file. I am expecting, for every download link only one form should open, after the form submussion, download should start automatically. – The_Learner Mar 28 '20 at 15:36
  • Ah ok, no, that's not possible with forms, you'll have to make a custom form through a web app – James D Mar 28 '20 at 15:38
  • thank you very much James. Or atleast is there a way to trigger a apps-script function, when a google drive file download URL gets clicked from a random website, other than google sites (new/old) – The_Learner Mar 28 '20 at 15:55
  • It may be possible again, using a published web app. Your questions are too vague to give an accurate answer. I suggest you edit your question, include as much information and details as possible to help with answering the question. – James D Mar 28 '20 at 16:21