๐ง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