En Google Drive, estoy tratando de comparar una identificación en un nombre de archivo con las carpetas nombradas con esa identificación y, si coinciden, mover ese archivo a esa carpeta.
Algunos de los problemas que tiene que dar cuenta el programa son:
Cosas que hacen esto más fácil:
Hasta ahora he escrito un código que con éxito:
He creado dos archivos en la carpeta "archivos" para probar, "john1111_test.pdf" y "thomas2222_test.pdf", y dos carpetas en la carpeta "childName" "john1111" y "thomas2222"
La parte en la que estoy atascado es copiar el archivo a la carpeta con la que coincidió.
var childFolder = DriveApp.getFolderById('****').getFolders(); // Folder containing folders var files = DriveApp.getFolderById('****').getFiles(); // Folder containing the files var fileNames = []; while (files.hasNext()) { var file1 = files.next(); var fName = file1.getName() Logger.log(fName); fileNames.push(fName); } var childNames = []; while (childFolder.hasNext()) { var child = childFolder.next(); var cdName = child.getName(); Logger.log(cdName); childNames.push(cdName); const match = fileNames.find(element => { if (element.includes(cdName)) { // This is the area that I am having issues with let folderMatch = childFolder.getFoldersByName(cdName); let fileMatch = files.getFilesByName(element); folderMatch.addFile(fileMatch) return true; } }); console.log(match); }Recibo un error que dice que foldermatch.addFile no es una función. También probé cdName.addFile (elemento).
function movingFile() { const files = DriveApp.getFolderById('1jeAihkCiA--EfAl150IXrXDUa-MhYSKY'); const folders = DriveApp.getFolderById('1xB4XTG8d1DYtthVQQqkSQCqvzv-TGWtW'); const fldrs = folders.getFolders(); let fldrNames = []; let fldrA = []; while (fldrs.hasNext()) { let fldr = fldrs.next() fldrNames.push(fldr.getName()); fldrA.push(fldr); } const fs = files.getFiles(); while (fs.hasNext()) { let file = fs.next(); let idx = fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_'))); if(~fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_')))) {//if folder is present move it here //SpreadsheetApp.getUi().alert(file.getName().slice(0,file.getName().indexOf('_'))); Drive.Files.update({"parents": [{"id": fldrA[idx].getId()}]}, file.getId()); } else {//if it is not then create it first and xfer let f = folders.createFolder(file.getName().slice(0,file.getName().indexOf('_'))); fldrA.push(f); Drive.Files.update({"parents": [{"id": f.getId()}]}, file.getId()); fldrNames.push(file.getName().slice(0,file.getName().indexOf('_')));//added folder name to fldrNames so that it will be used again if there are more files with that name } } }Antes:
Después:
Necesita habilitar Drive API versión 2