问题
I'm currently using Google Sheets as a way for people to sign up for certain time shifts. I'm using the following script to automatically protect cells after data entry so that people can't discretely erase their signups without notifying me:
function onEdit(e) {
var range = e.range;
var timeZone = Session.getScriptTimeZone();
var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
var description = 'Protected on ' + stringDate;
var protection = range.protect().setDescription(description);
// below code taken directly from Google's documentation (second comment is my own):
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
//user who installed trigger
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
The issue at hand is that I am working with many many shifts and people, which makes it very difficult and tedious to remove any protections in the event that someone needs to cancel for valid reasons (google sheets does not provide a way to search through your protected ranges). I understand the logic of writing scripts but am very inexperienced working with them. I could really use some help creating something that will remove all protections on any cell that I clear the data from.
回答1:
one way would be to open the protect range menu and click the corner. then delete them manually
script would be:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; // assuming you want the first sheet
var protections = sheet.getProtections();
for (var i = 0; i < protections.length; i++) {
if (protections[i].getDescription() == 'Protect column A') { //name of protected range
protections[i].remove();
}
}
and for mass unprotecting see: https://webapps.stackexchange.com/a/99304/186471
回答2:
You can get all the protections for a sheet with getProtections
[1] and search which protection from all the protections in the sheets is being cleared using getRange
method [2] on each Protect object to know the protected range and clear the protection in case the A1 notation is the same as the edited range (the range from the onEdit event), like this:
function onEdit(e) {
var range = e.range;
var newValue = range.getValue();
//If cell's new value is empty then remove protection
if(newValue == '') {
var sheet = range.getSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//Iterate through all sheet's protections
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if (protection.getRange().getA1Notation() == range.getA1Notation()) {
protection.remove();
}
}
}
else {
var timeZone = Session.getScriptTimeZone();
var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
var description = 'Protected on ' + stringDate;
var protection = range.protect().setDescription(description);
// below code taken directly from Google's documentation (second comment is my own):
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
//user who installed trigger
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
}
[1] https://developers.google.com/apps-script/reference/spreadsheet/sheet#getprotectionstype
[2] https://developers.google.com/apps-script/reference/spreadsheet/protection.html#getrange
[3] https://developers.google.com/apps-script/reference/spreadsheet/range#geta1notation
来源:https://stackoverflow.com/questions/60100715/how-do-i-automatically-remove-all-protections-that-exist-on-a-cell-in-google-she