Advertisement
kuroshan1104

listar archivos pdf de carpetas drive

Oct 27th, 2022
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.24 KB | Source Code | 0 0
  1. function listFilesAndFolders(){
  2.   var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
  3.   if (folderId === "") {
  4.     Browser.msgBox('Folder ID is invalid');
  5.     return;
  6.   }
  7.   getFolderTree(folderId, true);
  8. };
  9. // Get Folder Tree
  10. function getFolderTree(folderId, listAll) {
  11.   try {
  12.     // Get folder by id
  13.     var parentFolder = DriveApp.getFolderById(folderId);
  14.    
  15.     // Initialise the sheet
  16.     var file, data, sheet = SpreadsheetApp.getActiveSheet();
  17.     sheet.clear();
  18.     sheet.appendRow(["Full Path", "Name","Type" ,"Date", "URL", "Last Updated", "Description", "Size","Owner Email"]);
  19.    
  20.     // Get files and folders
  21.     getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
  22.   } catch (e) {
  23.     Logger.log(e.toString());
  24.   }
  25. };
  26.  
  27. // Get the list of files and folders and their metadata in recursive mode
  28. function getChildFolders(parentName, parent, data, sheet, listAll) {
  29.   var childFolders = parent.getFolders();
  30.  
  31.   // List folders inside the folder
  32.   while (childFolders.hasNext()) {
  33.     var childFolder = childFolders.next();
  34.     var folderId = childFolder.getId();
  35.     data = [
  36.       parentName + "/" + childFolder.getName(),
  37.       childFolder.getName(),
  38.       "Folder",
  39.       childFolder.getDateCreated(),
  40.       childFolder.getUrl(),
  41.       childFolder.getLastUpdated(),
  42.       childFolder.getDescription(),
  43.       childFolder.getSize()/1024,
  44.       childFolder.getOwner().getEmail()
  45.     ];
  46.     // Write
  47.     //sheet.appendRow(data);
  48.    
  49.     // List files inside the folder
  50.     var files = childFolder.getFiles();
  51.     while (listAll & files.hasNext()) {
  52.       var childFile = files.next();
  53.       data = [
  54.         parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
  55.         childFile.getName().replace(".pdf",""),
  56.         "Files",
  57.         childFile.getDateCreated(),
  58.         childFile.getUrl(),
  59.         childFile.getLastUpdated(),
  60.         childFile.getDescription(),
  61.         childFile.getSize()/1024,
  62.         childFile.getOwner().getEmail(),
  63.       ];
  64.       // Write
  65.       sheet.appendRow(data);
  66.     }
  67.     // Recursive call of the subfolder
  68.     getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  69.   }
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement