Try it again~
- function 汇总消费数据() {
- const awf = Application.WorksheetFunction;
- const hcs = Sheets('信息表').Cells(1).CurrentRegion().slice(1).reduce((a, [A, B, C]) => {
- a[B] ??= A; //以身份证号码为键,匹配姓名
- a[C] ??= [0, []];
- a[C][0]++; //以部门为键,汇总人数
- a[C][1].push(B); //以部门为键,收集身份证号码
- return a;
- }, {});
- const dpt = []; //储存部门列表
- for (let d in hcs) {
- if (typeof hcs[d] === 'object') dpt.push(d);
- }
- const dic = Sheets('订单表').Cells(1).CurrentRegion().slice(1).reduce((a, [A, B, C, D, E, , G, , , , , L]) => {
- let od = awf.Text(A, 'yyyy-mm-dd');
- a[od] ??= {};
- if (!a[od][E]) { //按日期和部门汇总
- let str = E === '教职工' ? '' : hcs[E][1].join('、') + '、';
- a[od][E] = [hcs[E][0], str, 0, 0, str, 0, 0, str, 0, 0]; //初始化结果数组
- }
- let idx = ['早餐', '午餐', '晚餐'].indexOf(L) * 3 + 1;
- a[od][E][idx] = E === '教职工' ? a[od][E][idx] + '、' + C : a[od][E][idx].replace(B + '、', ''); //提取身份证号
- a[od][E][idx + 1] += Number(G); //累加金额
- a[od][E][idx + 2]++; //统计人数
- return a;
- }, {});
- const res = [];
- for (let d in dic) { //循环写入统计结果到res中
- dpt.forEach(d1 => { //在部门列表中循环
- if (dic[d][d1]) {
- let arr = dic[d][d1].map((v, j, brr) => {
- return (j - 1) / 3 === parseInt((j - 1) / 3) && brr[j + 1] === 0 ? '' : String(v).split('、').filter(Boolean).map(s => hcs[s] || s).join('、');
- });
- res.push([d, d1, ...arr]);
- }
- });
- }
- res.sort((a, b) => a[0].localeCompare(b[0])); //按日期排序
- res.unshift(['日期', '部门', '人数', '早餐', , , '午餐', , , '晚餐'], [, , , ...Array(3).fill(['名单', '金额', '人数']).flat()]);
- Application.ScreenUpdating = false;
- with (Sheets('汇总表').Range('A2').Resize(res.length, res[1].length)) {
- CurrentRegion.Clear();
- Value2 = res;
- [...Rows(1).Cells].forEach(rng => { //合并单元格
- if (rng() && !rng.Offset(1, 0)()) rng.Resize(2, 1).Merge();
- if (rng() && !rng.Offset(0, 1)()) rng.Resize(1, 3).Merge();
- });
- Rows('1:2').Font.Bold = true;
- Borders.LineStyle = xlContinuous;
- HorizontalAlignment = xlHAlignCenter;
- WrapText = true;
- }
- Application.ScreenUpdating = true;
- }
复制代码 |