1

I want to write some metadata from Drive files in a spreadsheet. I get the owner, name, Id, url, etc., but I'm having problems with the File#getEditors() and File#getViewers() methods.

I get this value written in the cells where the editors and viewers results are written:

[Ljava.lang.Object@....

enter image description here Anyone knows what is happening? I'm the owner of the files.

My script:

function getChildFolders(parentName, parent, data, sheet, listAll) {
  sheet.appendRow(["Full Path", "Name", "Type", "URL", "Access", "Permission", "Editors can share", "Owner", "Can edit", "Can comment", "Can view"]);

  var childFolders = parent.getFolders();
  var childFiles = parent.getFiles();

  while (childFiles.hasNext()) {
    var fileItem = childFiles.next();

    data = [
      parentName + "/" + fileItem.getName() + "/" + fileItem.getName(),
      fileItem.getName(),
      fileItem.getMimeType(),
      fileItem.getUrl(),
      fileItem.getAccess(Session.getActiveUser()),
      fileItem.getSharingPermission(),
      fileItem.isShareableByEditors(),
      fileItem.getOwner().getEmail(),
      fileItem.getEditors(),
      fileItem.getViewers()
    ];

    // Write      
    sheet.appendRow(data);
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
jbra95
  • 881
  • 2
  • 11
  • 30

1 Answers1

2

[Ljava.lang.Object means an object. fileItem.getEditors() and fileItem.getViewers() are objects. Each object returns User[]. You can retrieve the following values from it.

  • getDomain(): Gets the domain name associated with the user's account.
  • getEmail(): Gets the user's email address.
  • getName(): Gets the user's name.
  • getPhotoUrl(): Gets the URL for the user's photo.

So for example, if emails and names of editors and viewers are retrieved, how about this modification?

Modified script :

Please modify as follows.

From :
fileItem.getEditors(),
fileItem.getViewers()
To :
fileItem.getEditors().map(function(e){return [e.getEmail(), e.getName()]}).join(","),
fileItem.getViewers().map(function(e){return [e.getEmail(), e.getName()]}).join(","),

Note :

  • This is a sample. So please modify this for your situation.

References :

If this was not what you want, please tell me. I would like to modify it.

Added :

This is a sample modified script using setValues() instead of appendRow(). The cost of setValues() is lower than that of appendRow(). So also how about this modification?

sheet.appendRow(["Full Path", "Name", "Type", "URL", "Access", "Permission", "Editors can share", "Owner", "Can edit", "Can comment", "Can view"]);
var childFolders = parent.getFolders();
var childFiles = parent.getFiles();
var allValues = []; // Added
while (childFiles.hasNext()){
  var fileItem = childFiles.next();
  data = [
    parentName + "/" + fileItem.getName() + "/" + fileItem.getName(),
    fileItem.getName(),
    fileItem.getMimeType(),
    fileItem.getUrl(),
    fileItem.getAccess(Session.getActiveUser()),
    fileItem.getSharingPermission(),
    fileItem.isShareableByEditors(),
    fileItem.getOwner().getEmail(),
    fileItem.getEditors().map(function(e){return [e.getEmail(), e.getName()]}).join(","),
    fileItem.getViewers().map(function(e){return [e.getEmail(), e.getName()]}).join(","),
  ];
  allValues.push(data); // Added
}
sheet.getRange(sheet.getLastRow() + 1, 1, allValues.length, allValues[0].length).setValues(allValues); // Added

Reference :

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you so much! I 've done a 5-line script to solve it 3 mins ago, but this is much better. It works perfectly! – jbra95 Aug 14 '18 at 08:36
  • @Juan Bravo Roig Thank you for replying. I'm glad your issue was solved. As an additional sample, I added a sample modified script. How about that modification? The cost of ``setValues()`` is lower than that of ``appendRow()``. I'm not sure whether this additional modification is useful for you. So if this is not useful for your situation, please ignore it. – Tanaike Aug 14 '18 at 08:44