练写了一个功能,总感觉要用类比较合适
- function main(){
- Application.ScreenUpdating = false
- Application.DisplayAlerts = false
- //数据入库
- let arr = Sheets("原始成绩").Range("a1").CurrentRegion.Value2.slice(2)
- let stu = {}
- arr.forEach(v=>{
- stu[v[0]] ={
- "班级": v[1],"语文": v[2],"数学": v[3], "英语": v[4], "物理": v[5],
- "生物": v[6],"历史": v[7], "地理": v[8],"道法": v[9],"体育": v[10]
- }
-
- })
- sumKm(stu)
- let kms = ["总分","语文","数学", "英语","物理","生物","历史", "地理","道法","体育"]
- kms.forEach(km => {
- rankGrade(km,stu)
- rankCalss(km,stu)
- })
- //生成成绩汇总排名表
- try{Sheets("成绩汇总排名表").Delete()}catch{}
- let sh = Sheets.Add({After:Sheets("原始成绩")})
- sh.Name = "成绩汇总排名表"
-
- let res = []
- let bt =["姓名","班级","总分","总分年名","总分班名","语文","语文年名","语文班名","数学","数学年名","数学班名",
- "英语","英语年名","英语班名","物理","英语年名","英语班名","生物","生物年名","生物班名",
- "历史","历史年名","历史班名", "地理","地理年名","地理班名","道法","道法年名","道法班名","体育","体育年名","体育班名"]
-
- res.push (bt)
- for (let name in stu){
- let tmp = [name]
- let xm = ["班级","总分","总分年名","总分班名","语文","语文年名","语文班名","数学","数学年名","数学班名",
- "英语","英语年名","英语班名","物理","英语年名","英语班名","生物","生物年名","生物班名",
- "历史","历史年名","历史班名", "地理","地理年名","地理班名","道法","道法年名","道法班名","体育","体育年名","体育班名"]
- xm.forEach(m => tmp.push(stu[name][m]))
- res.push(tmp)
- }
-
- sh.Range("a1").CurrentRegion.ClearContents()
- sh.Range("a1").Resize(res.length,res[0].length).Value2 = res
- sh.Columns("a:ba").AutoFit()
- sh.ListObjects.Add(xlSrcRange,sh.UsedRange,undefined,xlYes);
- Application.ScreenUpdating = true
- Application.DisplayAlerts = true
- }
- function rankGrade (km,stu){
- let tmp =[]
- for ( let name in stu){
- tmp.push([name,stu[name]["班级"],stu[name][km]])
- }
- tmp = tmp.sort((a,b) =>b[2] - a[2] )
- for(let i = 0 ;i< tmp.length ; i++){
- if (i ==0) {
- stu[tmp[i][0]][km+"年名"] = 1
- }else {
- stu[tmp[i][0]][km+"年名"] = tmp[i][2] == tmp[i-1][2]? stu[tmp[i-1][0]][km+"年名"] : stu[tmp[i-1][0]][km+"年名"]+1
- }
- }
- }
- function rankCalss (km,stu){
- let clss ={}
- for ( let name in stu){
- if(!clss[stu[name]["班级"]]) {
- clss[stu[name]["班级"]]=[ [name,stu[name]["班级"],stu[name][km]] ]
- }else{
- clss[stu[name]["班级"]].push([name,stu[name]["班级"],stu[name][km]])
- }
- }
- for(let k in clss){
- let tmp = clss[k].sort((a,b) =>b[2] - a[2] )
- for(let i = 0 ;i< tmp.length ; i++){
- if (i ==0) {
- stu[tmp[i][0]][km+"班名"] = 1
- }else {
- stu[tmp[i][0]][km+"班名"] = tmp[i][2] == tmp[i-1][2]? stu[tmp[i-1][0]][km+"班名"] : stu[tmp[i-1][0]][km+"班名"]+1
- }
- }
- }
- }
- function sumKm(stu){
- let kms = ["语文","数学", "英语","物理","生物","历史", "地理","道法","体育"]
- for ( let name in stu){
- stu[name]["总分"] = 0
- kms.forEach(km => stu[name]["总分"] += stu[name][km] )
- }
- }
复制代码
|