Export to spreadsheet

This commit is contained in:
2022-01-04 01:52:32 +02:00
parent 4458d22576
commit c41dfcb74f
4 changed files with 490 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
const express = require('express')
const XL = require('excel4node');
module.exports = function (options) {
const db = options.db;
@@ -355,5 +356,145 @@ module.exports = function (options) {
})
})
router.route('/:id/spreadsheet')
.post(function(request, response, next) {
db.Project.findByPk(request.params.id, {
}).then(async (project) => {
if (project) {
db.TranslationEntryVersion.findAll({
include: [
// TODO: require translationEntryVersions in ProjectEntryVersion
{
association: db.TranslationEntryVersion.ProjectTranslationEntryVersions,
where: {
projectId: request.params.id
},
required: true
},
{
association: db.TranslationEntryVersion.EntryVersion,
include: [
{
association: db.EntryVersion.Entry,
include: [
{
association: db.Entry.Dialect
}
]
},
{
association: db.EntryVersion.Chapters,
include: [
{
association: db.Chapter.Chunks,
include: [
{
association: db.Chunk.Translation
}
]
}
]
}
]
},
{
association: db.TranslationEntryVersion.Dialect
}
]
}).then(translationEntryVersions => {
console.log("Translation Entry: ", translationEntryVersions.length)
const workbook = new XL.Workbook();
const cellWidth = 45
const cellHeight = 25
translationEntryVersions.forEach((translationEntryVersion, index) => {
const worksheet_name = `${index+1}-${translationEntryVersion.dialect.countryId}-${translationEntryVersion.dialect.languageId} - ${translationEntryVersion.entryVersion.entry.name}`.substring(0, 30);
// Don't have [ ] * ? : / \
const worksheet = workbook.addWorksheet(worksheet_name.replace(/[`*?:'\[\]\\\/]/gi, ""))
worksheet.cell(2,2,2,3,true)
.string(translationEntryVersion.entryVersion.entry.name)
.style({
font: {
bold: true,
},
alignment: {
wrapText: true
}
})
worksheet.cell(4,2)
.string(translationEntryVersion.entryVersion.entry.dialect.name)
.style({
font: {
bold: true,
},
alignment: {
wrapText: true
}
})
worksheet.cell(4,3)
.string(translationEntryVersion.dialect.name)
.style({
font: {
bold: true,
},
alignment: {
wrapText: true
}
})
var cellIndex = 6
for (const i in translationEntryVersion.entryVersion.chapters) {
const chapter = translationEntryVersion.entryVersion.chapters[i]
const chunks = chapter.chunks.sort((a,b) => a.index - b.index)
for (const j in chunks) {
const chunk = chunks[j]
worksheet.cell(cellIndex,2)
.string(chunk.text)
.style({
alignment: {
wrapText: true,
vertical: 'top'
}
})
worksheet.cell(cellIndex,3)
.string(chunk.translation?.text ?? "")
.style({
alignment: {
wrapText: true,
vertical: 'top'
}
})
const textLength = chunk.text.length
worksheet.row(cellIndex).setHeight(
Math.ceil(Math.max(textLength/cellWidth, 1)*cellHeight)
)
cellIndex++
}
cellIndex++
}
// Format
worksheet.column(2).setWidth(cellWidth);
worksheet.column(3).setWidth(cellWidth);
})
workbook.write(`${project.name}.xlsx`, response);
}).catch(error => {
next(error)
})
} else {
next()
}
})
})
return router;
};

View File

@@ -10,6 +10,11 @@ block content
.row
.col.s12
a.btn.black(href=`/projects/${project.id}/campaign`) funding campaign
.row
.col.s12
form(action=`/projects/${project.id}/spreadsheet`, method="post")
button.btn.black(type="submit") export to spreadsheets
.divider
h2 Entries
each projectEntryVersion in project.projectEntryVersions