Google Sheets Get Editors of Cell Without Having to First Protect Cell

Viewed 35

Is there a way to get the current editors of a cell without having to first set permissions? The code I have below resets the permissions to the sheet default and then grabs that as the current editors as opposed to using the editors that were there before overwriting that cell with protect().

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var name = sheet.getRange("A4:A52").getValues();
  var setPermissions = [];  

  //Employee preferred initials in order to match last name array
  var initialsToMatchLastName = ['CC','RR'];
  //Employee last names
  var lastNames = ['Charles','Rickey'];
  var emails = ['charles@email.com','rickey@email.com'];

  name.forEach((value,v) => {
  for (var i = 0; i < lastNames.length; i++) {
    if (value[0] == lastNames[i]) {
      var protection = sheet.getRange("R" + (v + 4)).protect().setDescription('Locked_' + lastNames[i] + '_' + v);
      var users = protection.getEditors();
      //Logger.log(users);
      //Logger.log(emails[i]);
      if (users.includes(emails[i])) {
        //Do nothing.
      } else {
          protection.removeEditors(users);
          protection.addEditor(emails[i]);
      }
    }
  }
  })
}
0 Answers
Related