我試圖在前 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();
};
};
uj5u.com熱心網友回復:
在您的谷歌表中使用 atoday()來確定今天的日期并確定今天日期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();
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457226.html
