群里有小伙伴问到排名问题,试着写了个脚本,效果如下 下面是代码
const sheet = Application.Selection.GetActiveSheet()
const rec = fetchAllRecords()
Arrsort(rec)
let rankarr=[1]
let n=1
for(let i=1;i < rec.length;i++){
if(rec[i].fields["总分"]==rec[i-1].fields["总分"]){n=n}
else {n=i+1}
rankarr.push(n)
}
rec.forEach((item,idex) => {
item.fields={"成绩排名":rankarr[idex]}
}
)
Application.Record.UpdateRecords({
SheetId: sheet.sheetId,
Records: rec,
})
function Arrsort(arr) {
for(let i = 0; i < arr.length-1; i++){
for(let j = 0; j < arr.length-i-1; j++){
if(arr[j].fields["总分"] < arr[j+1].fields["总分"]){
let tem = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tem
}
}
}
return arr
}
function fetchAllRecords() {
let all = []
let offset = null;
while (all.length === 0 || offset) {
let records = Application.Record.GetRecords({
SheetId: sheet.sheetId,
Offset: offset
})
offset = records.offset
all = all.concat(records.records)
};
return all;
};