跳至內容
Astera Technology

把續頁資料接回指定表頭

確認續表編號及行寬才借用前頁表頭,不自行補欄名。

下載完整個案資料及參考答案 ↓

訂明本例驗收任務

假設續表有上一表的非空文字編號、不重複的有序表頭及該頁列明的續表編號。兩編號一致才沿用表頭,每行格數須同表頭數目。續表不符或格數不齊便列版面錯誤,不自行補欄名。

  1. 比對明示續表編號
  2. 按表頭數核對每行格數
  3. 列出欄名與格值關係

欠續表編號或行寬不齊,保留版面覆核。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
已列明接續同一表{"previous":{"id":"T1","headings":["code","quantity"]},"continues":"T1","rows":[["P8",6]]}{"rows":[{"code":"P8","quantity":6}]}T1 為該續行提供兩個欄名。
該頁續另一張表{"previous":{"id":"T1","headings":["code","quantity"]},"continues":"T2","rows":[["P8",6]]}{"error":"continuation"}附近表頭也不能代替不同表編號。
兩個表編號都欠{"previous":{"headings":["code"]},"rows":[["P8"]]}{"error":"continuation-identity"}兩個值都欠,不代表該頁接續同一張表。
表編號不是文字{"previous":{"id":7,"headings":["code"]},"continues":7,"rows":[["P8"]]}{"error":"continuation-identity"}數字相同也不能代替所需文字編號。
續行少一格{"previous":{"id":"T1","headings":["code","quantity"]},"continues":"T1","rows":[["P8"]]}{"error":"row-width"}一格未能提供第二欄值,不能自行補零。
下載可編輯 CSV ↓

逐步處理 missing-cell 個案,須得到甚麼結果?原因是甚麼?

查看參考答案

{"error":"row-width"}。一格未能提供第二欄值,不能自行補零。

執行完整本機驗算

另存為 .mjs,以 Node.js 執行;只驗提供的個案
import assert from 'node:assert/strict';

function evaluate(x){
 if(!x.previous||typeof x.previous.id!=='string'||!x.previous.id.trim()||typeof x.continues!=='string'||!x.continues.trim())return {error:'continuation-identity'};
 if(new Set(x.previous.headings).size!==x.previous.headings.length)return {error:'heading-identity'};
 if(x.previous.id!==x.continues)return {error:'continuation'};
 if(x.rows.some(r=>r.length!==x.previous.headings.length))return {error:'row-width'};
 return {rows:x.rows.map(cells=>Object.fromEntries(x.previous.headings.map((h,i)=>[h,cells[i]])))};
}

const cases = [
  {
    "id": "matched-continuation",
    "input": {
      "previous": {
        "id": "T1",
        "headings": [
          "code",
          "quantity"
        ]
      },
      "continues": "T1",
      "rows": [
        [
          "P8",
          6
        ]
      ]
    },
    "expected": {
      "rows": [
        {
          "code": "P8",
          "quantity": 6
        }
      ]
    }
  },
  {
    "id": "different-table",
    "input": {
      "previous": {
        "id": "T1",
        "headings": [
          "code",
          "quantity"
        ]
      },
      "continues": "T2",
      "rows": [
        [
          "P8",
          6
        ]
      ]
    },
    "expected": {
      "error": "continuation"
    }
  },
  {
    "id": "both-identities-absent",
    "input": {
      "previous": {
        "headings": [
          "code"
        ]
      },
      "rows": [
        [
          "P8"
        ]
      ]
    },
    "expected": {
      "error": "continuation-identity"
    }
  },
  {
    "id": "nontext-table-identity",
    "input": {
      "previous": {
        "id": 7,
        "headings": [
          "code"
        ]
      },
      "continues": 7,
      "rows": [
        [
          "P8"
        ]
      ]
    },
    "expected": {
      "error": "continuation-identity"
    }
  },
  {
    "id": "missing-cell",
    "input": {
      "previous": {
        "id": "T1",
        "headings": [
          "code",
          "quantity"
        ]
      },
      "continues": "T1",
      "rows": [
        [
          "P8"
        ]
      ]
    },
    "expected": {
      "error": "row-width"
    }
  }
];
for (const c of cases) assert.deepEqual(evaluate(c.input), c.expected, c.id);
console.log(JSON.stringify({checked:cases.length,caseIds:cases.map(c=>c.id)}));
下載可執行例子 ↓

讀取行連同表頭頁及續表依據保存。本例先有頁面關係才沿用表頭,不按相鄰頁自行估。

相關資源