Estoy tratando de modificar un código que encontré en este sitio web para que sea un poco más personalizado para mi proyecto y un poco más fácil de usar para mis compañeros de trabajo.
Estoy tratando de obtener los ID de carpeta para las carpetas de origen y de destino de las celdas de la hoja de cálculo. Estoy seguro de que hice el código correctamente para esta parte, pero recibo un error de excepción de ID cuando intento ejecutar el código. Cuando coloco directamente los ID en el código, usando comillas para convertirlos en cadenas, donde actualmente tengo resSource y resTarget, el código funciona perfectamente.
No puedo entender cómo usar una variable para pasar la ID de la carpeta a la función correctamente... ¿alguien puede ayudarme?
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) }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 |
|---|---|
| ID de fuente | ID de destino |
En la función copyFolder, el código pasa resSource y resTarget como parámetros en un par de declaraciones, pero estas variables se declaran después de ellas.
Intenta moverte
// Get the folders (by ID in this case) const toCopy = DriveApp.getFolderById(resSource) const copyInto = DriveApp.getFolderById(resTarget)justo antes
// Call the function that copies the folder copyFolder_(toCopy, copyInto)