|
楼主 |
发表于 2024-10-1 09:15
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- function 汇总收入明细() {
- const [wst1,wst2]=[...ThisWorkbook.Sheets];
- const reportMonth=new Date(wst2.Range('A1').Value2.match(/\d+/g).join('/')+'/2');
- const dates=wst1.Range('A1').CurrentRegion().slice(1).flat().filter(d=>{
- return new Date(d).getYear()=== reportMonth.getYear() && new Date(d).getMonth()===reportMonth.getMonth();
- });
- const clazz=wst1.Range('C1').CurrentRegion().slice(1);
- const weekday=(date)=>{//输入日期返回星期几
- if(Date.parse(date) !== NaN) return '星期' + '日一二三四五六'[new Date(date).getDay()];
- }
- const res=[];
- res.push(['低段','应到数',...dates.map(d=> Array(3).fill(d)).flat(),'顿人数总计',,,'应收金额',,,'班主任签字']);
- res.push([, , ...dates.map(d => Array(3).fill(weekday(d))).flat(),...Array(6).fill('小计')]);
- res.push([,,...Array(dates.length + 2).fill(['早','午','晚']).flat()]);
- const sumOfMeal = `=SUMPRODUCT(R5C3:R5C${dates.length * 3 + 2}=R5C)*RC3:RC${dates.length*3+2})`; // 顿人数总计
- const sumOfAR = `=RC[-3]*if(R5C="早",3,5)`; // 应收金额
- const sumOfRow = [...Array(3).fill(sumOfMeal), ...Array(3).fill(sumOfAR)]; // 定义行合计数组
- const junior = clazz.filter(([c]) => Number(c) < 4); // 筛选低年级信息
- junior.forEach(([c, s]) => {
- res.push([c, s, ...res[0].slice(2, -7).map((d, j) => j % 3===2 ? '' : s), ...sumOfRow]);
- });
- res.push(['低段人数合计', ...Array((dates.length + 2) * 3 + 1).fill(`=SUM(R[-${junior.length}]C:R[-1]C)`)]);
- const senior = clazz.filter(([c]) => Number(c) > 4); // 筛选高年级信息
- senior.forEach(([c, s]) => {
- res.push([c, s, ...res[0].slice(2, -7).map((d, j, arr) => arr[j+1]-d > 86400000 ? '' : s), ...sumOfRow]);
- });
- res.push(['高段人数合计', ...Array((dates.length + 2) * 3 + 1).fill(`=SUM(R[-${senior.length}]C:R[-1]C)`)]);
- res.push(['就餐学生总计', ...Array((dates.length + 2) * 3 + 1).fill(`=SUM(R[-${junior.length + senior.length + 2}]C:R[-1]C)/2`)]);
- const teachers = clazz.filter(([c]) => c === '陪餐教师').flat();
- res.push([...teachers, ...res[0].slice(2, -7).map((d, j,arr) => arr[j+1]-d > 86400000 ? '' : teachers[1]), ...sumOfRow]);
- const juniorStaff = clazz.filter(([c]) => c === '低段工友').flat();
- res.push([...juniorStaff, ...res[0].slice(2, -7).map((d, j) => j % 3 === 2 ? '' : juniorStaff[1]), ...sumOfRow]);
- const seniorStaff = clazz.filter(([c]) => c === '高段工友').flat();
- res.push([...seniorStaff, ...res[0].slice(2, -7).map((d, j,arr) => arr[j+1]-d > 86400000 ? '' : seniorStaff[1]), ...sumOfRow]);
- res.push(['同餐同费人数合计', ...Array((dates.length + 2) * 3 + 1).fill(`=SUM(R[-3]C:R[-1]C)`)]);
- res.push(['就餐总人数合计', ...Array((dates.length + 2) * 3 + 1).fill(`=SUM(R[-5]C,R[-1]C)`)]);
- wst2.Range('3:999').Clear();
- with(wst2.Range('A3').Resize(res.length,res[0].length)){
- Value2 = res;
- }
-
- }
复制代码
仔细核对代码没有抄错,结果在Value2 = res;还报错 |
|