[英]Google Sheets App Script lock cells for specific days
我試圖在前 10 天的每個月解鎖特定范圍的單元格,然后鎖定該范圍直到下個月。 這是我一直在做的事情。 但這並不像我預期的那樣有效。 任何幫助表示贊賞。
function tenDaysAllowance() {
var ss = SpreadsheetApp.getActive();
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Online Allowance");
var todaysdate = Utilities.formatDate(new Date(), 'GMT+5', 'dd/MM/yyyy');
var firstDate = new Date();
var fd = firstDate;
fd.setMonth(fd.getMonth());
fd.setDate(1);
var td = new Date();
td.setMonth(td.getMonth());
td.setDate(10);
var firstDay = Utilities.formatDate(fd, 'GMT+5', 'dd/MM/yyyy');
var afterTen = Utilities.formatDate(td, 'GMT+5', 'dd/MM/yyyy');
Logger.log(firstDay);
Logger.log(afterTen);
if (todaysdate >= firstDay && todaysdate <= afterTen) {
Logger.log("todays date is in range");
var range = ss.getRange('B27:AF40');
var protection = range.protect().setDescription('Sample protected range');
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
} else {
Logger.log("todays date is not in range");
var range = ss.getRange('B27:AF40');
var allProtections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
var matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'B27:AF40';
});
var protection = matchingProtections[0];
protection.remove();
};
};
在您的 google 工作表中,使用today()
來確定今天的日期和day()
來確定今天日期的值。
function myProtection() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Date Check");
var date=ss.getRange("B1");
//if it more the 10th of the month lock sheet, else allow others to edit
if(date > 10)
{
// Protect range A1:B10, then remove all other users from the list of editors.
var range = ss.getRange('B27:AF40');
var protection = range.protect().setDescription('Sample protected range');
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
else{
var allProtections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
var matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'B27:AF40';
});
var protection = matchingProtections[0];
protection.remove();
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.