19 lines
1.3 KiB
JavaScript
19 lines
1.3 KiB
JavaScript
// Offline XLSX fixture used only by download-center contract tests.
|
|
export function exportWorkbook(headers, values = []) {
|
|
const escape = value => String(value ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
const rows = [headers, values].map((row, index) => `<row r="${index + 1}">${row.map((value, col) => {
|
|
const letter = String.fromCharCode(65 + col)
|
|
return `<c r="${letter}${index + 1}" t="inlineStr"><is><t>${escape(value)}</t></is></c>`
|
|
}).join('')}</row>`).join('')
|
|
const name = Buffer.from('xl/worksheets/sheet1.xml')
|
|
const body = Buffer.from(`<worksheet><sheetData>${rows}</sheetData></worksheet>`)
|
|
const local = Buffer.alloc(30)
|
|
local.writeUInt32LE(0x04034b50); local.writeUInt32LE(body.length, 18); local.writeUInt32LE(body.length, 22); local.writeUInt16LE(name.length, 26)
|
|
const central = Buffer.alloc(46)
|
|
central.writeUInt32LE(0x02014b50); central.writeUInt32LE(body.length, 20); central.writeUInt32LE(body.length, 24); central.writeUInt16LE(name.length, 28)
|
|
const end = Buffer.alloc(22)
|
|
end.writeUInt32LE(0x06054b50); end.writeUInt16LE(1, 8); end.writeUInt16LE(1, 10)
|
|
end.writeUInt32LE(central.length + name.length, 12); end.writeUInt32LE(local.length + name.length + body.length, 16)
|
|
return Buffer.concat([local, name, body, central, name, end])
|
|
}
|