🚧app.parseFields()
⬆️ 需要: SheetFields, SheetField
/**
* app.parseFields(sheetName)
* ===============================
* 主要功能:
* 1. 解析相關欄位的索引
* 2. 將解析完的索引存入 DataRow
*/
app.parseFields = function(sheetName) {
// parse fields index in sheet:
let fields = new SheetFields(sheetName, [
['班級', /班級/u],
['座號', /座號/u],
['姓名', /姓名/u],
['平均分數', /平均分數/u],
['名次', /名次/u],
]);
// save col indices in DataRow
DataRow.classCol = fields.indexOfField('班級');
DataRow.seatCol = fields.indexOfField('座號');
DataRow.nameCol = fields.indexOfField('姓名');
DataRow.avgCol = fields.indexOfField('平均分數');
DataRow.rankCol = fields.indexOfField('名次');
return fields;
};
/**
* app.parseSheetRows(sheetName)
* =============================
* 主要功能:
* 1. 解析「學生成績」存入 app.students: Student[]
* 2. 得獎名單存入 app.awardList: Student[]
*/
app.parseSheetRows = function(sheetName) {
let filename = 'app.parseSheetRows';
// get values from sheet
let sheet = app.sheet.byName(sheetName);
let values = sheet.getDataRange().getValues(); // 2D array
LOG(filename, `🚩 開始解析「${sheetName}」頁面的「學生成績」(共 ${values.length} 列,${values[0].length} 欄)`);
let students = [];
let count = 0 // count student records
// data rows
forLoop:
for(let row of values) {
// if empty, skip
if(row.join('').length === 0) continue;
// not empty row
count += 1;
// ⭐️ raw array -> RawData
let dataRow = new DataRow(row);
var type = dataRow.type;
// debug
// if(this.debugMode && count < 10) LOG(filename, `row = [${row}]\ntype = ${type}`);
switch(type) {
// -------- 學生成績列 --------
case RowType.student:
// 放入學生的「班級」、「座號」「姓名」
let classNo = row[DataRow.classCol];
let name = row[DataRow.nameCol];
let seatNo = row[DataRow.seatCol];
let avg = row[DataRow.avgCol];
let rank = row[DataRow.rankCol];
let student = new Student(classNo, seatNo, name, avg, rank);
students.push(student);
// if(app.debugMode && count < 10) LOG(filename, `student = [${toString(student)}]`);
break;
default:
// 其他狀況,不處理,直接跳下一筆
break;
}// end switch
}// end for loop
app.students = students;
app.awardList = students.filter(s => s.isAwarded);
// defence
const n = students.length;
if (n === 0) throw new Error(`⛔️ app.parseSheet() 沒有解析出任何學生成績❗️`);
// milestone
let msg = `🏁 共解析出學生 ${n} 人,得獎名單 ${app.awardList.length} 人。`;
LOG(filename, msg);
app.log(msg);
};
Last updated