I have created a custom form on Google Form which contains an Upload File Question (allowing multiple files). I'm trying to run a script when submitting the form that creates a folder into a specific destination in Drive and that it saves the files uploaded in this one. But I have no idea how to retrieve the ID of the files uploaded and move them into the new folder. Thank you
I managed to create the new folder, but I'm not able to select the uploaded file and save them (or move them) to the target folder. I'm not posting the creating folder code.
function onFormSubmit(e) {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
var itemResponse = itemResponses[1];
var uploadedfile = itemResponse.getResponse();
//now??
}
I would like to get the ID of the file but using the .getItem().getID() method I get something like 1.148501776E9 Also how can I loop through the uploaded files?
onFormSubmit()
.If my understanding is correct, how about this modification? Please think of this as just one of several answers.
.getItem().getId()
is the item's unique identifier.getResponse()
of itemResponses
has the file IDs of uploaded files. You can use this.Before you use this script, please set the destination folder ID to var folderId = "###"
.
When you modified the script, please delete the OnSubmit event trigger once and install the trigger again. At that time, if the authorization screen is displayed. Please authorize it. By this, the script works. Please be careful this.
function onFormSubmit(e) {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
// I modified below script.
Utilities.sleep(3000);
var folderId = "###"; // Please set folder ID of the destination folder.
var destfolder = DriveApp.getFolderById(folderId);
for (var i = 0; i < itemResponses.length; i++) {
if (itemResponses[i].getItem().getType() == "FILE_UPLOAD") {
var ids = itemResponses[i].getResponse();
for (var j = 0; j < ids.length; j++) {
var file = DriveApp.getFileById(ids[j]);
destfolder.addFile(file);
file.getParents().next().removeFile(file);
}
}
}
}
Utilities.sleep(3000)
is not used, there was the case that an error occurs because the file is tried to move before the upload of file is not finished. So I put it. If in your environment, no error occurs without it, please remove it.If I misunderstood your question and this was not the result you want, I apologize.