From f5b936212c2bea84dc4e9f56e6dbd7e33733a80e Mon Sep 17 00:00:00 2001 From: kngako Date: Wed, 22 Jun 2022 04:16:01 +0200 Subject: [PATCH] Ability to export sheets from tracker --- server/router/projects/index.js | 126 +++++++++++++++++++++++++++++-- server/server.js | 1 + server/views/project-tracker.pug | 8 ++ 3 files changed, 130 insertions(+), 5 deletions(-) diff --git a/server/router/projects/index.js b/server/router/projects/index.js index 4023841..6bb24d0 100644 --- a/server/router/projects/index.js +++ b/server/router/projects/index.js @@ -1241,11 +1241,127 @@ module.exports = function (options) { } ] }).then(translationChapters => { - response.display("project-tracker", { - user: request.user, - project: project, - translationChapters: translationChapters - }) + if (request.query.sheets) { + // Return sheets + const workbook = new XL.Workbook(); + const cellWidth = 45 + const cellHeight = 25 + + translationChapters.sort((a, b) => a.index - b.index).forEach((translationChapter, index) => { + const worksheet_name = `${index+1}-${translationChapter.translationArtifactVersion.dialect.countryId}-${translationChapter.translationArtifactVersion.dialect.languageId} - ${translationChapter.translationArtifactVersion.artifactVersion.artifact.name}`.substring(0, 30); + + // Don't have [ ] * ? : / \ + const worksheet = workbook.addWorksheet(worksheet_name.replace(/[`*?:'\[\]\\\/]/gi, "")) + + worksheet.cell(2,2,2,3,true) + .string(translationChapter.translationArtifactVersion.artifactVersion.artifact.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + + worksheet.cell(4,2) + .string(translationChapter.translationArtifactVersion.artifactVersion.artifact.dialect.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + + worksheet.cell(4,3) + .string(translationChapter.translationArtifactVersion.dialect.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + // Meta data for the translation and artifact + worksheet.cell(4,4) + .string(translationChapter.translationArtifactVersion.artifactVersion.id) + worksheet.cell(4,5) + .string(translationChapter.translationArtifactVersion.id) + + var cellIndex = 6 + const translationChunks = translationChapter.translationChunks.sort((a,b) => a.index - b.index) + for (const j in translationChunks) { + const translationChunk = translationChunks[j] + + worksheet.cell(cellIndex,2) + .string(translationChunk.text) + .style({ + alignment: { + wrapText: true, + vertical: 'justify' + } + }) + + const translationCell = worksheet.cell(cellIndex,3) + .style({ + alignment: { + wrapText: true, + vertical: 'justify' + } + }) + + if (translationChunk.translation) { + translationCell.string(translationChunk.translation.text) + } + + const textLength = translationChunk.text.length + worksheet.row(cellIndex).setHeight( + Math.ceil(Math.max(textLength/cellWidth, 1)*cellHeight) + ) + + // Meta data... + // ID for translationChunk + worksheet.cell(cellIndex,4) + .string(translationChunk.id) + // ID for translation + worksheet.cell(cellIndex,5) + .string(translationChunk.translation?.id ?? "") + + cellIndex++ + } + + + // Format Columns + worksheet.column(2).setWidth(cellWidth); + worksheet.column(3).setWidth(cellWidth); + worksheet.column(4).hide(); + worksheet.column(5).hide(); + + // Format Rows... + worksheet.row(5).freeze() + worksheet.row(1).setHeight(5) + worksheet.row(3).setHeight(5) + worksheet.row(5).setHeight(5) + }) + if (translationChapters.length > 0) { + workbook.write(`${project.name}.xlsx`, response); + } else { + response.redirect(`/projects/${request.params.id}`) + } + } else { + response.display("project-tracker", { + user: request.user, + project: project, + translationChapters: translationChapters + }) + } + }).catch(error => { next(error) }) diff --git a/server/server.js b/server/server.js index ade8afb..131e700 100644 --- a/server/server.js +++ b/server/server.js @@ -46,6 +46,7 @@ module.exports = function () { response.display = (view, options) => { options.domain = config.get("server.domain"); options.urlEndpoint = request.originalUrl; + options.query = request.query; // Set a default pageTitle... options.pageTitle = options.pageTitle ? options.pageTitle : "Mantra"; options.errorMessage = (error) => { diff --git a/server/views/project-tracker.pug b/server/views/project-tracker.pug index 7e1e8b9..41c94c2 100644 --- a/server/views/project-tracker.pug +++ b/server/views/project-tracker.pug @@ -22,6 +22,14 @@ block content .col.s12 a.btn.black(href=`/projects/${project.id}/tracker/trail`) audit trail + .row + .col.s12 + form(action=urlEndpoint, method="get") + input(type="hidden", name="sheets", value="true") + each val, key in query + if key != "sheets" + input(type="hidden", name=key, value=val) + button.btn.black(type="submit") export to spreadsheets .divider table.highlight