如何保护 google sheet 中每个特定用户的范围?
How to protect ranges per specific users in google sheet?
我正在尝试使用 google 脚本对其工作簿中的每个 sheet 的所有 sheet 和某些范围应用保护(并且可以应用于新添加的 sheets 稍后通过触发器)
问题是,我想在保护整个 sheet 免受任何人攻击后,为不同的用户分配不同的范围,但人们允许编辑他们的范围!
我似乎不知道为什么它不起作用..它的工作方式相反,允许所有用户编辑所有范围但受保护的范围。
我希望第一个编辑能够编辑他的QC_Range,
和第二位能够编辑他的PLN_Range的编辑
并限制对所有其他单元格和整个 sheet.
的编辑访问(使其仅供查看)
function Sheet_Ranges_Protection(){
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
//var workbookB = SpreadsheetApp.getActiveSpreadsheet();
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++)
{
var Shprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.SHEET);
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
if (Shprotection == true)
SheetNumb++;
else if (Shprotection == false)
{
var Shprotection = Veranda_Sheets[SheetNumb].protect().setDescription('Sheet Protection');
var Rangesprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (Rangesprotection == false)
{
var QCprotection = QC_Range.protect().setDescription('QC Protection');
var me = Session.getEffectiveUser();
QCprotection.addEditor(me);
QCprotection.removeEditors(QCprotection.getEditors());
if (QCprotection.canDomainEdit())
{
QCprotection.setDomainEdit(false);
QCprotection.addEditors(['Editor1@gmail.com']);
}
var PLNprotection = PLN_Range.protect().setDescription('PLN Protection');
var me = Session.getEffectiveUser();
PLNprotection.addEditor(me);
PLNprotection.removeEditors(PLNprotection.getEditors());
if (PLNprotection.canDomainEdit())
{
PLNprotection.setDomainEdit(false);
PLNprotection.addEditors(['Editor2@gmail.com']);
}
}
else
SheetNumb++;
// Shprotection = true;
// Rangesprotection = true;
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
}
}
}
我重新编写了您的代码,我认为它可以实现您现在假装的功能。在这种情况下,我不进行 sheet 保护,而是单独保护所有范围:
function Sheet_Ranges_Protection() {
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++) {
var me = Session.getEffectiveUser();
// Define ranges that will be protected for everyone
var range1 = Veranda_Sheets[SheetNumb].getRange(6, 1, Veranda_Sheets[SheetNumb].getMaxRows(), Veranda_Sheets[SheetNumb].getMaxColumns());
var range2 = Veranda_Sheets[SheetNumb].getRange(1, 8, 5, Veranda_Sheets[SheetNumb].getMaxColumns());
var range3 = Veranda_Sheets[SheetNumb].getRange(1, 4, 5);
var ranges = [range1, range2, range3];
// Set protection for all the sheet minus QC/PLN ranges
for(var i = 0; i < ranges.length; i++) {
var rangeProtection = ranges[i].protect().setDescription('Range protection');
rangeProtection.addEditor(me);
rangeProtection.removeEditors(rangeProtection.getEditors());
if (rangeProtection.canDomainEdit()) {
rangeProtection.setDomainEdit(false);
}
}
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
// Set protection for QC range
var QC_protection = QC_Range.protect().setDescription('QC protection');
QC_protection.removeEditors(QC_protection.getEditors());
QC_protection.addEditor('Editor1@gmail.com');
if (QC_protection.canDomainEdit()) {
QC_protection.setDomainEdit(false);
}
// Set protection for PLN range
var PLN_protection = PLN_Range.protect().setDescription('PLN protection');
PLN_protection.removeEditors(PLN_protection.getEditors());
PLN_protection.addEditor('Editor2@gmail.com');
if (PLN_protection.canDomainEdit()) {
PLN_protection.setDomainEdit(false);
}
}
}
希望这对你有用!
我正在尝试使用 google 脚本对其工作簿中的每个 sheet 的所有 sheet 和某些范围应用保护(并且可以应用于新添加的 sheets 稍后通过触发器)
问题是,我想在保护整个 sheet 免受任何人攻击后,为不同的用户分配不同的范围,但人们允许编辑他们的范围!
我似乎不知道为什么它不起作用..它的工作方式相反,允许所有用户编辑所有范围但受保护的范围。
我希望第一个编辑能够编辑他的QC_Range, 和第二位能够编辑他的PLN_Range的编辑 并限制对所有其他单元格和整个 sheet.
的编辑访问(使其仅供查看)function Sheet_Ranges_Protection(){
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
//var workbookB = SpreadsheetApp.getActiveSpreadsheet();
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++)
{
var Shprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.SHEET);
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
if (Shprotection == true)
SheetNumb++;
else if (Shprotection == false)
{
var Shprotection = Veranda_Sheets[SheetNumb].protect().setDescription('Sheet Protection');
var Rangesprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (Rangesprotection == false)
{
var QCprotection = QC_Range.protect().setDescription('QC Protection');
var me = Session.getEffectiveUser();
QCprotection.addEditor(me);
QCprotection.removeEditors(QCprotection.getEditors());
if (QCprotection.canDomainEdit())
{
QCprotection.setDomainEdit(false);
QCprotection.addEditors(['Editor1@gmail.com']);
}
var PLNprotection = PLN_Range.protect().setDescription('PLN Protection');
var me = Session.getEffectiveUser();
PLNprotection.addEditor(me);
PLNprotection.removeEditors(PLNprotection.getEditors());
if (PLNprotection.canDomainEdit())
{
PLNprotection.setDomainEdit(false);
PLNprotection.addEditors(['Editor2@gmail.com']);
}
}
else
SheetNumb++;
// Shprotection = true;
// Rangesprotection = true;
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
}
}
}
我重新编写了您的代码,我认为它可以实现您现在假装的功能。在这种情况下,我不进行 sheet 保护,而是单独保护所有范围:
function Sheet_Ranges_Protection() {
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++) {
var me = Session.getEffectiveUser();
// Define ranges that will be protected for everyone
var range1 = Veranda_Sheets[SheetNumb].getRange(6, 1, Veranda_Sheets[SheetNumb].getMaxRows(), Veranda_Sheets[SheetNumb].getMaxColumns());
var range2 = Veranda_Sheets[SheetNumb].getRange(1, 8, 5, Veranda_Sheets[SheetNumb].getMaxColumns());
var range3 = Veranda_Sheets[SheetNumb].getRange(1, 4, 5);
var ranges = [range1, range2, range3];
// Set protection for all the sheet minus QC/PLN ranges
for(var i = 0; i < ranges.length; i++) {
var rangeProtection = ranges[i].protect().setDescription('Range protection');
rangeProtection.addEditor(me);
rangeProtection.removeEditors(rangeProtection.getEditors());
if (rangeProtection.canDomainEdit()) {
rangeProtection.setDomainEdit(false);
}
}
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
// Set protection for QC range
var QC_protection = QC_Range.protect().setDescription('QC protection');
QC_protection.removeEditors(QC_protection.getEditors());
QC_protection.addEditor('Editor1@gmail.com');
if (QC_protection.canDomainEdit()) {
QC_protection.setDomainEdit(false);
}
// Set protection for PLN range
var PLN_protection = PLN_Range.protect().setDescription('PLN protection');
PLN_protection.removeEditors(PLN_protection.getEditors());
PLN_protection.addEditor('Editor2@gmail.com');
if (PLN_protection.canDomainEdit()) {
PLN_protection.setDomainEdit(false);
}
}
}
希望这对你有用!