如何取消对 google sheet 脚本中所有用户的所有保护?
how can remove all of the protection for all of user in google sheet script?
我有一个 Sheet,其中只有 A-C
列我想与用户 1 共享,并且只有 F-H
列我想与用户 2 共享。我使用 Apps Script 来完成这项工作。 user1 和 user2 都不是所有者。要执行某些代码,我必须删除所有用户的所有保护。我为此编写了下面的代码,但它只对所有者有效,对其他用户无效。
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();
}
}
恐怕没有直接的方法可以实现您的目标:仅向某些用户显示 Sheet 的某些部分。但是有希望完成类似的事情,因为有两个已知的解决方法:
- 一种方法是 publish a webapp 检查用户名(就像您在代码中所做的那样)并仅打印预期的列。
- 另一种方法是 share 不同的 Sheet 与不同的用户,只显示您想要显示给他们的数据。每个 Sheet 将自动与一个 master sheet 同步,将它们连接在一起;但只有管理员可以访问它。
如您所见,这是两个相近的解决方法。我希望他们为你工作。如有任何问题或疑问,请随时回信。
我有一个 Sheet,其中只有 A-C
列我想与用户 1 共享,并且只有 F-H
列我想与用户 2 共享。我使用 Apps Script 来完成这项工作。 user1 和 user2 都不是所有者。要执行某些代码,我必须删除所有用户的所有保护。我为此编写了下面的代码,但它只对所有者有效,对其他用户无效。
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();
}
}
恐怕没有直接的方法可以实现您的目标:仅向某些用户显示 Sheet 的某些部分。但是有希望完成类似的事情,因为有两个已知的解决方法:
- 一种方法是 publish a webapp 检查用户名(就像您在代码中所做的那样)并仅打印预期的列。
- 另一种方法是 share 不同的 Sheet 与不同的用户,只显示您想要显示给他们的数据。每个 Sheet 将自动与一个 master sheet 同步,将它们连接在一起;但只有管理员可以访问它。
如您所见,这是两个相近的解决方法。我希望他们为你工作。如有任何问题或疑问,请随时回信。