The_Learner
The_Learner

Reputation: 619

Allow file download from google drive only after submitting the google form

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

Upvotes: 2

Views: 5835

Answers (2)

Iamblichus
Iamblichus

Reputation: 19339

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:

Upvotes: 2

James D
James D

Reputation: 3152

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.

Upvotes: 1

Related Questions