我正在尝试提出一个简单的条件格式公式,以突出显示日期比今天的日期早三个月的单元格。似乎“日期早于”选项仅提供了几个选项,但似乎没有一个选项允许我在寻找什么。是否有一个可以完成此任务的自定义公式?
编辑:在相关列的片段上附加一个片段:
答案 0 :(得分:1)
答案 1 :(得分:0)
转到条件格式设置规则中的自定义公式,并使用以下方法:
=DATEDIF(A1,TODAY(),"D")<90
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以使用Apps Script和Custom Menu来解决根据日期设置单元格颜色的问题。转到 工具->脚本编辑器 并粘贴以下代码:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Check Difference', 'dateDifference')
.addToUi();
}
function dateDifference(){
var sheet = SpreadsheetApp.getActiveSheet().getActiveRange(); // Get the selected range on the sheet
var dates = sheet.getValues(); // Get the values in the selected range
var oneDay = 1000*60*60*24;
var row = 1;
var re = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/; // This will help you to check if it's really a date
dates.forEach(function(el){ // Iterate over each value
if(typeof el[0] == 'object'){ // Check if it's really a date
var gmtZone = el[0].toString().split(" ")[4].split(":")[0]; // Take the date's GMT
var dateFormatted = Utilities.formatDate(el[0], gmtZone, "dd/MM/yyyy"); // Format the date
if(re.test(dateFormatted)){ // Test if it's the right format
// This part will calculate the difference between the current date and the future date
var futureDateMs = new Date(el[0]);
var todayDateMs = (new Date()).getTime();
var differenceInMs = futureDateMs - todayDateMs;
var differenceInDays = Math.round(differenceInMs/oneDay);
if(differenceInDays >= 91.2501){ // Test if the difference it's greater to 91.2501 days (3 motnhs)
sheet.getCell(row, 1).setBackground("#00FF00"); // Set the color to the cell
}
row++;
}
}
});
通过单击 文件->保存 进行保存。
然后,您可以在列中选择一个范围,然后单击 自定义菜单->检查差异 ,如下图所示:
当心您认为是“月”的时间非常重要,我的意思是要考虑多少天。在我的代码中,我采纳了Google对1 = 30.4167
的建议。
这些是我为您提供帮助的其他文档:
我希望这种方法可以为您提供帮助。