Files
wekan/server/lib/plannerWorkbook.js
T
Lauri Ojansivu a2257442a8 Teamwork and Planner workbooks are written a page of cards at a time with ExcelJS's streaming writer.
The workbook exports were built whole, as an ExcelJS workbook in memory.
Teamwork's and Planner's are now written to the response by
ExcelJS.stream.xlsx.WorkbookWriter, each row committed as it comes
(server/lib/renderExternalExport.js STREAMED_WORKBOOKS), every page's rows
through the export boundary as the whole workbook's are.

Teamwork's rows are a task tree: its order and each card's depth are
planned over the whole board from the light first pass (teamworkPlan,
teamworkPageOrder), the pages come in that order, and each page writes its
own rows. Planner's rows stand on their own: its head on the first page,
each card's row after it.

tests/workbookStream.test.cjs checks every page size against the whole
sheet, and that the streamed workbook reads back - and is read by
Teamwork's own parser - as the whole one; the import/export audit (173)
and the admin-only custom field specs (12) pass.

Thanks to xet7 !
2026-11-20 11:21:50 +02:00

57 lines
2.5 KiB
JavaScript

// Opens and writes Microsoft Planner's Excel workbook; what its rows mean is
// models/lib/plannerFormat.js. Server only: ExcelJS is a server dependency.
// The rows of the "Tasks" sheet (the first sheet when there is no "Tasks"), as
// arrays of raw cell values, at their sheet positions: rows[0] is row 1 and an
// empty row is [], so a reported row number is the one Excel shows. Unlike
// the generic Excel import, a date cell stays a Date, so its day is not lost
// to String(date)'s local time.
async function readPlannerWorkbook(excelBase64) {
if (typeof excelBase64 !== 'string' || !excelBase64) throw new Error('Planner workbook is empty');
const ExcelJS = require('@wekanteam/exceljs');
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.load(Buffer.from(excelBase64, 'base64'));
const sheet = workbook.getWorksheet('Tasks') || workbook.worksheets[0];
const rows = [];
if (sheet) {
sheet.eachRow((row, number) => {
while (rows.length < number - 1) rows.push([]);
rows.push(row.values.slice(1));
});
}
return rows;
}
// A .xlsx Buffer from formatPlannerRows()'s { sheet, rows }.
async function writePlannerWorkbook({ sheet, rows }) {
const ExcelJS = require('@wekanteam/exceljs');
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet(sheet || 'Tasks');
(rows || []).forEach(cells => worksheet.addRow(cells));
const header = (rows || []).findIndex(cells => Array.isArray(cells) && cells.includes('Task Name'));
if (header !== -1) worksheet.getRow(header + 1).font = { bold: true };
return Buffer.from(await workbook.xlsx.writeBuffer());
}
// The same workbook written to `stream` a page at a time, from an async
// iterable of formatPlannerRows()'s pages ({ sheet, rows }, the head on the
// first): ExcelJS's streaming writer commits each row as it comes.
async function streamPlannerWorkbook(stream, pages) {
const ExcelJS = require('@wekanteam/exceljs');
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ stream, useStyles: true });
let worksheet = null;
for await (const { sheet, rows } of pages) {
if (!worksheet) worksheet = workbook.addWorksheet(sheet || 'Tasks');
for (const cells of rows || []) {
const row = worksheet.addRow(cells);
if (Array.isArray(cells) && cells.includes('Task Name')) row.font = { bold: true };
row.commit();
}
}
if (!worksheet) workbook.addWorksheet('Tasks');
else await worksheet.commit();
await workbook.commit();
}
module.exports = { readPlannerWorkbook, writePlannerWorkbook, streamPlannerWorkbook };