0

The function I'm struggling with is part of a script that creates folders and copies a certain spreadsheet into each folder 15 times and names them.

I have a loop to create the files and inside of it I have an if..then..else statement to use a certain name for the file if j>10 and another name (else) if it's not. It always names the file one name and doesn't seem to recognize the if/else statement to change the name if j is over 10.

Basically what I want is to name the file

var namedFileCopy = fileCopy.setName(newFileName);

if j is less than 10 and...

var _namedFileCopy = fileCopy.setName("_" + newFileName); 

if j isn't less than 10.

I'm wondering if I need to add a break in there somewhere, but I'm not really familiar with how to use breaks.

This is the function that is giving me a problem, and you'll find all my code in the snippet following it.

function putFilesIntoFolders(arrayOfFolderNames,theFolderNames,cell) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var theSheet = ss.getSheetByName('Sheet1');
  var folderType = theSheet.getRange(2,1);
  var cell = folderType.getValue();
  var file = DriveApp.getFileById("ID");
  var dest_folder  = "";

  var baseFileName = "",
      newfile,
      newFileName = "",
      i=0,
      j=0;

  for (i=0;i<arrayOfFolderNames.length;i+=1) {
    var source_folder = DriveApp.getFolderById("ID");
    dest_folder  = DriveApp.getFolderById(folderIds[i]);
    Logger.log('dest_folder' + dest_folder);

    baseFileName = arrayOfFolderNames[i];

    for (j=1; j<16; j+=1) {
      var newFileName = baseFileName + " " + cell + " " + j.toString();
      var fileCopy = file.makeCopy();
      var namedFileCopy = fileCopy.setName(newFileName);
      var _namedFileCopy = fileCopy.setName("_" + newFileName);

      if (j<10) {
          dest_folder.addFile(namedFileCopy);
          source_folder.removeFile(fileCopy); 
      }  else {
          dest_folder.addFile(_namedFileCopy);
          source_folder.removeFile(fileCopy);
      };
    };
  };
};

  var folderIds = [];

function onOpen(e) {
   SpreadsheetApp.getUi()
    .createMenu('Data System Tool')
       .addItem('Create Data System Folders', 'copyAndRenameTemplate')
       .addToUi();
}

function copyAndRenameTemplate() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var theSheet = ss.getSheetByName('Sheet1');
  var rangeOfFileNames = ss.getRange("B4:B");
  var twoD_Array = rangeOfFileNames.getValues();
  var arrayOfFileNames = twoD_Array.join().split(",");
  var folderType = theSheet.getRange(2,1);
  var cell = folderType.getValue();

  Logger.log(folderType);
  Logger.log(cell);
  
//  throw new Error('This is not an error. This is just to abort javascript');
  
  var fldrNamesRng  = theSheet.getRange(4,1,theSheet.getLastRow()-3,1);
  Logger.log('fldrNamesRng: ' + fldrNamesRng);
  
  var folderNames  = fldrNamesRng.getValues();
  Logger.log('folderNames: ' + folderNames);
  
  var oneD_FolderNames = folderNames.join().split(",");
  Logger.log('oneD_FolderNames: ' + oneD_FolderNames);
  
  makeTheFolders(oneD_FolderNames);
  putFilesIntoFolders(oneD_FolderNames);
};

function makeTheFolders(theFolderNames,cell) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var theSheet = ss.getSheetByName('Sheet1');
  var folderType = theSheet.getRange(2,1);
  var cell = folderType.getValue();
  
  var i=0,
      folderObj;
  
  for (i=0;i<theFolderNames.length;i+=1) {
    folderObj = DriveApp.createFolder(theFolderNames[i] + " " + cell);
    
    folderIds.push(folderObj.getId())
  };
};

function putFilesIntoFolders(arrayOfFolderNames,theFolderNames,cell) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var theSheet = ss.getSheetByName('Sheet1');
  var folderType = theSheet.getRange(2,1);
  var cell = folderType.getValue();
  var file = DriveApp.getFileById("ID");
  var dest_folder  = "";
  
  var baseFileName = "",
      newfile,
      newFileName = "",
      i=0,
      j=0;
  
  for (i=0;i<arrayOfFolderNames.length;i+=1) {
    var source_folder = DriveApp.getFolderById("ID");
    dest_folder  = DriveApp.getFolderById(folderIds[i]);
    Logger.log('dest_folder' + dest_folder);
    
    baseFileName = arrayOfFolderNames[i];
    
    for (j=1; j<16; j+=1) {
      var newFileName = baseFileName + " " + cell + " " + j.toString();
      var fileCopy = file.makeCopy();
      var namedFileCopy = fileCopy.setName(newFileName);
      var _namedFileCopy = fileCopy.setName("_" + newFileName);
     
      if (j<10) {
          dest_folder.addFile(namedFileCopy);
          source_folder.removeFile(fileCopy); 
      }  else {
          dest_folder.addFile(_namedFileCopy);
          source_folder.removeFile(fileCopy);
      };
    };
  };
};

1
  • Close your for loop with }
    – www139
    Oct 4, 2015 at 18:17

2 Answers 2

0

There's no issue about your loops, no need to break; out of them. Your problem occurs in these lines of code:

  var fileCopy = file.makeCopy();
  var namedFileCopy = fileCopy.setName(newFileName);
  var _namedFileCopy = fileCopy.setName("_" + newFileName);

The makeCopy() method will create a new file object, representing a new file in your Google Drive.

After that, you call the setName() method on the fileCopy object, which changes the name of the associated file. That's fine, except that you've assigned the return value of the method call to a new variable, namedFileCopy. You've now got two variables that reference the same object, because this method simply returns the original object for chaining. (Chaining is when we call multiple methods on on object in the same statement; it's only possible if each method returns the original object reference for the next method.)

Next, you call setName() on fileCopy again, assigning the return value to _namedFileCopy; that's the third reference to the exact same object. Oh, and that file - the file that all three object references point to - is now named with an underscore.

After this, your if..then..else is doomed to name the resulting file with an underscore. Actually... the file is already named with an underscore, from the third operation above.

Separate concerns some more; you've got to make a decision about a file name, so do that in isolation. Once you've completed that concern, you are ready to manipulate the file, which is a separate concern.

The resulting loop should look something like this:

  for (i=0; i<arrayOfFolderNames.length; i++) {
    var source_folder = DriveApp.getFolderById("ID");
    dest_folder  = DriveApp.getFolderById(folderIds[i]);
    Logger.log('dest_folder' + dest_folder);

    baseFileName = arrayOfFolderNames[i];

    for (j=1; j<16; j++) {
      // Determine the name for the new file
      var newFileName =  baseFileName + " " + cell + " " + j.toString();
      // Prepend file name with underscore if j >= 10
      if (j >= 10) newFileName = "_" + newFileName;

      // Copy "file" and apply our newFileName, then move to new destination.
      var fileCopy = file.makeCopy()
                         .setName(newFileName);

      dest_folder.addFile(fileCopy);
      source_folder.removeFile(fileCopy); 
    }
  }

(I suspect this is what being said in Alvaz' answer.)

0
0

fileCopy.setName() changes the filename property of the file object. You should move your calls to fileCopy.setName() inside of your conditional branches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.