Search code examples
google-apps-scriptgoogle-sheetsspreadsheetprivileges

how can remove all of the protection for all of user in google sheet script?


I have a Sheet which only columns A-C I want to share with user1, and only columns F-H to user2. I do this work with Apps Script. Neither user1 nor user2 are the owner. To execute some code I have to delete all the protection for all users. I wrote the code below for this, but it only works for the owner and not for other users.

function onOpen (e){
var s1 = SpreadsheetApp.getActive();
var range1 = s1.getRange('A:A');
var range2 = s1.getRange('B:J');
var range3 = s1.getRange('K:Z');
var range4 = s1.getRange('AA:AC');
var range5 = s1.getRange('AD:AE');
var range6 = s1.getRange('A1:AE1');  
var protection1 = range1.protect().setDescription('1');
var protection2 = range2.protect().setDescription('2');
var protection3 = range3.protect().setDescription('3');
var protection4 = range4.protect().setDescription('4');
var protection5 = range5.protect().setDescription('5');
var protection6 = range6.protect().setDescription('6');   
protection1.removeEditors(protection1.getEditors());
protection2.removeEditors(protection2.getEditors());
protection3.removeEditors(protection3.getEditors());
protection4.removeEditors(protection4.getEditors());
protection5.removeEditors(protection5.getEditors());
protection6.removeEditors(protection6.getEditors());  
protection1.addEditor('rafi@gmail.com'); 
protection1.addEditor('naseri@gmail.com');   
protection2.addEditor('sabeti@gmail.com');
protection2.addEditor('chavoshi@gmail.com');
protection2.addEditor('naseri@gmail.com');
protection3.addEditor('naseri@gmail.com');
protection3.addEditor('rezvani@gmail.com');
protection3.addEditor('nurian@gmail.com');
protection3.addEditor('fazeli@gmail.com');
protection4.addEditor('naseri@gmail.com');
protection5.addEditor('naseri@gmail.com');
protection5.addEditor('nurian@gmail.com');
protection5.addEditor('fazeli@gmail.com');
protection5.addEditor('rezvani@gmail.com');  
protection6.addEditor('chavoshi@gmail.com');
protection6.addEditor('naseri@gmail.com');  
if (protection1.canDomainEdit()) {
protection1.setDomainEdit(false);
}
if (protection2.canDomainEdit()) {
protection2.setDomainEdit(false);
}
if (protection3.canDomainEdit()) {
protection3.setDomainEdit(false);
}
if (protection4.canDomainEdit()) {
protection4.setDomainEdit(false);
}
if (protection5.canDomainEdit()) {
protection5.setDomainEdit(false);
}
if (protection6.canDomainEdit()) {
protection6.setDomainEdit(false);
}
}  



}
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange(); 

if(s.getName() == "Order List" && r.getColumn() == 30 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Arrived Complete");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}

var s10 = SpreadsheetApp.getActive(); 
var protections = s10.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if (protection.canEdit()) {  
protection.remove();
}  
} 

onOpen (e);  
}


function clearFormat() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var ss = s.getActiveSheet();
var sheetname = ss.getName();
var s2c = SpreadsheetApp.getActiveSpreadsheet();
var ss2cc = s2c.getActiveSheet();
var sheetname = ss2cc.getName();
if (sheetname == "Arrived Complete") {
ss.clearConditionalFormatRules();
}
if (sheetname == "Arrived Canceled") {
ss2cc.clearConditionalFormatRules();
}
}

Solution

  • I am afraid that there is no direct way of doing your goal: to show certain parts of a Sheet only to some users. But there is hope to accomplish something similar, because there are two known workarounds for that:

    1. One way is to publish a webapp that checks the username (like you did in your code) and prints only the intended column.
    2. Another way is to share different Sheets with different users, with only the data that you want to show them. Each Sheet will be automatically synchronized with a master sheet that joins all of them together; but only the administrator will access it.

    As you can see, these are two close workarounds. I hope that they work for you. Don't hesitate to write back with any question or doubt.