在我的工作表 A 列中有日期,我想锁定范围 ColBC、ColE 和 ColGH 直到 A 列中日期所在的行(今天 -2)。例如。如果今天是 3 月 22 日,A 列中的日期到第 151 行是 3 月 20 日或更少,并且从第 152 行 3 月 21 日起开始,那么我想保护上述三个范围,直到第 151 行。 151. 此外,我想在受保护的范围内添加两个编辑器。
我根据以下两个链接的解决方案设计了一个脚本。该脚本正在运行并应用保护,并在上述三个范围中添加编辑器,但仅对第 2 行应用保护。
下面是我采用脚本的链接。
链接1
此链接提供了锁定多个范围的解决方案
Google Sheets Lock 使用一个脚本应用三种不同范围的保护
链接2
此链接提供了基于日期锁定范围的解决方案。
下面是我正在使用的脚本
function rangeLock() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1'); //Change the sheet name if necessary
var end = sourceSheet.getLastRow();
var sourceRange = sourceSheet.getRange(1, 2, sourceSheet.getLastRow()).getValues().flat();
var editors = ['[email protected]', '[email protected]'] //Update this array with the
editors' actual email addresses.
var dateRange = sourceSheet.getRange(2, 1, sourceSheet.getLastRow()-2, 1);
var curDate = Utilities.formatDate(new Date(), "GMT+1", "dd/M/YYYY");
var val = dateRange.getDisplayValues();
for(var x = 0; x <= end; x++){
if(val[x][0]> curDate){
row = x;
break;
}
}
var colBC = sourceSheet.getRange(2, 2, row, 2).getA1Notation();
var colE = sourceSheet.getRange(2, 5, row).getA1Notation();
var colGH = sourceSheet.getRange(2, 7, row, 2).getA1Notation();
var rangesToLock = sourceSheet.getRangeList([colBC, colE, colGH]).getRanges(); //returns the
specific ranges to lock
var currentProtection = sourceSheet.getProtections(SpreadsheetApp.ProtectionType.RANGE) //Gets
the sheet's existing protected ranges.
//if there are existing protected ranges, the if statement below will remove it prior to
protecting the identified ranges.
if(currentProtection.length > 0) currentProtection.forEach(p => p.remove());
rangesToLock.forEach(range => {
protectedRange = range.protect()
protectedRange.removeEditors(protectedRange.getEditors());
protectedRange.addEditors(editors);
if (protectedRange.canDomainEdit()) protectedRange.setDomainEdit(false);
})
}
以上任何帮助将不胜感激。
我正在使用这个脚本:
const dateColumn = 1; // date column
const workSheetName = ['Склад готовой продукции', 'Склад сырья', 'Склад оборудования/хоз. товаров']; // sheets where we will close the range
const protectRangeName = 'закрытые дни'; // range name
/**
* who will be allowed to edit the protected range
* list the emails of other employees separated by commas,
* you may not add yourself.
*/
const editors = ['[email protected]'];
function closeRows() {
fworkSheetName.forEach((i) => {
const sheet = ss.getSheetByName(i);
const fr = findRow(sheet, dateColumn);
if (fr > 0) {
delProtection(sheet, protectRangeName);
createProtection([sheet.getRange(2, 1, fr - 2, sheet.getMaxColumns() - 1)], [protectRangeName]);
}
else {
}
});
}
function findRow(sh, column) {
const data = sh.getDataRange().getValues();
var date = new Date((new Date).getTime() - 1000 * 60 * 60 * 24); // date after which we will close the ranges
return Math.max(...(data.map((g, i) => g[column - 1] && g[column - 1] <= date ? ++i : '')));
}
function delProtection(sheet, name) {
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)
.forEach((protection) => {
if (protection.canEdit()) {
protection.getDescription() == name ? protection.remove() : '';
}
})
}
function createProtection(range, names) {
var me = Session.getEffectiveUser();
range.map((rng, i) => {
var protection = rng.protect().setDescription(names[i]);
protection.removeEditors(protection.getEditors());
protection.canDomainEdit() ? protection.setDomainEdit(false) : '';
protection.addEditors([me].concat(editors));
})
}