当有人通过复制我的模板创建新的电子表格时,我想实现一个脚本来在电子表格的不同标签上实现范围保护。
我遇到的问题是脚本运行保护实现。
我的代码看起来很好,因为调试工具没有发现任何错误,但是,我的脚本只实现了第一个受保护的范围。
这里是样本:
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);
}
}
...
有人可以帮助我,因为我不知道这里有什么问题。
由于
答案 0 :(得分:0)
回答我的问题,
对于使用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);
...
}