I have Google Form for new property listings.
When the form is submitted, I would like to have a folder created in the parent folder titled the name inputted (Form Entry Field 1) and contain the images uploaded by that form (Form Entry Field 3). So effectively, a folder created on every form entry.
In the Google form, using Script Editor and I have inputted this code below with the parent folder ID. I have also setup a Trigger for onFormSubmit.
However, Line 5 is throwing up an error TypeError: Cannot read property 'getItemResponses' of undefined
Please understand, I am a beginner at this type of code, so I'm not sure and a little out my depth.
Is this task possible?
Thanks
function onFormSubmit(e) {
const folderId = "1VXzzzzzzzzzzzzzz";
const form = FormApp.getActiveForm();
const formResponses = form.getResponses();
const itemResponses = formResponses[formResponses.length-1].getItemResponses();
Utilities.sleep(3000); // This line might not be required.
// Prepare the folder.
const destFolder = DriveApp.getFolderById(folderId);
const folderName = itemResponses[0].getResponse();
const subFolder = destFolder.getFoldersByName(folderName);
const folder = subFolder.hasNext() ? subFolder : destFolder.createFolder(folderName);
// Move files to the folder.
itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}
CodePudding user response:
I think the main problem is the way you get the answers: formResponses[formResponses.length-1].getItemResponses();
.
Since each of the answers occupies a place in the array it is easy to keep track of them. If question 1 occupies place 0, question 2 occupies place 1, and so on.
The following example is tested and works:
function handleForms() {
/* Initialize an empty array for save the folders */
let folders = []
const form = FormApp.getActiveForm()
for (const res of form.getResponses()) {
/* Getting the responses in this example only are two of them */
const folderName = res.getItemResponses()[0].getResponse()
const imgResponse = res.getItemResponses()[1]
/* Create a new folder with the name */
const folder = DriveApp.createFolder(folderName)
/* Iterate to move the files to the new folder */
for (const img of imgResponse.getResponse()) {
DriveApp.getFileById(img).moveTo(folder)
}
Logger.log(folders)
folders.push(folder)
}
/* Iterate over the folders to move them to the main one */
const mainFolder = DriveApp.getFolderById('<YOUR_FORM_FOLDER>')
folders.forEach(f => DriveApp.getFolderById(f.getId()).moveTo(mainFolder))
}
Update
If you want to keep the original files, just add this two lines inside image iteration:
const newIMG = DriveApp.getFileById(img).makeCopy()
newIMG.moveTo(folder)
Update 2
For control if the file already exist you can implement a control to check if the folder exists just before creating it.
const checker = DriveApp
.getFolderById(MAIN_FOLDER)
.getFoldersByName(folderName)
if (checker.hasNext()) continue
/* Create a new folder with the name */
const folder = DriveApp.createFolder(folderName)