2

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
    ]);
        }
    }

    
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Sachin
  • 1,217
  • 2
  • 11
  • 31

1 Answers1

1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks this is working well , can you please make the division round off to 1 digits – Sachin Jun 16 '21 at 02:47
  • @Sachin Thank you for replying. About your additional question of `can you please make the division round off to 1 digits`, unfortunately, I cannot understand it. I apologize for my poor English skill. So, can you provide the sample values? For example, when the size is `1.2345`, you want to retrieve `1`. Is my understanding correct? – Tanaike Jun 16 '21 at 02:49
  • Yes correct , For further example if size is 1.2345 , it should round off to 1.2. File size is 2.67897 , it should round off to 2.7 – Sachin Jun 16 '21 at 02:52
  • @Sachin Thank you for replying. From your replying, I updated my answer. Could you please confirm it? – Tanaike Jun 16 '21 at 02:55
  • 1
    @Sachin Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Jun 16 '21 at 03:06
  • Hello sorry for bothering you but there's a small issue. Google script only allows a maximum runtime of few minutes . Since i am running this script on thousands of files its unable to complete within that duration .. is there any way out ? – Sachin Jun 16 '21 at 04:55
  • @Sachin I would like to support you. About your new issue, I think that [this thread](https://stackoverflow.com/q/41741520) might be useful. But, in that case, I would like to recommend to post it as new question. Because I think that your this question is useful for other users, and your new question is also important for other users. So, can you post it as new question? By this, it will help users including me think of the solution. If you can cooperate to resolve your new issue, I'm glad. – Tanaike Jun 16 '21 at 05:41
  • 1
    Appreciate your help and suggestion . i have asked a new question regarding this since its not directly related to this one but all users are very likely to face it so linking it here is a good idea - https://stackoverflow.com/questions/67997214/exceeded-maximum-execution-time-error-in-google-apps-script – Sachin Jun 16 '21 at 06:08
  • 1
    @Sachin Thank you for replying. I would like to check it and think of the solution. – Tanaike Jun 16 '21 at 06:12