How to sort google drive files by size in a spreadsheet?

Viewed 160

I am using a script to recursively list all the files in a Google drive folder to a spreadsheet. It is working fine but i need to sort the file listing by size (highest size on top). Also drive api returns value of size in bytes but i need them in GB's. I haven't found any way to do it through api directly, so i want to divide the size value of each file by 1 073 741 824 up to 1 decimal rounding it off (1 GB = 1 073 741 824 bytes)

function start() {
    var sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type", "Folder", "Folder Slug"]);

    var folders = DriveApp.getFolderById('FOLDER_ID');
    var folder = folders.getFolders();
    if (folder.hasNext()) {
        processFolder(folder);
    } else {
        Browser.msgBox('Folder not found!');
    }

    function processFolder(folder) {
        while (folder.hasNext()) {
            var f = folder.next();
            var contents = f.getFiles();
            addFilesToSheet(contents, f);
            var subFolder = f.getFolders();
            processFolder(subFolder);
        }
    }

    function addFilesToSheet(files, folder) {
        var data;
        var folderName = folder.getName();
        while (files.hasNext()) {
            var file = files.next();
            Logger.log(file.getName());

            sheet.appendRow([
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      file.getMimeType(),
      folderName
    ]);
        }
    }

    
}
1 Answers

Modification points:

  • In your script, the values are put to the Spreadsheet using appendRow in the loops. In this case, the process cost will be high. Ref And also, in this case, after the values were put to the Spreadsheet, it is required to sort the sheet.
  • So, in this answer, I would like to propose the following flow.
    1. Retrieve the file list and put to an array.
    2. Sort the array by the file size.
    3. Put the array to the Spreadsheet.

When above points are reflected to your script, it becomes as follows.

Modified script:

function start() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type", "Folder", "Folder Slug"]);
  var folders = DriveApp.getFolderById('FOLDER_ID');
  var folder = folders.getFolders();
  if (folder.hasNext()) {

    // 1. Retrieve the file list and put to an array.
    // 2. Sort the array by the file size.
    var list = processFolder(folder).sort((a, b) => a[2] < b[2] ? 1 : -1);

    // 3. Put the array to the Spreadsheet.
    sheet.getRange(2, 1, list.length, list[0].length).setValues(list);
  } else {
    Browser.msgBox('Folder not found!');
  }

  function processFolder(folder, list = []) {
    while (folder.hasNext()) {
      var f = folder.next();
      var contents = f.getFiles();
      addFilesToSheet(contents, f, list);
      var subFolder = f.getFolders();
      processFolder(subFolder, list);
    }
    return list;
  }

  function addFilesToSheet(files, folder, list) {
    var folderName = folder.getName();
    while (files.hasNext()) {
      var file = files.next();
      list.push([
        file.getName(),
        file.getDateCreated(),
        Math.round(10 * file.getSize() / 1073741824) / 10, // Modified from file.getSize() / 1073741824,
        file.getUrl(),
        "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
        file.getDescription() || "",
        file.getMimeType(),
        folderName
      ]);
    }
  }
}
  • In this modified script, the maximum file size is top of sheet.

References:

Related