0
votes

I want to copy folders into a Master Folder within Google Drive. My main issue is creating a function when one column is not blank and another column is blank. Once, a new folder is created it has to paste in the folder URL in one of the columns.

This is what I have so far:

function addData(){
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var lr = activeSheet.getLastRow();
  var lc = activeSheet.getLastColumn();
  var sourceData = sourceSheet.getRange(1, 1, lr, lc).getValues();
  for (row in sourceData) {
    if (sourceData[row][0] !== "" && sourceData[row][19] !== "") {
      var rowNum = parseInt(row);

     //code will go here for Folder copying and renaming and getting the new folder url

     activeSheet.getRange(rowNum+1,lc).setValue("Completed"); //we will eventually change Completed with the new Folder URL
  }
 }
}
1
Unfortunately, I cannot understand about I'm stuck with my coding on automating folder creation in Google Drive. I want to copy one folder to the next all within Google Drive. My main issue is creating a function when column A is not blank and column S is blank. Once, the new folder is created it pastes in the folder URL in column S.. I apologize for this. Can I ask you about the detail of your current issue and your goal?Tanaike
@Tanaike, my current issue is copying folders in Google Drive and adding them to a master folder that I've set up.Coach212
Thank you for replying. I would like to confirm my understading of your actual question. In your current issue, you want to copy the specific folder which has subfolders and files to the other specific folder using Google Apps Script. In this case, you don't want to move them. You want to copy them. And, you have the permissions for retrieving the source folder. Is my understanding correct?Tanaike
@Tanaike, yes that is correct.Coach212
Thank you for replying. I could understand that my understanding of I want to copy folders into a Master Folder within Google Drive. is correct. I have one more question. I cannot understand the relationship between I want to copy folders into a Master Folder within Google Drive. and My main issue is creating a function when one column is not blank and another column is blank. Once, a new folder is created it has to paste in the folder URL in one of the columns.. I apologize for this. Can I ask you about the detail of your goal?Tanaike

1 Answers

3
votes

The main idea would be to iterate files using Folder.getFiles() and folders with Folder.getFolders(). Make new folders with Folder.createFolder(name) and copy files using File.makeCopy(name, destination):

/**
 * Copies all the files in a folder to another one.
 */
function copyFolderContents_(source, target) {
  // Iterate files in source folder
  const filesIterator = source.getFiles()
  while (filesIterator.hasNext()) {
    const file = filesIterator.next()

    // Make a copy of the file keeping the same name
    file.makeCopy(file.getName(), target)
  }
}

/**
 * Recursivelly copies a folder and all its subfolders with all the files
 */
function copyFolder_(toCopy, copyInto) {
  // Makes the new folder (with the same name) into the `copyInto`
  const newFolder = copyInto.createFolder(toCopy.getName())

  // Copy the contents
  copyFolderContents_(toCopy, newFolder)

  // Iterate any subfolder
  const foldersIterator = toCopy.getFolders()
  while (foldersIterator.hasNext()) {
    const folder = foldersIterator.next()

    // Copy the folder and it's contents (recursive call)
    copyFolder_(folder, newFolder)
  }
}

/**
 * Entry point to execute with the Google Apps Script UI
 */
function copyFolder() {
  // Get the folders (by ID in this case)
  const toCopy = DriveApp.getFolderById('')
  const copyInto = DriveApp.getFolderById('')

  // Call the function that copies the folder
  copyFolder_(toCopy, copyInto)
}

You need to add the ids of the folder to be copied and the folder to copy it from on copyFolder().

foldersIterator and filesIterator are Google Apps Script Iterators. They have a method hasNext() that returns if there is another item, and a next() that retrieves it.

Note that copyFolder_(toCopy, copyInto) uses itself to copy its subfolders.

References