user2953386
user2953386

Reputation: 33

How can I analyze each value of the .getEditors() array

I'm developing a code here, and here is my problem.

I'm using the .getEditors() to define who's authorized to edit a document, and compare the data with the document owner, to send an e-mail to th owner, in case of an unauthorized edition of the file.

But i want to analyze each element from get.Editors() separated, and I don't know how to do it. I don't know.... maybe there's a way to separate the array... in case the document has more than one editor....

Here's what I have so far:

 var editorNome = DocumentApp.getActiveDocument().getEditors();
  Logger.log("Editor: "+editorNome)
 var fileOwner= DriveApp.getFileById(file).getOwner().getEmail();
  Logger.log("File owner: " +fileOwner)

if(fileOwner != editorNome){
    var yey = ('diferente');
    Logger.log('Resultado: ' +yey)
  }else{
    var nay = ('igual');
    Logger.log('Resultado: ' +nay)
  }

Can you help me please?

Upvotes: 1

Views: 550

Answers (1)

Serge insas
Serge insas

Reputation: 46812

The result being an array, you can use a for loop to iterate and log/compare each value.

Try like below :

function myFunction() {
  var editors = DocumentApp.getActiveDocument().getEditors();
  var fileOwner= DriveApp.getFileById('1PLW0T0erpFtiUQXTpUVBZla2r_NDn2C9MyvwS5PsihM').getOwner().getEmail();
  Logger.log("File owner: " +fileOwner)
  for(var n=0;n<editors.length;++n){ 
    Logger.log('testing '+editors[n]);
    if(fileOwner != editors[n]){
      Logger.log('Resultado: different')
    }else{
      Logger.log('Resultado: equal')
    }
  }
}

enter image description here

Upvotes: 1

Related Questions