I'm trying to create a Google form which collects documents, and stores it into a specific folder (with a specific name that is submitted in the form). The code mostly works correctly when users upload a single file in the question where they need to upload a file, but runs into problems when someone uploads multiple files into a question. I think since the response is stored in a comma separated format for more than one file, my code is not reading it as an ID.
Here is my code
function saveDetailsInFolder(e) {
var submissionResponses = e.response.getItemResponses();
var name = submissionResponses[0].getResponse(); //get name submitted in the form
var parentFolder = DriveApp.getFolderById("My desired parent folder"); //get the parent folder, put the ID in the quotes
var newFolder = parentFolder.createFolder(name); //create a new folder with the name submitted in the form
var fileUploads = submissionResponses.filter(item => item.getItem().getType().toString() === 'FILE_UPLOAD');
for (var x=0; x<fileUploads.length;x++){
var fileUploadID = fileUploads[x].getResponse();
DriveApp.getFileById(fileUploadID).makeCopy(newFolder)
}
}
From some testing, I came to the conclusion that for questions where users upload multiple files, form stores it in a comma separated format, and when it is passed in the variable for "getFileById", it's unable to get it.
I need a way to get all documents. One way I can personally think of, is combining all response IDs, single file.upload questions and multiple file upload questions in an array and go through it one by one and upload them to the folder, but I can't seem to do it due to insufficient knowledge about arrays.
Please know if there's a solution for this, or a different method that I can use to get the desired outcome.
In your script, how about the following modification?
function saveDetailsInFolder(e) {
var submissionResponses = e.response.getItemResponses();
var name = submissionResponses[0].getResponse();
var parentFolder = DriveApp.getFolderById("My desired parent folder"); // Please set your folder ID.
var newFolder = parentFolder.createFolder(name);
var fileUploads = submissionResponses.filter(item => item.getItem().getType().toString() === 'FILE_UPLOAD');
for (var x = 0; x < fileUploads.length; x++) {
var fileUploadID = fileUploads[x].getResponse();
if (!Array.isArray(fileUploadID)) {
fileUploadID = [fileUploadID];
}
fileUploadID.forEach(id => DriveApp.getFileById(id).makeCopy(newFolder));
}
}
fileUploadID
. On the other hand, when multiple files are uploaded, an array including the file IDs is returned.fileUploadID
is not an array, the value of fileUploadID
is put into an array. And, the file IDs of an array of fileUploadID
are processed in the loop.