Home > OS >  Copy a Google Folder
Copy a Google Folder

Time:10-23

I am trying to alter some code I found on this website to be a bit more custom to my project and a bit more user-friendly for my co-workers.

I am trying to pull in the folder ID's for the source and target folders from cells in the spreadsheet. I'm positive I did the code correctly for this part but I get an ID exception error when trying to run the code. When I directly place the ID's into the code, using quotations to make them strings, where I currently have resSource and resTarget, the code works perfectly.

I can't figure out how to use a variable to pass the folder ID's into the function correctly...can anyone help?

function onOpen(){
 createMenu()
}
function createMenu (){
 const ui = SpreadsheetApp.getUi() 
 const menu = ui.createMenu("Copy Folders")
menu.addItem("Copy Folder", "copyFolder")
menu.addToUi()
}

/**
 * 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(resSource)
  const copyInto = DriveApp.getFolderById(resTarget)

  var app = SpreadsheetApp;
   var ss = app.getActiveSpreadsheet();
   var activeSheet = ss.getActiveSheet();
   var sourceText = activeSheet.getRange("A2").getValue();
  var resSource = sourceText.toString;
   console.log(resSource);
   var targetText = activeSheet.getRange("B2").getValue();
   var resTarget = targetText.toString;
   console.log(resTarget);

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

CodePudding user response:

function copyFolderContents_(source, target) {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("SheetName");
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, 2).getValues();
  vs.forEach(r => {
    let source = DriveApp.getFolderById(r[0]);
    let target = DriveApp.getFolderById(r[1]);
    const files = source.getFiles()
    while (files.hasNext()) {
      const file = files.next()
      file.makeCopy(file.getName(), target)
    }
  });
}
A B
Source Id's Target Id's

CodePudding user response:

On the copyFolder function the code is passing resSource and resTarget as parameters on a couple of statements but these variables are declared after them.

Try moving

// Get the folders (by ID in this case)
const toCopy = DriveApp.getFolderById(resSource)
const copyInto = DriveApp.getFolderById(resTarget)

just before

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