I have a Google Spreadsheet that share to my staffs to record every single order details. This is only example:
https://docs.google.com/spreadsheets/d/1r8_6S_jI-ZzL1GgZEur4ZVM51xqu3fWfnbFOHw3ZTZw/edit?usp=sharing
Every time the order is closed, I wish to protect whole order row to avoid mistaken edits. In the example file I have a script code that I copied from some other posts(I couldn't find it anymore) but the demand between me and the original poster is kinda different so I edited the range but I still can't make it perfect.
Here's what I need:
If G2 = 1, A2:F2 protect to only owner can edit. When G2 is anything else,clear the protected range A2:F2. I need it to do the same thing in every single row until 2000. (G3 = 1 then protect A3:F3) (G4 = 1 then protect A4:F4) Something like this until 2000 row.
I want the same code applying to all 4 tab (Sheet1 to Sheet4)
I found that with my current script, if G2 = 1, everytime I edit something it will add a new range to the same range (A2:F2).I remember the original post is onOpen but I have to make it onEdit to make sure everything is protected well.
Maybe this can help you. A few considerations first:
First of all, creating a Range is not a quick process, it requires from 0,5 to 1 second, so doing this for 2000 rows in 4 sheets (that's 8000 rows) it would take 1 or 2 hours, and that's way beyond the execution time limit even if you were an Enterprise G Suite user.
The main problem with ranges is that they are stored in SpreadsheetApp.ProtectionType.RANGE
, this creates an array of Ranges, so in order to delete an specific range when G is not 1, you have to compare the Row you edited with all the values of the Range array.
This script executes each time you edit a row, if it's protected (G = 1) it does nothing, if it's not and you change G = 1, then it protects the range A:F of that row. If you change the 1 in G, it deletes the protection. So this doesn't protect or unprotect each row of the 4 sheets, it only protects or unprotects the row you edit in any of the sheets.
function onEdit() {
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sprsheet.getActiveSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
var col_G = sheet.getRange("G2:G2000").getValues();
var edited_row = sheet.getActiveRange().getRow();
var protected = false;
var range_pos;
modifyProtection(edited_row, protections, col_G, sheet, protected);
}
function modifyProtection(edited_row, protections, col_G, sheet, protected, range_pos){
if (protections.length == 0 && col_G[edited_row - 2] == 1){ //In case there aren't ranges yet
createRange(edited_row, sheet);
} else {
for (var i = 0; i < protections.length; i++){
if (edited_row == protections[i].getRange().getRow()){
range_pos = i;
protected = true;
}
}
if (protected && col_G[edited_row - 2] != 1){
protected = false;
deleteRange(range_pos, protections);
} else {
if (!protected && col_G[edited_row - 2] == 1){
protected = true;
createRange(edited_row, sheet);
}
}
}
}
function createRange(edited_row, sheet, protected){
var range = sheet.getRange('A'+(edited_row)+':F'+(edited_row));
var protection = range.protect().setDescription('Sample protected range');
var me = 'your_email';//This will be the only editor of the protected ranges
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
function deleteRange(i, protections){
protections[i].remove();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.