Chris Anderson
Chris Anderson

Reputation: 31

Specify folder to save files to in Google Script

I have a simple form published through Google Apps Scripts that lets someone upload a file anonymously. The script works well, but it will always save to the root of my Google Drive instead of to a specific folder. I'd like it to create a new folder with the person's first name + last name (collected from the form) within a specific folder. I know I need to use the destination FolderID, just not sure how to use it properly.

Here's my existing code:

function uploadFiles(form) {

  try {

var dropbox = form.FirstName + form.LastName;
var folder, folders = DriveApp.getFoldersByName(dropbox);

if (folders.hasNext()) {
  folder = folders.next();
} else {
  folder = DriveApp.createFolder(dropbox);
}

var blob = form.myFile;    
var file = folder.createFile(blob);    
file.setDescription("Uploaded by " + form.Name);

return "Thank you, your file was uploaded successfully!";

Upvotes: 2

Views: 14610

Answers (1)

Břeťa
Břeťa

Reputation: 122

I'm not sure if I understood well what is your goal. But if you want to create folder 'FirstnameLastname' within some specific folder, you should first select this 'parent' folder and then use it for creating new one.

EDIT:

Summary

First look at the reference from Google. You use class DriveApp and methode .createFolder(). Google cheat sheet says, that this methode within the DriveApp class 'Creates a folder in the root of the user's Drive with the given name.'. This methode returns folder as return type (your variable type will be folder).

This is the reason why your code creates folders only in root folder.

Look in the cheatsheet again but now look at the folder class. You can use the same methode .createFolder() with this class. Result is following: 'Creates a folder in the current folder with the given name.' This methode returns again folder.

Solution

Google Drive:

  1. Create (or open) manually your desired folder in your Google Drive repository.
  2. Find the ID of your folder (when you open the folder, you can find the id at the end of the URL: drive.google.com/drive/folders/{this is your ID})

Script:

  1. Get your desired destination folder var parentFolder = DriveApp.getFolderById("{put your folder id here}");
  2. Add your code
  3. When you are creating new folder within parent folder, just call the .createFolder() methode from your parentFolder which you have created earlier: var folder = parentFolder.createFolder();

Edited script:

    function uploadFiles(form) {
          try {
            var dropbox = form.FirstName + form.LastName;

            var parentFolder = DriveApp.getFolderById('parentfolderId'); //add this line... 

            var folder, folders = DriveApp.getFoldersByName(dropbox);

            if (folders.hasNext()) {
              folder = folders.next();
            } else {
              folder = parentFolder.createFolder(dropbox); //edit this line
            }

            var blob = form.myFile;
            var file = folder.createFile(blob); 

            file.setDescription("Uploaded by " + form.Name);

            return "Thank you, your file was uploaded successfully!";
          } 

Upvotes: 3

Related Questions