mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Every workbook export is now written by ExcelJS's streaming workbook writer, a row at a time, instead of a whole workbook in memory. - monday.com and Businessmap: one sheet each; the columns any card fills (monday.com's custom fields; Businessmap's optional columns, comment columns and other fields) and Businessmap's parent Card IDs are found over the whole board from the light first pass, the header written on the first page (server/lib/streamSheetWorkbook.js, shared). - Wrike: the lanes' order, the folder rows and every row's Key are planned over the whole board (wrikePlan, wrikePageOrder); the pages come in that order and a lane's folder row goes before its first card. The date columns are date cells, as in the whole workbook. tests/workbookStream.test.cjs checks every page size against the whole sheet and reads each streamed workbook back as the whole one; the import/export audit (173) and the admin-only custom field specs (12) pass. Thanks to xet7 !
46 lines
2.2 KiB
JavaScript
46 lines
2.2 KiB
JavaScript
// Opens and writes Businessmap (Kanbanize) Excel workbooks; what their rows
|
|
// mean is models/lib/businessmapFormat.js. Server only: ExcelJS is a server
|
|
// dependency.
|
|
|
|
// Every sheet, in order, as { name, rows }: the card sheet, then any "Links"
|
|
// or "Subtasks" tab an export added. Rows keep their sheet positions
|
|
// (rows[0] is row 1, an empty row is []), and a date cell stays a Date, so
|
|
// its day is not lost to String(date)'s local time.
|
|
async function readBusinessmapWorkbook(excelBase64) {
|
|
if (typeof excelBase64 !== 'string' || !excelBase64) throw new Error('Businessmap workbook is empty');
|
|
const buffer = Buffer.from(excelBase64, 'base64');
|
|
// Businessmap also reads the old binary .xls; ExcelJS reads .xlsx only.
|
|
if (buffer.length >= 4 && buffer.readUInt32BE(0) === 0xd0cf11e0) {
|
|
throw new Error('Businessmap workbook is an .xls file; open it in a spreadsheet program and save it as .xlsx');
|
|
}
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.Workbook();
|
|
await workbook.xlsx.load(buffer);
|
|
return workbook.worksheets.map(sheet => {
|
|
const rows = [];
|
|
sheet.eachRow((row, number) => {
|
|
while (rows.length < number - 1) rows.push([]);
|
|
rows.push(Array.from(row.values.slice(1), cell => (cell === undefined ? null : cell)));
|
|
});
|
|
return { name: sheet.name, rows };
|
|
});
|
|
}
|
|
|
|
// A .xlsx Buffer from formatBusinessmapSheets()'s { sheets }.
|
|
async function writeBusinessmapWorkbook({ sheets }) {
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.Workbook();
|
|
(sheets || []).forEach((sheet, index) => {
|
|
const worksheet = workbook.addWorksheet(String(sheet.name || `sheet${index + 1}`).replace(/[\\/?*[\]:]/g, ' ').slice(0, 31) || `sheet${index + 1}`);
|
|
(sheet.rows || []).forEach(cells => worksheet.addRow(cells));
|
|
if ((sheet.rows || []).length) worksheet.getRow(1).font = { bold: true };
|
|
});
|
|
return Buffer.from(await workbook.xlsx.writeBuffer());
|
|
}
|
|
|
|
// The same workbook written to `stream` a page at a time
|
|
// (server/lib/streamSheetWorkbook.js).
|
|
const streamBusinessmapWorkbook = (stream, pages) => require('./streamSheetWorkbook').streamSheetWorkbook(stream, pages);
|
|
|
|
module.exports = { readBusinessmapWorkbook, writeBusinessmapWorkbook, streamBusinessmapWorkbook };
|