mercredi 20 janvier 2016

Google Script if statement does not check user vs entered data

I have a booking program in Google Sheets where a user can pick their name (uses data verification to provide a list of emails) and then the cell is then placed under protection so no other user can change the booking.

The strange thing that happens is that a person can enter in another person's email and then the cell is protected by the entered email not the user. The user can enter in a non-email string and it does not protect the cell.

The desired result would be that if the user's email and the data entered is the same, protect the cells otherwise it is free to be edited.

Any help would be appreciated!

function onEdit(e){
  var CurrentUser = Session.getEffectiveUser().getEmail()
  var range_DataEntered = SpreadsheetApp.getActiveRange();
  var DataEntered = range_DataEntered.getValue();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SheetName = SpreadsheetApp.getActiveSheet();
  var Cell = ss.getActiveCell();

  if (CurrentUser = DataEntered) {
      var protection = range_DataEntered.protect()

      // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
      // permission comes from a group, the script will throw an exception upon removing the group.

      protection.addEditor(CurrentUser);
     if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
    }
  else {
  //remove protection, set everyone to edit
    range_DataEntered.protect().setDomainEdit(true);
  }
}

Aucun commentaire:

Enregistrer un commentaire