今天分享一个【wps云文档js宏-多维表累计求和功能】,挺有用的一个功能,可以用于累计出入库等
//操作选中的表
const sheet = Application.Selection.GetActiveSheet()
//将记录集转为数组
const rec = Array.from(fetchAllRecords())
//按编号排序
rec.sort((a, b) => a.fields["编号"] - b.fields["编号"])
let arr = []
let a = 0
//遍历数字字段累加,这种加法是全部编号相加,适合进销存
rec.forEach(x => {
console.log(x,x.fields["数字"])
a += x.fields["数字"]+1000
arr.push(a)
console.log("求和后:",a)
})
//修改字段的字符串,用来更新记录集,将数据更新到累计字段
rec.forEach((item,idex)=>{
item.fields={"上期期末累计求和":arr[idex]}
}
)
//更新记录集
Application.Record.UpdateRecords({
SheetId: sheet.sheetId,
Records: rec,
})
//获取所有记录行
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
}
fetchAllRecords()