跳至內容
Astera Technology

讀取合併表頭下的完整欄位名稱

同名子欄帶回完整父表頭路徑,避免兩個數值互相覆寫。

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

訂明本例驗收任務

假設出貨表的 requested 及 delivered 父表頭都各有 units 子欄。路徑與同一行數值用對齊清單提供,輸出須帶完整兩層路徑,恰好兩個非空文字名稱;完整路徑重複或兩清單長度不同便拒絕該行,零仍是已觀察值。

  1. 父表頭與子欄一起讀
  2. 每個不同完整路徑核對一值
  3. 輸出完整路徑與值

路徑不清楚或缺失,須覆核版面。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
不同父欄下同名子欄{"paths":[["requested","units"],["delivered","units"]],"values":[8,5]}{"fields":[{"path":["requested","units"],"value":8},{"path":["delivered","units"],"value":5}]}要求八與已交五保留不同父路徑。
已交零有記錄{"paths":[["requested","units"],["delivered","units"]],"values":[4,0]}{"fields":[{"path":["requested","units"],"value":4},{"path":["delivered","units"],"value":0}]}零保留在 delivered.units,不列作欠欄。
子欄欠父表頭{"paths":[["units"]],"values":[8]}{"error":"header-association"}只有 units,不是所需父表頭與子欄兩層關係。
完整路徑重複{"paths":[["requested","units"],["requested","units"]],"values":[8,5]}{"error":"header-association"}兩值同指一個完整欄位,不可靜默覆寫。
下載可編輯 CSV ↓

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

查看參考答案

{"error":"header-association"}。兩值同指一個完整欄位,不可靜默覆寫。

執行完整本機驗算

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

function evaluate(x){
 if(x.paths.some(path=>!Array.isArray(path)||path.length!==2||path.some(part=>typeof part!=='string'||!part.trim())))return {error:'header-association'};
 const keys=x.paths.map(p=>JSON.stringify(p));
 if(x.paths.length!==x.values.length||new Set(keys).size!==keys.length)return {error:'header-association'};
 return {fields:x.paths.map((path,i)=>({path,value:x.values[i]}))};
}

const cases = [
  {
    "id": "same-leaf",
    "input": {
      "paths": [
        [
          "requested",
          "units"
        ],
        [
          "delivered",
          "units"
        ]
      ],
      "values": [
        8,
        5
      ]
    },
    "expected": {
      "fields": [
        {
          "path": [
            "requested",
            "units"
          ],
          "value": 8
        },
        {
          "path": [
            "delivered",
            "units"
          ],
          "value": 5
        }
      ]
    }
  },
  {
    "id": "observed-zero",
    "input": {
      "paths": [
        [
          "requested",
          "units"
        ],
        [
          "delivered",
          "units"
        ]
      ],
      "values": [
        4,
        0
      ]
    },
    "expected": {
      "fields": [
        {
          "path": [
            "requested",
            "units"
          ],
          "value": 4
        },
        {
          "path": [
            "delivered",
            "units"
          ],
          "value": 0
        }
      ]
    }
  },
  {
    "id": "leaf-without-parent",
    "input": {
      "paths": [
        [
          "units"
        ]
      ],
      "values": [
        8
      ]
    },
    "expected": {
      "error": "header-association"
    }
  },
  {
    "id": "duplicate-path",
    "input": {
      "paths": [
        [
          "requested",
          "units"
        ],
        [
          "requested",
          "units"
        ]
      ],
      "values": [
        8,
        5
      ]
    },
    "expected": {
      "error": "header-association"
    }
  }
];
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)}));
下載可執行例子 ↓

原版面關係跟讀取行一併保留。個案測合併表頭;未執行 OCR,也沒有核實實物存量。

相關資源

流程自動化服務 →