How to get files from google drive by filtering based on ‘Unknown’ criteria with the apps script?

I want to retrieve a list of files from google drive using GAS. So I want the list of files based on the owner who is empty. For files that we create on the share drive, the owner doesn't exist. If we try to force the owner name, it will get an error.

For example like this:

file.getOwner().getName();

it cannot be retrieved from the file list. I have tried adding a filter based on an empty owner, I would like to say Unknown.

function getfilesUnknown () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var st = ss.getActiveSheet ();
  var files = DriveApp.getFiles ();
  var counter = 0;
  while (files.hasNext () && counter <21) {
    var file = files.next ();
    var data = [
      file.getName (),
      file.getUrl (),
      file.getOwner ()? file.getOwner (). getName (): 'Unknown'
    ];
    var filter = data.filter (v => v [2] === 'Unknown');
    counter ++;
    st.appendRow (filter);
  }
}

But the list of files won't retrieve.

It seems that the filter in while is not recommended. Or maybe, folks here have another solution. Thank you.



Read more here: https://stackoverflow.com/questions/64889568/how-to-get-files-from-google-drive-by-filtering-based-on-unknown-criteria-with

Content Attribution

This content was originally published by d'natsu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: