如何在多个选项卡上实施 rangeProtect() - google 电子表格
How to implement rangeProtect() over multiple tabs - google spreadsheet
当有人通过复制我的模板创建新电子表格时,我想实现一个脚本来在电子表格的不同选项卡上实现范围保护。
我遇到的问题是脚本运行通过保护执行时。
我的代码似乎没有问题,因为调试工具没有发现任何错误,但是,我的脚本只实现了第一个受保护的范围。
这里是示例:
function rangeProtect() {
var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if(s.getName()=='Overview') {
var range = s.getRange('A1:K20');
var protection = range.protect().setDescription('Overview');
var me = Session.getEffectiveUser();
var eds = protection.getEditors();
protection.addEditor(me);
protection.removeEditors(eds);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
else if(s.getName()=='Picklist (Buying Ops)') {
var range = s.getRange('A1:H254');
var protection = range.protect().setDescription('Picklist');
var range2 = s.getRange('C5:H254')
var protection2 = range2.protect().setDescription('Buying Ops');
var me = Session.getEffectiveUser();
var eds = protection.getEditors();
var add = raw[11];
protection.addEditor(me);
protection.removeEditors(eds);
protection2.addEditor(add);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
...
谁能帮我解决这个问题,因为我看不出这里有什么问题。
谢谢
回答我的问题,
主脚本描述了找到 sheet 的方法和保护/解除保护的范围,针对不同的用户使用 remove/addEditors(mail) 函数
//main script - where to protect - who can edit
function Protect_UnProtect( myrange, mysheet, mydescription, startRow, colnumber, numRows, columncount) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mysheet);
var range1 = ss.getRange(myrange);
var protection1 = range1.protect().setDescription(mydescription);
var me = Session.getEffectiveUser();
var eds1 = protection1.getEditors();
protection1.addEditor(me);
protection1.removeEditors(eds1);
//Incase we dont want to run this part of the code, then send the StartRow parameter with -1 value
// and the rest of the parameteres with 0
//startRow = first raw where to fetch datas
//colnumber = column number where to fetch datas - column A = 1
//numRows = number of rows where to fetch datas
//columncount = number of columns where to fetch datas
if(startRow > 0) {
var dataRange = ss.getRange(startRow, colnumber, numRows, columncount)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0];
protection1.addEditor(emailAddress);
}
}
if (protection1.canDomainEdit()) {
protection1.setDomainEdit(false);
}
}
第二个脚本正在调用主脚本并指出应用它的位置。
function rangeProtect() { //script to recall the main script
Protect_UnProtect("A1:H", 'Overview', '0-Overview', -1, 0, 0, 0);
Protect_UnProtect("A1:B", 'Picklist (Buying Ops)', '1- Picklist 1', -1, 0, 0, 0);
Protect_UnProtect("C1:H2", 'Picklist (Buying Ops)', '1- Picklist 2', 5, 11, 2, 1);
Protect_UnProtect("C3:H4", 'Picklist (Buying Ops)', '1- Picklist 3', -1, 0, 0, 0);
Protect_UnProtect("I1:K", 'Picklist (Buying Ops)', '1- Picklist 4', -1, 0, 0, 0);
Protect_UnProtect("C5:H", 'Picklist (Buying Ops)', '1- Buying Ops', 5, 11, 2, 1);
Protect_UnProtect("A1:O", 'Dashboard (View Only)', '2- Dashboard', -1, 0, 0, 0);
...
}
当有人通过复制我的模板创建新电子表格时,我想实现一个脚本来在电子表格的不同选项卡上实现范围保护。
我遇到的问题是脚本运行通过保护执行时。
我的代码似乎没有问题,因为调试工具没有发现任何错误,但是,我的脚本只实现了第一个受保护的范围。
这里是示例:
function rangeProtect() {
var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if(s.getName()=='Overview') {
var range = s.getRange('A1:K20');
var protection = range.protect().setDescription('Overview');
var me = Session.getEffectiveUser();
var eds = protection.getEditors();
protection.addEditor(me);
protection.removeEditors(eds);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
else if(s.getName()=='Picklist (Buying Ops)') {
var range = s.getRange('A1:H254');
var protection = range.protect().setDescription('Picklist');
var range2 = s.getRange('C5:H254')
var protection2 = range2.protect().setDescription('Buying Ops');
var me = Session.getEffectiveUser();
var eds = protection.getEditors();
var add = raw[11];
protection.addEditor(me);
protection.removeEditors(eds);
protection2.addEditor(add);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
...
谁能帮我解决这个问题,因为我看不出这里有什么问题。
谢谢
回答我的问题,
主脚本描述了找到 sheet 的方法和保护/解除保护的范围,针对不同的用户使用 remove/addEditors(mail) 函数
//main script - where to protect - who can edit
function Protect_UnProtect( myrange, mysheet, mydescription, startRow, colnumber, numRows, columncount) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mysheet);
var range1 = ss.getRange(myrange);
var protection1 = range1.protect().setDescription(mydescription);
var me = Session.getEffectiveUser();
var eds1 = protection1.getEditors();
protection1.addEditor(me);
protection1.removeEditors(eds1);
//Incase we dont want to run this part of the code, then send the StartRow parameter with -1 value
// and the rest of the parameteres with 0
//startRow = first raw where to fetch datas
//colnumber = column number where to fetch datas - column A = 1
//numRows = number of rows where to fetch datas
//columncount = number of columns where to fetch datas
if(startRow > 0) {
var dataRange = ss.getRange(startRow, colnumber, numRows, columncount)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0];
protection1.addEditor(emailAddress);
}
}
if (protection1.canDomainEdit()) {
protection1.setDomainEdit(false);
}
}
第二个脚本正在调用主脚本并指出应用它的位置。
function rangeProtect() { //script to recall the main script
Protect_UnProtect("A1:H", 'Overview', '0-Overview', -1, 0, 0, 0);
Protect_UnProtect("A1:B", 'Picklist (Buying Ops)', '1- Picklist 1', -1, 0, 0, 0);
Protect_UnProtect("C1:H2", 'Picklist (Buying Ops)', '1- Picklist 2', 5, 11, 2, 1);
Protect_UnProtect("C3:H4", 'Picklist (Buying Ops)', '1- Picklist 3', -1, 0, 0, 0);
Protect_UnProtect("I1:K", 'Picklist (Buying Ops)', '1- Picklist 4', -1, 0, 0, 0);
Protect_UnProtect("C5:H", 'Picklist (Buying Ops)', '1- Buying Ops', 5, 11, 2, 1);
Protect_UnProtect("A1:O", 'Dashboard (View Only)', '2- Dashboard', -1, 0, 0, 0);
...
}