Files
order-notification-automation/web/modules/storage/preview-office.js
T

33 lines
3.5 KiB
JavaScript

/* Adapted from OpenList Frontend v4.2.7 doc.tsx and xls.tsx (MIT).
* Copyright (c) 2025 The OpenList Projects Contributors.
* Changes: local assets, read-only sandbox, bounded worksheet rendering,
* plain DOM integration and cancellation handling. See licenses/OpenList-Frontend-MIT.txt.
*/
function script(src){return new Promise((resolve,reject)=>{const node=document.createElement('script');node.src=src;node.onload=resolve;node.onerror=()=>reject(new Error('预览组件加载失败。'));document.head.append(node);});}
export async function renderWord(bytes,container){
await script('/vendor/jszip/jszip.min.js');await script('/vendor/docx/docx-preview.js');
const toolbar=document.createElement('div');toolbar.className='preview-toolbar';const label=document.createElement('span');label.textContent='Word 文档';toolbar.append(label);
const pages=document.createElement('div');pages.className='word-pages';container.append(toolbar,pages);
await window.docx.renderAsync(bytes,pages,undefined,{className:'word-document',inWrapper:true,ignoreWidth:false,ignoreHeight:false,breakPages:true,ignoreLastRenderedPageBreak:true,renderHeaders:true,renderFooters:true,renderFootnotes:true,renderEndnotes:true,renderAltChunks:false,useBase64URL:false});
}
export async function renderExcel(bytes,container){
await script('/vendor/exceljs/exceljs.min.js');const book=new window.ExcelJS.Workbook();await book.xlsx.load(bytes);
const toolbar=document.createElement('div');toolbar.className='preview-toolbar';const label=document.createElement('label');label.textContent='工作表';label.htmlFor='preview-sheet';const select=document.createElement('select');select.id='preview-sheet';
book.worksheets.forEach((sheet,index)=>{const option=document.createElement('option');option.value=String(index);option.textContent=sheet.name;select.append(option);});
toolbar.append(label,select);const note=document.createElement('p');note.className='preview-note';const viewport=document.createElement('div');viewport.className='sheet-viewport';container.append(toolbar,note,viewport);
function showSheet(){const sheet=book.worksheets[Number(select.value)];viewport.replaceChildren();if(!sheet){note.textContent='此文件没有可显示的工作表。';return;}
const rows=Math.min(sheet.rowCount,500),cols=Math.min(sheet.columnCount,50);note.textContent=(sheet.rowCount>500||sheet.columnCount>50?'仅预览前 500 行、50 列,可下载原文件查看完整内容。':`${sheet.name} · ${sheet.rowCount} 行、${sheet.columnCount} 列`)+'。数值按原始值展示,未应用单元格数字格式;公式显示文件中保存的结果。';
const table=document.createElement('table');table.className='preview-sheet';const body=document.createElement('tbody');
for(let row=1;row<=rows;row++){const tr=document.createElement('tr'),number=document.createElement('th');number.scope='row';number.textContent=String(row);tr.append(number);
for(let col=1;col<=cols;col++){const cell=sheet.getCell(row,col),td=document.createElement('td');td.textContent=cell.text;
if(cell.font?.bold)td.classList.add('cell-bold');if(cell.font?.italic)td.classList.add('cell-italic');
const fill=cell.fill?.fgColor?.argb,color=cell.font?.color?.argb;if(/^[A-Fa-f0-9]{8}$/.test(fill||''))td.style.backgroundColor='#'+fill.slice(2);if(/^[A-Fa-f0-9]{8}$/.test(color||''))td.style.color='#'+color.slice(2);
tr.append(td);
}body.append(tr);
}table.append(body);viewport.append(table);
}
select.addEventListener('change',showSheet);showSheet();
}