先核來源與已訂規則
本例合併範圍是第一欄、第二至三行(包括兩端),左上角 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}]} |
比較兩條實際路徑
更正填第三行,第四行留 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}] |
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}] |
North 應否帶到第四行?
查看參考答案
不應,第四行超出第二至三行合併範圍,第一欄只有第二、三行是 North。