|
- function spp(){
- Application.DisplayAlerts=false;
- Application.ScreenUpdating=false;
- for(shtt of Sheets){
- if(shtt.Name !=Sheets(1).Name) shtt.Delete()
- }
- date=Range(`a1`).CurrentRegion();
- arr=date.slice(1);
- bj=Range(`f1`).Value2;
- var boy=[],girl=[];
- //按性别分组并分配随机数
- arr.map(v=>{
- num=Math.random()*10000;
- v[2]==`男` ? boy.push([...v,num]) :girl.push([...v,num]);
- });
- //计算每个班男女平均人数及余数
- boy_avr=Math.floor(boy.length/bj);
- mod_boy=boy.length % boy_avr;
- girl_avr=Math.floor(girl.length/bj);
- mod_girl=girl.length % girl_avr;
- brr=[];
- //生成班级序号及每班男女人数
- for(i=1;i<=bj;i++){
- brr.push([i,boy_avr,girl_avr]);
- };
- //分配男生剩余人数
- i2=0;
- do {
- brr[i2][1]++;
- i2++;
- mod_boy--;
- } while(mod_boy>0);
- //分配女生剩余人数
- i2=brr.length-1;
- do {
- brr[i2][2]++;
- i2--;
- mod_girl--;
- } while(mod_girl>0);
- //男女分组排序
- boy.sort((a,b)=>a[3]-b[3]);
- girl.sort((a,b)=>a[3]-b[3]);
- //按班级男女人数表分配学生
- brr.map(v=>{
- var [bj,boy_count,girl_count]=v;
- stu=boy.splice(0,boy_count).concat(girl.splice(0,girl_count));
- stu.map(vv2=>vv2.pop());
- stu.sort((a,b)=>a[0]-b[0]).unshift(...date.slice(0,1));
- sht=Worksheets.Add(undefined,Sheets(Sheets.Count));
- sht.Name=`${bj}班`;
- sht.Range(`a1`).Resize(stu.length,stu[0].length).Value2=stu;
- sht.UsedRange.Borders.LineStyle=1;
- sht.Columns.AutoFit();
- });
- Application.ScreenUpdating=true;
- Application.DisplayAlerts=true;
- alert(`分班完毕!!!`);
- };
复制代码 |
|