google-apps-scriptgoogle-forms

sharing the folder created by google form and google script


I am a complete beginner and that is why I used an almost ready solution. However, the subsequent changes were beyond me

I used the solution available here to move the uploaded file to an individual subfolder

https://developers.google.com/apps-script/samples/automations/upload-files?hl=pl

Hhen I tried to share the subfolder. I modified the function as follows

function getSubFolder_(objParentFolder, subFolderName) {

  // Iterates subfolders of parent folder to check if folder already exists.
  const subFolders = objParentFolder.getFolders();
  while (subFolders.hasNext()) {
    let folder = subFolders.next();

    // Returns the existing folder if found.
    if (folder.getName() === subFolderName) {
      return folder;
    }
  }
  // Creates a new folder if one doesn't already exist.
  return objParentFolder.createFolder(subFolderName)
    .setDescription(`Created by ${APP_TITLE} application to store uploaded Forms files.`)

    //
    .createPermission("emailuser@gmail.com", "writer");
}

Ultimately I would like the email address to be created based on one of the responses from the form.


Solution

  • Passing an Email based on OnFormSubmit(e)

    Please feel free to raise any questions if you have one, because as of the moment I have limited information about your whole code and how does your form looks like.

    Codes to be added based on the Sample you have sent:

    You need to add something to grab the email from the responses. Assuming that you follow the shared documents in the article.

    So based on the Link:

    From this:

    let itemResponses = e.response.getItemResponses();
    

    To this

    let itemResponses = e.response.getItemResponses();
    var email = itemResponses[0].getResponse();//Change the Index by the counting where does the question falls and subtract 1. I.E: If the email is on question 2 then the Index or [0] should be on 1.
    

    Note: It is just a declaration I am just making sure that it is declared correctly, based on the placement where it is supposed to happen on the code.

    Please edit, something that resembles this in your code, that is currently not shared:

    From this:

    destFolder = getSubFolder_(destFolder, subFolderName);
    

    To this

    destFolder = getSubFolder_(destFolder, subFolderName, email);
    

    Finally Update your Called Functions like this:

    function getSubFolder_(objParentFolder, subFolderName, email) {
    
      // Iterates subfolders of parent folder to check if folder already exists.
      const subFolders = objParentFolder.getFolders();
      while (subFolders.hasNext()) {
        let folder = subFolders.next();
    
        // Returns the existing folder if found.
        if (folder.getName() === subFolderName) {
          return folder;
        }
      }
      // Creates a new folder if one doesn't already exist.
      return objParentFolder.createFolder(subFolderName)
        .setDescription(`Created by ${APP_TITLE} application to store uploaded Forms files.`)
    
        //
        .createPermission(email, "writer");
    }
    

    Info: What the code modification does is grab the email from the responses, pass the data on the parameters of the called function to have a more dynamic permission sharing.

    Reference:

    Event Objects

    Form Response