跳至內容
Astera Technology

合併格標題為何填到不相關後行?

按合併範圍的行、欄兩邊界填,不一路向下沿用。

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

先核來源與已訂規則

本例合併範圍是第一欄、第二至三行(包括兩端),左上角 North。只有範圍內未有獨立值的格沿錨點;範圍已核有效且不重疊,範圍外普通空格仍 null。

已填假設輸入
紀錄輸入 JSON
原例輸入{"column":1,"rows":[2,3,4],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[{"fromRow":2,"toRow":3,"fromCol":1,"toCol":1}]}
下載可編輯 CSV ↓

比較兩條實際路徑

更正填第三行,第四行留 null。判斷依行欄座標是否在範圍,不只是前面曾出現非空標題。

錯誤與更正路徑的輸出
路徑輸出 JSON
錯誤路徑[{"row":2,"value":"North"},{"row":3,"value":"North"},{"row":4,"value":"North"}]
更正路徑[{"row":2,"value":"North"},{"row":3,"value":"North"},{"row":4,"value":null}]
下載可編輯 CSV ↓
本地 Node.js 程序:22.23.3 或 24.14.1
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};

import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
    const output=[];let carried=null;
    for(const row of i.rows){
      const own=i.cells.find(c=>c.row===row&&c.col===i.column);
      if(own)carried=own.value;
      if(!corrected){output.push({row,value:carried});continue;}
      const merge=i.merges.find(m=>row>=m.fromRow&&row<=m.toRow&&i.column>=m.fromCol&&i.column<=m.toCol);
      const anchor=merge?i.cells.find(c=>c.row===merge.fromRow&&c.col===merge.fromCol):null;
      output.push({row,value:own?own.value:anchor?anchor.value:null});
    }return output;
  };
const cases=[{"label":"misleading base","input":{"column":1,"rows":[2,3,4],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[{"fromRow":2,"toRow":3,"fromCol":1,"toCol":1}]},"expected":[{"row":2,"value":"North"},{"row":3,"value":"North"},{"row":4,"value":"North"}],"corrected":false},{"label":"corrected base","input":{"column":1,"rows":[2,3,4],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[{"fromRow":2,"toRow":3,"fromCol":1,"toCol":1}]},"expected":[{"row":2,"value":"North"},{"row":3,"value":"North"},{"row":4,"value":null}],"corrected":true},{"label":"column outside merge","input":{"column":2,"rows":[2,3],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[{"fromRow":2,"toRow":3,"fromCol":1,"toCol":1}]},"expected":[{"row":2,"value":null},{"row":3,"value":null}],"corrected":true},{"label":"no merged range","input":{"column":1,"rows":[2,3],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[]},"expected":[{"row":2,"value":"North"},{"row":3,"value":null}],"corrected":true}];
for(const test of cases){
  const actual=run(test.input,test.corrected);
  assert.deepEqual(actual,test.expected,test.label);
  console.log(test.label,actual);
}
下載可執行例子 ↓

下次載入前核邊界

第二欄不在範圍,要留空;根本沒有合併時,第三行也不沿 North。

可編輯邊界紀錄及預期輸出
情況輸入 JSON預期輸出 JSON
欄座標在合併範圍外{"column":2,"rows":[2,3],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[{"fromRow":2,"toRow":3,"fromCol":1,"toCol":1}]}[{"row":2,"value":null},{"row":3,"value":null}]
沒有合併範圍{"column":1,"rows":[2,3],"cells":[{"row":2,"col":1,"value":"North"}],"merges":[]}[{"row":2,"value":"North"},{"row":3,"value":null}]
下載可編輯 CSV ↓

North 應否帶到第四行?

查看參考答案

不應,第四行超出第二至三行合併範圍,第一欄只有第二、三行是 North。

相關資源