跳至內容
Astera Technology

文件沒有明細時,重複區塊怎樣處理?

沒有明細便移除重複區塊,獨立必須的頁尾仍保留。

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

訂判斷及所需輸入

本例文件的頁尾必須有,與明細數目無關。零明細時整個重複區塊不出現。錯誤後備會加一行 Empty,已改文件結構。一行及三行輸入則各自只呈現一、三行。

已填本地測試工作表
情況輸入 JSON
原例測試{"lines":[]}
下載可編輯 CSV ↓

跑所附比較

例子真正建立獨立頁面並查 DOM。同時核區塊有沒有、明細行數及頁尾文字;資料陣列空白,本身未證明生成文件正確。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"repeatPresent":true,"lineCount":1,"footer":"Required footer"}
所需路徑{"repeatPresent":false,"lineCount":0,"footer":"Required footer"}
下載可編輯 CSV ↓
Node24.14.1 程序;須 Playwright1.63 Chromium
import assert from 'node:assert/strict';
import {createRequire} from 'node:module';
const htmlEscape=s=>String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
async function withPage(action,options={}){
  const require=createRequire(import.meta.url);const {chromium}=require('playwright');
  const browser=await chromium.launch({headless:true});
  try{const context=await browser.newContext(options);const page=await context.newPage();
    const requests=[];await page.route('**/*',route=>{requests.push(route.request().url());return route.abort();});
    return await action(page,context,requests);
  }finally{await browser.close();}
}

const run=async function run(i,corrected=true){
    return withPage(async page=>{const lines=i.lines.length?i.lines:corrected?[]:['Empty'];
      await page.setContent((lines.length?'<section id="repeat">'+lines.map(x=>'<p class="line">'+htmlEscape(x)+'</p>').join('')+'</section>':'')+'<footer>Required footer</footer>');
      return page.evaluate(()=>({repeatPresent:!!document.querySelector('#repeat'),lineCount:document.querySelectorAll('.line').length,footer:document.querySelector('footer').textContent}));});
  };
const cases=[{"input":{"lines":[]},"corrected":false,"expected":{"repeatPresent":true,"lineCount":1,"footer":"Required footer"}},{"input":{"lines":[]},"corrected":true,"expected":{"repeatPresent":false,"lineCount":0,"footer":"Required footer"}},{"input":{"lines":["A"]},"corrected":true,"expected":{"repeatPresent":true,"lineCount":1,"footer":"Required footer"}},{"input":{"lines":["A","B","C"]},"corrected":true,"expected":{"repeatPresent":true,"lineCount":3,"footer":"Required footer"}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
下載可執行例子 ↓

按邊界依據作判斷

有內容的邊界保區塊及同一頁尾,不會為了讓範本可呈現而插空業務行。

  1. 分開重複及固定區域
  2. 按所附明細呈現
  3. 核生成後實際結構

缺明細不能補虛構佔位行。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
只有一行真明細{"lines":["A"]}{"repeatPresent":true,"lineCount":1,"footer":"Required footer"}
有三行真明細{"lines":["A","B","C"]}{"repeatPresent":true,"lineCount":3,"footer":"Required footer"}
下載可編輯 CSV ↓

原例明細空白,文件應有甚麼結構?

查看參考答案

沒有重複區塊、零明細行,但 Required footer 仍在。頁尾不是隨行數決定。

相關資源