1

I've found scripts that save a copy of a spread sheet, but so far I've not been able to figure out how to modify the following code to save my spreadsheet into the newly created/already existing folder.

//Create folder if it does not exist
function createFolder(folderID, folderName){
  var parentFolder = DriveApp.getFolderById(folderID);
  var subFolders = parentFolder.getFolders();
  var doesntExist = true;
  var newFolder = '';
  
  // Check if folder already exists.
  while(subFolders.hasNext()){
    var folder = subFolders.next();
    
    //If the folder exists return the id of the folder
    if(folder.getName() === folderName){
      doesntExist = false;
      newFolder = folder;
      return newFolder.getId();
    };
  };
  //If the folder doesn't exist, then create a new folder
  if(doesntExist == true){
    //If the file doesn't exist
    newFolder = parentFolder.createFolder(folderName);
    return newFolder.getId();
  };
};
 
function start(){
  var FOLDER_ID = 'my folder id';
  var NEW_FOLDER_NAME = SpreadsheetApp.getActiveSheet().getRange('A1').getValue();
  var myFolderID = createFolder(FOLDER_ID, NEW_FOLDER_NAME);
};

Update:

  • My goal is to create a new folder based on the contents of cell A1 if that folder doesn't exist and save the file in that folder. If the folder already exists, then save the file in the existing folder. The file name will be in cell A2.

  • I want to save a copy, and leave the original file where it is.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • In your script, it seems that when the folder name of the inputted `FOLDER_ID` is `NEW_FOLDER_NAME`, the folder ID is returned, and when the folder name of the inputted `FOLDER_ID` is not `NEW_FOLDER_NAME`, the folder ID of the created new folder with `NEW_FOLDER_NAME` is returned. So I cannot understand about `I've not been able to figure out how to modify the following code to save my spreadsheet into the newly created/already existing folder.`. Can you explain about the detail of your goal? – Tanaike Oct 08 '20 at 22:41
  • my goal is to create a new folder based on the contents of A1 if that folder doesn't exist and save the file in that folder. If the folder already exists, then save the file in the existing folder with the file name in A2. – Erik Stenerud Oct 09 '20 at 17:53
  • Thank you for replying. I noticed that 2 answers have already been posted. In this case, I would like to respect them. I think that those will resolve your issue. – Tanaike Oct 09 '20 at 23:07
  • 1
    Hi, I edited my code snippet in my answer based on the comments you provided below the answer provided by Yuri Khristich (you want to copy the file and specify the copied file name). I also provided an alternative code to the function `createFolder`, which would reduce it's size and complexity. I hope this is helpful to you. – Iamblichus Oct 10 '20 at 07:46

2 Answers2

3

Since September 30, 2020, Drive files cannot have multiple parent folders.

Therefore, you should either move your file to the new folder, or make a copy of the file. But you cannot have the same file in two different folders.

You should use either moveTo(destination), if you want to move the file to the new folder, or makeCopy(destination) if you want to copy your file to the new folder.

Update: You mentioned in comments that you want to make a copy the file (not move it) and specify the name of the copied file (corresponding to value in A2). Therefore, you should use makeCopy(name, destination).

Considering that you want to save the active spreadsheet, your main function could be like this:

function start(){
  var PARENT_FOLDER_ID = 'my folder id';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var NEW_FOLDER_NAME = sheet.getRange('A1').getValue();
  var FILE_NAME = sheet.getRange('A2').getValue();
  var spreadsheetId = ss.getId();
  var file = DriveApp.getFileById(spreadsheetId);
  var folder = createFolder(PARENT_FOLDER_ID, NEW_FOLDER_NAME);
  file.makeCopy(FILE_NAME, folder);
}

And the function createFolder could be greatly reduced in size, using Folder.getFoldersByName(name):

function createFolder(folderID, folderName){
  var parentFolder = DriveApp.getFolderById(folderID);
  var subFolders = parentFolder.getFoldersByName(folderName);
  if (subFolders.hasNext()) return subFolders.next(); // Return existing folder
  else return parentFolder.createFolder(folderName); // Return created folder
}

Notes:

  • Because of this behaviour change in Drive, methods like addFile(child) are deprecated.

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
0

The script in your example creates a new folder. It doesn't save anything, as far as I can tell.

If you want just save your current spreadsheet in some already existing folder, here you go:

var id     = SpreadsheetApp.getActiveSpreadsheet().getId();
var file   = DriveApp.getFileById(id);
var folder = DriveApp.getFolderById("FOLDER_ID");  // <-- ID of the destination folder

folder.addFile(file);

Update. In case you know the name of destination folder and the folder shares the same parent folder with current spreadsheet file the script can look like this:

var ss               = SpreadsheetApp.getActiveSheet();
var dest_folder_name = ss.getRange('A1').getValue();
var copy_name        = ss.getRange('A2').getValue();

var dest_folder      = "";
var file_id          = SpreadsheetApp.getActiveSpreadsheet().getId();
var file             = DriveApp.getFileById(file_id);
var parent_folder    = file.getParents().next();
var sub_folders      = parent_folder.getFolders();

// search a destination folder in current folder
while (sub_folders.hasNext()) {
    dest_folder = sub_folders.next();
    if (dest_folder.getName() === dest_folder_name) {
        break;
    }
    dest_folder = "";
}

// create the destination folder if nothing was found
if (dest_folder === "") {
    dest_folder = parent_folder.createFolder(dest_folder_name);
}

// copy the spreadsheet to the destination folder
file.makeCopy(copy_name, dest_folder);

The script tires to finds destination folder (the name taken from cell 'A1') inside current folder and creates the folder if nothing was found. After that it saves a copy of current spreadsheet (the name taken from cell 'A2').

NB. GoogleDrive allows files or subfolders with the same names inside one folder. So it's not unlikely to get many spreadsheets with identical names. Perhaps it makes sense to check names of existing files before the saving.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • I apologize, I didn't explain my goal very clearly at all. My goal is to create a new folder based on the contents of cell A1 if that folder doesn't exist and save the file in that folder. If the folder already exists, then save the file in the existing folder. The file name will be in cell A2. – Erik Stenerud Oct 09 '20 at 17:57
  • So you have almost all you need. Your script successfully creates folder (it takes name from 'A2' though). Probably in my sample you need to change the last line to `file.moveTo(folder)`. And to add somewhere something like `var name = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();` `SpreadsheetApp.getActiveSpreadsheet().rename(name)`. – Yuri Khristich Oct 09 '20 at 18:39
  • My folder name could be anything. I can see that the script gets the folder ID, but I don't know where of if it's being stored somewhere. I'm thinking I need something like file.moveTo(whatever the found/created folder is), but I want to save a copy, and leave the original file where it is. – Erik Stenerud Oct 09 '20 at 20:27
  • If you need to make a copy there is command `file.makeCopy(folder)`. But again you should have a folder ID of the destination folder. If you know the destination folder name and if you sure that the folder is sharing the same parent folder with your current spreadsheet file you can get folder ID from your script (`var folder_list = parentFolder.getFolders()` etc). It's a quite trivial task, I think I can do it, but I not sure if I understand your task correctly. – Yuri Khristich Oct 09 '20 at 22:45