mirror of
https://github.com/wekan/wekan.git
synced 2026-11-26 14:45:11 -05:00
Wrike documents the columns of its Excel import, not of its export: the help article's key, parent task, title, status, priority, assigned to, start date, duration, end date, depends on, start date constraint and description, custom fields to the right of description, and the official sample excel_import_sample.xls, whose folders and projects are rows with a path as their Title (/Folder 2/Subfolder 1/) above their tasks. WeKan reads and writes that template, headers matched case-insensitively. Folder rows become swimlanes, Status the lists (a custom workflow's Custom Status wins over its group), Key the reference, Parent Task the parent card (a folder's Key puts the task in that folder), Assigned To "Name <email>" the owner and assignees, Start and End Date the start and due dates, Depends On "13FS" an is-blocked-by dependency, and Priority, Duration and every other column custom fields. A start date constraint, a folder or project's own status and dates, the workflow columns, the SS/FF/SF kind of a dependency, bad keys and dates, and the repeated rows of a task in several folders are reported. Export writes the template on a Tasks sheet with a folder row per swimlane, sequential Keys, date cells, the Wrike status group as Status and the list as the custom field WeKan list, which the import reads back. tests/wrikeFormat.test.cjs builds the sample's structure with ExcelJS and covers the negatives and the round trip; a Playwright case imports a template through the page. New instruction text is in English, pending Transifex. Thanks to xet7 !
48 lines
2.3 KiB
JavaScript
48 lines
2.3 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 ExcelJS = require('@wekanteam/exceljs');
|
|
|
|
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 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 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());
|
|
}
|
|
|
|
module.exports = { readWrikeWorkbook, writeWrikeWorkbook };
|