Script for Google Form

213 views
Skip to first unread message

Olivier Robillard

unread,
Nov 19, 2021, 7:13:52 PM11/19/21
to Google Apps Script Community
I have a google form that has two file upload sections. I want to be able to upload those files to two different folders. The files in the first file upload section should go in Folder one and the files in the second file upload section should go in folder two. I also want to automatically name the folders based on the name of the senders. Folders should go as follow: sendername_1, sendername_2, etc, if the senders have the same name. How would I make this happen? I found the script below but it seems to only work with folder 1. Folder 2 has the files from the second file upload section but it doesn't automatically create a folder for them.

function onFormSubmit(e) {
  const folderId = "1y40PNXYXSm7Ue_b6nbVnauz7dzKiKj4u";  // Please set top folder ID of the destination folders.

  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[2].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}

Clark Lind

unread,
Nov 20, 2021, 10:40:28 AM11/20/21
to Google Apps Script Community
Change the folder ID, otherwise, I was able to get this code to work. 

function onFormSubmit(e) {
    const folderId = "XXXXX add top folder ID XXXXX"; // Please set top folder ID of the destination folders.
    const form = FormApp.getActiveForm();
    const formResponses = form.getResponses();
    const length = formResponses.length;

    //if email exists in contacts, get full name, otherwise use email as name
    const email = formResponses[length - 1].getRespondentEmail();
    let name = "";
    if (ContactsApp.getContact(email) == null) {
        name = email
    } else(
        name = ContactsApp.getContact(email)
        .getFullName()
    )

    // Prepare the folders.
    const itemResponses = formResponses[length - 1].getItemResponses();
    const destFolder = DriveApp.getFolderById(folderId);
    let childFolder;

    for (let i = 0; i < itemResponses.length; i++) {
        childFolder = destFolder.createFolder(`${name} - ${i+1}`);
        itemResponses[i].getResponse()
            .forEach(id => DriveApp.getFileById(id)
                .moveTo(childFolder));
    }
}

Yousuf Ahmed

unread,
Nov 22, 2021, 6:57:00 AM11/22/21
to Google Apps Script Community
Change the drive folder id and  with link google sheet 
Reply all
Reply to author
Forward
0 new messages