新增查当周按钮,代码重构与数据分离
- 新增"查当周"按钮,计算当周周一至周日的加班总工时 - 新增 createQueryButton 工厂函数,消除月按钮重复代码 - 修复 calculateTimeDifference 返回字符串导致 NaN 的 bug - 修复 sumOfSecondColumn/get_work_time 缺少空值保护的 bug - 修复 isWorkDay 中 == 改为 === - var 统一为 const/let,提取 QUERY_DELAY_MS 常量 - 提取 getCustomMonthRange 中重复的 formatLocal - 按钮增加 cursor:pointer 样式 - 数据与代码分离:main.template.js(~300行)+ build.py 自动组装 main.js - update.py/comp_json.py 支持命令行年份参数,默认当年 - comp_json.py 增加去重逻辑 - 更新 README 为 GitHub 开源项目风格,含快速上手和每年更新章节 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
194
main.js
194
main.js
@@ -14,7 +14,7 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
// 硬编码工作日数据
|
||||
const workDayData = {
|
||||
const workDayData = {
|
||||
"list": [
|
||||
{
|
||||
"year": 2024,
|
||||
@@ -21937,7 +21937,7 @@
|
||||
"holiday_recess": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const workDayList = workDayData.list;
|
||||
function isWorkDay(weekday, date) {
|
||||
// 将日期格式化为 YYYYMMDD
|
||||
@@ -21945,7 +21945,7 @@
|
||||
// 查找工作日列表中是否存在该日期
|
||||
const foundItem = workDayList.find(item => String(item.date) === date);
|
||||
if (!foundItem) {
|
||||
if (weekday === "周六" || weekday == "周日") {
|
||||
if (weekday === "周六" || weekday === "周日") {
|
||||
return false; // 周六和周日默认不是工作日
|
||||
}
|
||||
else{
|
||||
@@ -21977,7 +21977,8 @@
|
||||
const validTimes = parsedTimes.filter(time => time !== null);
|
||||
|
||||
if (validTimes.length === 0) {
|
||||
return 'No valid times provided';
|
||||
console.warn(`calculateTimeDifference: 没有有效的时间数据, timeString="${timeString}"`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 计算最大值和最小值
|
||||
@@ -22004,19 +22005,23 @@
|
||||
}
|
||||
|
||||
function sumOfSecondColumn(tableId) {
|
||||
var table = document.getElementById(tableId);
|
||||
var totalSum = 0;
|
||||
var rows = table.getElementsByTagName('tr');
|
||||
const table = document.getElementById(tableId);
|
||||
if (!table) {
|
||||
console.error(`sumOfSecondColumn: 未找到ID为 "${tableId}" 的表格`);
|
||||
return 0;
|
||||
}
|
||||
let totalSum = 0;
|
||||
const rows = table.getElementsByTagName('tr');
|
||||
|
||||
// 遍历每行 从1开始跳过表头
|
||||
for (var i = 1; i < rows.length; i++) {
|
||||
var cells = rows[i].getElementsByTagName('td');
|
||||
for (let i = 1; i < rows.length; i++) {
|
||||
const cells = rows[i].getElementsByTagName('td');
|
||||
if (cells.length > 1) { // 确保该行至少有两列
|
||||
var cellValue = cells[3].textContent;
|
||||
var weekday = cells[2].textContent;
|
||||
var date = cells[1].textContent;
|
||||
var workDay = isWorkDay(weekday, date);
|
||||
var difference = calculateTimeDifference(cellValue, workDay);
|
||||
const cellValue = cells[3].textContent;
|
||||
const weekday = cells[2].textContent;
|
||||
const date = cells[1].textContent;
|
||||
const workDay = isWorkDay(weekday, date);
|
||||
const difference = calculateTimeDifference(cellValue, workDay);
|
||||
totalSum += difference;
|
||||
|
||||
// 修改单元格的值
|
||||
@@ -22053,10 +22058,18 @@
|
||||
}
|
||||
|
||||
function getCustomMonthRange(monthsAgo) {
|
||||
const formatLocal = (date) => {
|
||||
return date.toLocaleDateString('en-CA', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
}).replace(/\//g, '-');
|
||||
};
|
||||
|
||||
const now = new Date();
|
||||
let year = now.getFullYear();
|
||||
let month = now.getMonth();
|
||||
let day = now.getDate();
|
||||
const day = now.getDate();
|
||||
|
||||
// 如果今天是21号及以后,当前周期是本月21到下月20
|
||||
// 否则,周期是上月21到本月20
|
||||
@@ -22074,13 +22087,6 @@
|
||||
}
|
||||
const firstDay = new Date(startYear, startMonth, 21);
|
||||
const lastDay = new Date(endYear, endMonth, 20);
|
||||
const formatLocal = (date) => {
|
||||
return date.toLocaleDateString('en-CA', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
}).replace(/\//g, '-');
|
||||
};
|
||||
return {
|
||||
firstDay: formatLocal(firstDay),
|
||||
lastDay: formatLocal(lastDay)
|
||||
@@ -22099,13 +22105,6 @@
|
||||
}
|
||||
const firstDay = new Date(startYear, startMonth, 21);
|
||||
const lastDay = new Date(endYear, endMonth, 20);
|
||||
const formatLocal = (date) => {
|
||||
return date.toLocaleDateString('en-CA', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
}).replace(/\//g, '-');
|
||||
};
|
||||
return {
|
||||
firstDay: formatLocal(firstDay),
|
||||
lastDay: formatLocal(lastDay)
|
||||
@@ -22113,6 +22112,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
const QUERY_DELAY_MS = 500;
|
||||
|
||||
// 创建菜单按钮
|
||||
function createMenuButton(text, options = {}) {
|
||||
const button = document.createElement('button');
|
||||
@@ -22132,90 +22133,88 @@
|
||||
padding: "0px 10px",
|
||||
textDecoration: "none",
|
||||
color: "#e5e5e5",
|
||||
cursor: "pointer",
|
||||
};
|
||||
Object.assign(button.style, copiedStyles);
|
||||
|
||||
|
||||
// 处理点击事件回调
|
||||
if (typeof options.onClick === 'function') {
|
||||
button.addEventListener('click', options.onClick);
|
||||
}
|
||||
return button;
|
||||
}
|
||||
// 查询工作时间
|
||||
function get_work_time() {
|
||||
const message = sumOfSecondColumn('grid').toFixed(2);
|
||||
const table = document.querySelector("#gview_grid > div.ui-state-default.ui-jqgrid-hdiv > div > table > thead > tr")
|
||||
const rows = table.getElementsByTagName('th');
|
||||
// 修改表头
|
||||
rows[5].querySelector('#jqgh_grid_fillCardTimeStr').textContent = `加班时间(${message}小时)`;
|
||||
|
||||
// 创建查询按钮(设置日期范围并查询)
|
||||
function createQueryButton(text, rangeGetter) {
|
||||
return createMenuButton(text, {
|
||||
onClick: () => {
|
||||
const { firstDay, lastDay } = rangeGetter();
|
||||
console.log(`firstDay: ${firstDay}, lastDay: ${lastDay}`);
|
||||
simulateDateInput('beginDate', firstDay);
|
||||
simulateDateInput('endDate', lastDay);
|
||||
queryButton.click();
|
||||
setTimeout(() => {
|
||||
get_work_time();
|
||||
}, QUERY_DELAY_MS);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var query_button = document.getElementById("query");
|
||||
// 创建菜单按钮
|
||||
const button = createMenuButton("统计加班时间", {
|
||||
// 计算当周(周一至周日)的日期范围
|
||||
function getCurrentWeekRange() {
|
||||
const now = new Date();
|
||||
const dayOfWeek = now.getDay(); // 0=周日, 1=周一, ..., 6=周六
|
||||
const mondayOffset = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
|
||||
const monday = new Date(now);
|
||||
monday.setDate(now.getDate() + mondayOffset);
|
||||
const sunday = new Date(monday);
|
||||
sunday.setDate(monday.getDate() + 6);
|
||||
|
||||
const formatLocal = (date) => {
|
||||
return date.toLocaleDateString('en-CA', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
}).replace(/\//g, '-');
|
||||
};
|
||||
return {
|
||||
firstDay: formatLocal(monday),
|
||||
lastDay: formatLocal(sunday)
|
||||
};
|
||||
}
|
||||
|
||||
// 查询工作时间
|
||||
function get_work_time() {
|
||||
const totalSum = sumOfSecondColumn('grid');
|
||||
const headerTable = document.querySelector("#gview_grid > div.ui-state-default.ui-jqgrid-hdiv > div > table > thead > tr");
|
||||
if (!headerTable) {
|
||||
console.error('get_work_time: 未找到表头元素');
|
||||
return;
|
||||
}
|
||||
const rows = headerTable.getElementsByTagName('th');
|
||||
const titleCell = rows[5].querySelector('#jqgh_grid_fillCardTimeStr');
|
||||
if (!titleCell) {
|
||||
console.error('get_work_time: 未找到表头加班列元素');
|
||||
return;
|
||||
}
|
||||
titleCell.textContent = `加班时间(${totalSum.toFixed(2)}小时)`;
|
||||
}
|
||||
|
||||
const queryButton = document.getElementById("query");
|
||||
|
||||
const button = createMenuButton("统计加班时间", {
|
||||
onClick: () => {
|
||||
get_work_time();
|
||||
}
|
||||
})
|
||||
const button0 = createMenuButton('查当月', {
|
||||
onClick: () => {
|
||||
const month = getCustomMonthRange(0);
|
||||
const firstDay = month.firstDay;
|
||||
const lastDay = month.lastDay;
|
||||
console.log(`firstDay: ${firstDay}, lastDay ${lastDay}`)
|
||||
simulateDateInput('beginDate', firstDay);
|
||||
simulateDateInput('endDate', lastDay);
|
||||
query_button.click();
|
||||
setTimeout(function() {
|
||||
get_work_time();
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
const button1 = createMenuButton('查上月', {
|
||||
onClick: () => {
|
||||
const month = getCustomMonthRange(1);
|
||||
const firstDay = month.firstDay;
|
||||
const lastDay = month.lastDay;
|
||||
console.log(`firstDay: ${firstDay}, lastDay ${lastDay}`)
|
||||
simulateDateInput('beginDate', firstDay);
|
||||
simulateDateInput('endDate', lastDay);
|
||||
query_button.click();
|
||||
setTimeout(function() {
|
||||
get_work_time();
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
const button2 = createMenuButton('查上上月', {
|
||||
onClick: () => {
|
||||
const month = getCustomMonthRange(2);
|
||||
const firstDay = month.firstDay;
|
||||
const lastDay = month.lastDay;
|
||||
console.log(`firstDay: ${firstDay}, lastDay ${lastDay}`)
|
||||
simulateDateInput('beginDate', firstDay);
|
||||
simulateDateInput('endDate', lastDay);
|
||||
query_button.click();
|
||||
setTimeout(function() {
|
||||
get_work_time();
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
const button3 = createMenuButton('查上上上月', {
|
||||
onClick: () => {
|
||||
const month = getCustomMonthRange(3);
|
||||
const firstDay = month.firstDay;
|
||||
const lastDay = month.lastDay;
|
||||
console.log(`firstDay: ${firstDay}, lastDay ${lastDay}`)
|
||||
simulateDateInput('beginDate', firstDay);
|
||||
simulateDateInput('endDate', lastDay);
|
||||
query_button.click();
|
||||
setTimeout(function() {
|
||||
get_work_time();
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
const button0 = createQueryButton('查当月', () => getCustomMonthRange(0));
|
||||
const button1 = createQueryButton('查上月', () => getCustomMonthRange(1));
|
||||
const button2 = createQueryButton('查上上月', () => getCustomMonthRange(2));
|
||||
const button3 = createQueryButton('查上上上月', () => getCustomMonthRange(3));
|
||||
const button4 = createQueryButton('查当周', getCurrentWeekRange);
|
||||
|
||||
// 获取要将按钮添加到其中的容器
|
||||
var container = document.getElementById('seclevelmenu');
|
||||
let container = document.getElementById('seclevelmenu');
|
||||
if (!container) {
|
||||
container = document.body;
|
||||
}
|
||||
@@ -22224,6 +22223,7 @@
|
||||
container.appendChild(button1);
|
||||
container.appendChild(button2);
|
||||
container.appendChild(button3);
|
||||
container.appendChild(button4);
|
||||
console.log("复旦微加班时间统计脚本已加载");
|
||||
console.log("点击按钮后会统计加班时间");
|
||||
})()
|
||||
Reference in New Issue
Block a user