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 !
81 lines
3.7 KiB
JavaScript
81 lines
3.7 KiB
JavaScript
// Opens and writes Wrike's Excel import template; what its rows mean is
|
|
// models/lib/wrikeFormat.js. Server only: ExcelJS is a server dependency.
|
|
|
|
const DATE_COLUMNS = ['start date', 'end date', 'start date constraint'];
|
|
|
|
// 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. A date
|
|
// cell stays a Date, so its day is not lost to String(date)'s local time.
|
|
async function readWrikeWorkbook(excelBase64) {
|
|
if (typeof excelBase64 !== 'string' || !excelBase64) throw new Error('Wrike 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 formatWrikeRows()'s { sheet, rows }. The template's
|
|
// dates are real date cells, as in Wrike's sample, so the YYYY-MM-DD text the
|
|
// formatter writes in the date columns becomes a date at 00:00 UTC.
|
|
async function writeWrikeWorkbook({ sheet, rows }) {
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.Workbook();
|
|
const worksheet = workbook.addWorksheet(sheet || 'Tasks');
|
|
const header = Array.isArray(rows && rows[0]) ? rows[0] : [];
|
|
const dateAt = header.map((title, index) => (DATE_COLUMNS.includes(String(title).trim().toLowerCase()) ? index : -1))
|
|
.filter(index => index !== -1);
|
|
(rows || []).forEach((cells, rowIndex) => {
|
|
const values = (cells || []).map((value, index) => {
|
|
const iso = rowIndex > 0 && dateAt.includes(index) && /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(value));
|
|
return iso ? new Date(Date.UTC(+iso[1], +iso[2] - 1, +iso[3])) : value;
|
|
});
|
|
worksheet.addRow(values);
|
|
});
|
|
dateAt.forEach(index => { worksheet.getColumn(index + 1).numFmt = 'yyyy-mm-dd'; });
|
|
if (header.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, from an async
|
|
// iterable of formatWrikeRows()'s pages ({ sheet, rows }, the header on the
|
|
// first): ExcelJS's streaming writer commits each row as it comes, the date
|
|
// columns made date cells as above.
|
|
async function streamWrikeWorkbook(stream, pages) {
|
|
const ExcelJS = require('@wekanteam/exceljs');
|
|
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ stream, useStyles: true });
|
|
let worksheet = null;
|
|
let dateAt = [];
|
|
for await (const { sheet, rows } of pages) {
|
|
for (const cells of rows || []) {
|
|
if (!worksheet) {
|
|
worksheet = workbook.addWorksheet(sheet || 'Tasks');
|
|
dateAt = (cells || []).map((title, index) => (DATE_COLUMNS.includes(String(title).trim().toLowerCase()) ? index : -1))
|
|
.filter(index => index !== -1);
|
|
dateAt.forEach(index => { worksheet.getColumn(index + 1).numFmt = 'yyyy-mm-dd'; });
|
|
const header = worksheet.addRow(cells);
|
|
header.font = { bold: true };
|
|
header.commit();
|
|
continue;
|
|
}
|
|
worksheet.addRow((cells || []).map((value, index) => {
|
|
const iso = dateAt.includes(index) && /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(value));
|
|
return iso ? new Date(Date.UTC(+iso[1], +iso[2] - 1, +iso[3])) : value;
|
|
})).commit();
|
|
}
|
|
}
|
|
if (!worksheet) workbook.addWorksheet('Tasks');
|
|
else await worksheet.commit();
|
|
await workbook.commit();
|
|
}
|
|
|
|
module.exports = { readWrikeWorkbook, writeWrikeWorkbook, streamWrikeWorkbook };
|