Ways to get attachments that are submitted through a Google form into a Google drive folder - google-apps-script

I have created a Google form and I want an app script code that fetches all submitted documents and put it inside a folder. It should be a onformSubmit trigger, when a user uploads the documents and submits the form, the documents should be saved inside one folder.
I tried a for loop where for every response, the code checked that whether it was a FILE_UPLOAD and if it was, it stored it's data in a blob. Else it skipped it. Then I used the createFile (blob) where the blob is the var I used to store the responses that are FILE_UPLOAD.

Related

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
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:
FormTriggerBuilder
Event objects: Form submit
File.addEditor(emailAddress)
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.
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.

Google Apps Script Webhook to Save any Input to a Google Drive File (JSON?)

I would like to use Apps Script to save anything sent to the Web App URL as a text file on Google Drive. I believe that this should be done with a basic doGet and doPost setup.
If I could get the file saved with a unique filename, that'd be great, but it's not important. I thought there would be a tutorial somewhere about this, but I can't seem to find it at all.
A basic workflow of what you need to do:
Create a WebApp with a doGet() function
Retrieve the data your application has sent to your WebApp url with e.parameter or e.parameters - depending on either it's a single string or an array, your retrieved data should be assigned to certain key
If the key is called "test" and your data is passed as https://script.google.com/a/XXX/macros/s/XXX/exec?test=Hello, you should query for myString = e.parameter.name
Create a text file on your drive with DriveApp.createFile(name, content, mimeType) - specifying a name of your choice, passing the retrieved string as content and chosing mimeType PLAIN_TEXT

Programatically retrieve files uploaded to a Google Form

Looking to automate the processing of data in a spreadsheet generated by google forms; specifically, I want to attach the files uploaded to the form to outgoing emails as attachments. The files can be synced to a local folder for the program to access, but the google form only has a url for each uploaded file.
What would be the most efficient way to determine which of the files uploaded correspond with each form submission and its corresponding link?
Edit: my research indicates that I may be able to use an identifier from the url to see which document it is through an apps api. Another thought was to scrape the html from the linked webpage and then to glean the file name from somewhere in the html here it hopefully occurs with regularity.
Then I could use the filename to construct a filepath to the synced folder on my local machine or would it be better to use the drive api to manipulate the file into an email attachment?

Google app script import CSVs

Okay. Multiple people use the scanpet app to create csv files of 3 columns [uniformly structured, no headers in csv].
I'd like them to email the created file to a gmail account, then go to a google form. I realize for this to work it's have to be done after each email submitted and that is okay... please read to end.
I'd like to make a google form with only one option, just to enter a record to trigger an event. Link the form to a google sheet. I'd like this sheet to house the script I'm asking help for in the script editor, and the google sheet to then have two other tabs named Transfer and MailOrder each with the same 3 columns as the csv files.
After the employee sends the file he completes the google form.
script triggers on form submit, and will grab the the latest csv attachment from gmail and depending on the subject line of the email (Mail Order or Transfer) add the values to the existing data entries under the tab with the same name as the subject line.

Google Form Submit > Attach New Responses to Email and Send

I have a script that turns a submitted form into a PDF and emails it automatically but I can't seem to get my head around if it is possible to implement a function i would like: -
User fills in Google form
Submits form
This creates entirely new spreadsheet based on same headers from the form
Emails thie spreadsheet as an .xlsx attachment automatically to a specific email address
Is this possible? I feel like I am missing a simple way to do this but I am having a moment of noobishness! :-)
1, 2, 3 - all quite possible.
4 - Not as such. There's no support in Google Apps Script for converting a Sheet to Excel format. However you could produce a CSV file which would be understood by Excel or any other spreadsheet program, and email that as an attachment.
Generating a CSV is covered in one of the Apps Script tutorials. See Saving a selected Range to a CSV file from Tutorial: Interacting With Your Docs List.
An example of attaching files from Drive is provided in the documentation for MailApp.sendEmail().