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