mirror of
https://github.com/wekan/wekan.git
synced 2026-11-20 15:45:01 -05:00
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 !
80 lines
3.4 KiB
JavaScript
80 lines
3.4 KiB
JavaScript
// Opens and writes Teamwork.com's Excel task import template; what its rows
|
|
// mean is models/lib/teamworkFormat.js. Server only: ExcelJS is a server
|
|
// dependency.
|
|
|
|
// The first sheet as { name, rows }: rows at their sheet positions (rows[0] is
|
|
// row 1, an empty row is []), so a reported row number is the one Excel
|
|
// shows, and a date cell stays a Date, so its day is not lost to
|
|
// String(date)'s local time. Teamwork also accepts the older .xls; ExcelJS
|
|
// reads only .xlsx, so an .xls file is refused with what to do instead.
|
|
async function readTeamworkWorkbook(excelBase64) {
|
|
if (typeof excelBase64 !== 'string' || !excelBase64) throw new Error('Teamwork.com workbook is empty');
|
|
const buffer = Buffer.from(excelBase64, 'base64');
|
|
if (buffer.length >= 4 && buffer.readUInt32BE(0) === 0xd0cf11e0) {
|
|
throw new Error('Teamwork.com workbook is an .xls file; save it as .xlsx and import that');
|
|
}
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.Workbook();
|
|
await workbook.xlsx.load(buffer);
|
|
const sheet = 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 { name: sheet ? sheet.name : '', rows };
|
|
}
|
|
|
|
// A .xlsx Buffer from formatTeamworkSheet()'s { name, rows }.
|
|
async function writeTeamworkWorkbook({ name, rows }) {
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.Workbook();
|
|
const title = String(name || 'Tasks').replace(/[\\/?*[\]:]/g, ' ').trim().slice(0, 31) || 'Tasks';
|
|
const worksheet = workbook.addWorksheet(title);
|
|
(rows || []).forEach(cells => worksheet.addRow(cells));
|
|
if ((rows || []).length) {
|
|
worksheet.getRow(1).font = { bold: true };
|
|
// Start date and Due date, as dates Excel shows in the reader's format.
|
|
const header = rows[0] || [];
|
|
for (const columnName of ['Start date', 'Due date']) {
|
|
const index = header.indexOf(columnName);
|
|
if (index !== -1) worksheet.getColumn(index + 1).numFmt = 'yyyy-mm-dd';
|
|
}
|
|
}
|
|
return Buffer.from(await workbook.xlsx.writeBuffer());
|
|
}
|
|
|
|
// The same workbook written to `stream` a page at a time, from an async
|
|
// iterable of formatTeamworkSheet()'s pages ({ name, rows }, the header on
|
|
// the first): ExcelJS's streaming writer commits each row as it comes, so
|
|
// only one page of rows is in memory.
|
|
async function streamTeamworkWorkbook(stream, pages) {
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ stream, useStyles: true });
|
|
let worksheet = null;
|
|
for await (const { name, rows } of pages) {
|
|
for (const cells of rows || []) {
|
|
if (!worksheet) {
|
|
const title = String(name || 'Tasks').replace(/[\\/?*[\]:]/g, ' ').trim().slice(0, 31) || 'Tasks';
|
|
worksheet = workbook.addWorksheet(title);
|
|
for (const columnName of ['Start date', 'Due date']) {
|
|
const index = cells.indexOf(columnName);
|
|
if (index !== -1) worksheet.getColumn(index + 1).numFmt = 'yyyy-mm-dd';
|
|
}
|
|
const header = worksheet.addRow(cells);
|
|
header.font = { bold: true };
|
|
header.commit();
|
|
continue;
|
|
}
|
|
worksheet.addRow(cells).commit();
|
|
}
|
|
}
|
|
if (!worksheet) workbook.addWorksheet('Tasks');
|
|
else await worksheet.commit();
|
|
await workbook.commit();
|
|
}
|
|
|
|
module.exports = { readTeamworkWorkbook, writeTeamworkWorkbook, streamTeamworkWorkbook };
|