【发布时间】:2020-06-22 22:19:41
【问题描述】:
下面的代码可以解析 Excel 文件,获取用户的日程安排并将其添加到对象中。如果我使用数组,我可以在班次的开始和结束时推送单个日期对象。但是,如果我想改用一个对象并将日期作为该对象的单独属性,那么它每次通过循环时都会被覆盖。我能做到吗?
const ExcelJS = require("exceljs");
let date = new Date(2020, 05, 01);
var moment = require("moment");
const schedules = {
"user1": [],
};
shifts = {
"9-18": {
start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("18:00:00", "HH:mm:ss").format("hh:mm A"),
},
"12-21": {
start: moment("12:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
},
"10-14": {
start: moment("10:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("14:00:00", "HH:mm:ss").format("hh:mm A"),
},
"9-16": {
start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("116:00:00", "HH:mm:ss").format("hh:mm A"),
},
"15-21": {
start: moment("15:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
},
"21-1": {
start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("1:00:00", "HH:mm:ss").format("hh:mm A"),
},
"21-6": {
start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("6:00:00", "HH:mm:ss").format("hh:mm A"),
},
"00-9": {
start: moment("00:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
},
DO: { start: null, end: null },
CO: { start: null, end: null },
};
const readxl = async () => {
const workbook = new ExcelJS.Workbook();
const file = await workbook.xlsx.readFile("../../June.xlsx");
const worksheet = file.getWorksheet("June");
let cell = 5;
let row = 9;
let name = 4;
while (true) {
let rows = worksheet.getRow(row);
if (row === 10) {
break;
} else if (row === 19) {
row = 28;
} else if (row === 31) {
row = 33;
} else if (cell === 35) {
date.setDate(date.getDate() - 30);
row += 1;
console.log(row);
cell = 5;
}
while (true) {
if (cell === 35) {
break;
}
// console.log(
// date.toLocaleDateString(),
// rows.getCell(name).value,
// `${shifts[rows.getCell(cell).value].start} - ${
// shifts[rows.getCell(cell).value].end
// }`
// );
schedules[rows.getCell(name).value].push({
[date.toLocaleDateString()]: {
start: shifts[rows.getCell(cell).value].start,
end: shifts[rows.getCell(cell).value].end,
},
});
cell++;
date.setDate(date.getDate() + 1);
}
}
console.log(schedules["user1"]);
};
readxl();
【问题讨论】:
标签: javascript node.js json excel object