【发布时间】:2021-01-08 15:33:49
【问题描述】:
目标: 我正在尝试根据用户编辑单元格的日期添加 1 到 3 天来进行日期计算。以下是希望具备的主要条件。
- 如果在周一至周四的下午 2:00 到下午 6:30 (JST) 之间,则将当前日期加 1 天。
- 如果是在星期五下午 2:00 到下午 6:30 (JST) 之间,则将当前日期加上 3 天以在星期一得到它,避免得到周末日期。
- 如果星期一或任何其他工作日不是基于日本银行假日系统的工作日,我想添加所需的天数,直到我们可以得到下一个工作日。例如,2021 年 1 月 11 日是星期一,但是对于日本来说,这是一个银行假日,所以在这种情况下,我希望得到 1 月 12 日。
问题 我不知道如何或者是否有可能以某种方式自动添加日期并始终获得基于日本银行假日系统的工作日。
当前代码 这是我目前拥有的代码,这是我从@NaziA 收到的,它很好用,因为它不包括周末。但我只是想知道我们是否可以更进一步,排除日本的银行假日,只获得日本的工作日。 Continuation to this question.
function onEdit(e) {
const sheetName = 'Status';
const statusSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const cell = statusSheet.getActiveCell();
const row = cell.getRow();
const column = cell.getColumn();
const editedRangeValue = cell.getValue();
// var currentDate = new Date(new Date().toLocaleString('en-US', { timeZone: 'Japan' }));
// The above line of code translates your time into japan timezone but since
// sheets is converting your timezone automatically to the regional one,
// it adjusts an additional day when used. Be careful in using the above line.
var currentDate = new Date();
// Use the line below to test different time slots, we do not want to wait the
// exact time just to test this script.
// currentDate.setHours(11, 30, 0 ,0);
startDate = new Date(currentDate.getTime());
startDate.setHours(9, 30, 0, 0);
endDate1 = new Date(currentDate.getTime());
endDate1.setHours(14, 0, 0, 0);
endDate2 = new Date(currentDate.getTime());
endDate2.setHours(18, 30, 0, 0);
// Column numbers for Status sheet
const caseTypeColumnStatus = 4;
if (sheetName == 'Status' && column == caseTypeColumnStatus && row > 15 && editedRangeValue == 'Chat => Email') {
var statusColumn = column + 3;
if (startDate <= currentDate && endDate1 > currentDate) { // If the current time is between 9:30 A.M. and 2:00 P.M. (JST), then TRUE.
// Insert today's date
statusSheet.getRange(row, statusColumn).setValue(currentDate);
} else if (endDate1 <= currentDate && endDate2 > currentDate) { // If the current time is between 2:00 P.M. and 6:30 P.M. (JST), then TRUE.
// Insert tomorrow's date
var tomorrow = new Date(currentDate.setDate(currentDate.getDate() + 1));
if(tomorrow.getDay() == 6){
// if saturday, add 2 days
tomorrow.setDate(tomorrow.getDate() + 2);
}
else if(tomorrow.getDay() == 0){
// if sunday, add 1 day
tomorrow.setDate(tomorrow.getDate() + 1);
}
statusSheet.getRange(row, statusColumn).setValue(tomorrow);
}
}
}
【问题讨论】: