You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: 'yz-7g23c92hf3223f1d' }) // 使用当前云环境
//引入node-xlsx文件
var xlsx = require('node-xlsx');
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
console.log("filename : " + event.filename);
let fileID = event.fileID;
let filename = event.filename;
//通过fileID下载云存储里的excel文件
const res = await cloud.downloadFile({
fileID: fileID,
})
console.log('下载的文件', res);
const file_xlsx = res.fileContent
//解析excel文件
var files = xlsx.parse(file_xlsx); //获取到已经解析的对象数组
var arr = files[0].data;
for (let i = 0; i < arr.length; i++) {
arr[i][0] = String(arr[i][0]); // 或者 arr[i][0] = arr[i][0].toString();
}
console.log('获得内容表格数组', arr);
var row = 0;
// console.log(arr[row][0].length + " " + arr[row][0][0]);
function judgeID(s) {
var len = s.length;
if(len == 0) return false;
var flag = true;
for(var i = 0; i < arr[row][0].length; ++i)
if(!(s[i] >= '0' && s[i] <= '9'))
flag = false;
return flag;
}
while (row <= arr.length) {
if(arr[row].length == 0) {
row++;
continue ;
}
if(judgeID(arr[row][0])) break;
row++;
}
// console.log("数据从第" + row + "行开始");
if(row == arr.length) {
return "error";
//返回错误
}
// var namelist = "namelist" + filename;
// console.log("namelist = " + namelist);
while(row <= arr.length - 1 && arr[row].length > 0 && judgeID(arr[row][0])) {
var _student_ID = arr[row][0].toString();
while(_student_ID.length < 9) _student_ID = '0' + _student_ID;
var _name = arr[row][1];
var _attendance_count = arr[row][2]; // 提取被点名次数
if (_attendance_count === undefined || _attendance_count === "") {
_attendance_count = 0; // 如果为空白默认设置为0
}
var _score = arr[row][3]; // 提取积分
if (_score === undefined || _score === "") {
_score = 0; // 如果为空白默认设置为0
}
// console.log(_student_ID + " " + _name);
db.collection("namelist").add({
data: {
student_ID: _student_ID,
name: _name,
attendance_count: _attendance_count,
score: _score,
filename : filename,
state : "present"
}
})
row++;
}
console.log("数据上传完成");
console.log(fileID);
cloud.deleteFile({
fileList: [fileID],
success(res) {
console.log(res, '文件删除成功')
},
fail(res) {
console.log(res, "文件删除失败")
}
})
return "success";
}