Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
shortid: request.template.baseXlsxTemplate.shortid
}, request)
if (!docs.length) {
throw reporter.createError(`Unable to find xlsx template with shortid ${request.template.baseXlsxTemplate.shortid}`, {
statusCode: 404
})
}
xlsxTemplateBuf = docs[0].contentRaw
}
tableXlsxBuf = Buffer.concat(await toArrayAsync(response.stream))
const [templateWorkbook, tableWorkbook] = await Promise.all([
XlsxPopulate.fromDataAsync(xlsxTemplateBuf),
XlsxPopulate.fromDataAsync(tableXlsxBuf)
])
const sheetsInTableWorkbook = tableWorkbook.sheets()
sheetsInTableWorkbook.forEach((sheet) => {
let oldSheet
if (templateWorkbook.sheet(sheet.name()) != null) {
if (templateWorkbook.sheets().length === 1) {
oldSheet = `${sheet.name()}-old`
// the workbook can not have empty sheets so we need to rename the only sheet first and then delete it
templateWorkbook.sheet(sheet.name()).name(oldSheet)
} else {
templateWorkbook.deleteSheet(sheet.name())
}
}, request)
if (!docs.length) {
throw reporter.createError(`Unable to find xlsx template with shortid ${request.template.baseXlsxTemplate.shortid}`, {
statusCode: 404
})
}
xlsxTemplateBuf = docs[0].contentRaw
}
tableXlsxBuf = Buffer.concat(await toArrayAsync(response.stream))
const [templateWorkbook, tableWorkbook] = await Promise.all([
XlsxPopulate.fromDataAsync(xlsxTemplateBuf),
XlsxPopulate.fromDataAsync(tableXlsxBuf)
])
const sheetsInTableWorkbook = tableWorkbook.sheets()
sheetsInTableWorkbook.forEach((sheet) => {
let oldSheet
if (templateWorkbook.sheet(sheet.name()) != null) {
if (templateWorkbook.sheets().length === 1) {
oldSheet = `${sheet.name()}-old`
// the workbook can not have empty sheets so we need to rename the only sheet first and then delete it
templateWorkbook.sheet(sheet.name()).name(oldSheet)
} else {
templateWorkbook.deleteSheet(sheet.name())
}
}
const targetCell = newSheetInTemplateWorkbook.cell(cell.rowNumber(), cell.columnNumber())
// applying formula if some exists in the cell
if (cell.formula() != null && cell.formula() !== 'SHARED') {
targetCell.formula(cell.formula())
}
// copying number format style to be able to preserve date format or some ther custom format
if (cell.style('numberFormat') != null) {
targetCell.style('numberFormat', cell.style('numberFormat'))
}
const stylesToApply = {}
getXlsxStyleNames().forEach((styleName) => {
if (cell.style(styleName) != null) {
stylesToApply[styleName] = cell.style(styleName)
}
})
if (mergedAddress != null) {
const range = newSheetInTemplateWorkbook.range(mergedAddress).merged(true)
if (Object.keys(stylesToApply).length > 0) {
range.style(stylesToApply)
}
mergedCells[mergedAddress] = true
} else {
if (Object.keys(stylesToApply).length > 0) {
targetCell.style(stylesToApply)
const util = require('util')
const fs = require('fs')
const XlsxPopulate = require('html-to-xlsx').XlsxPopulate
const getXlsxStyleNames = require('html-to-xlsx').getXlsxStyleNames
const toArray = require('stream-to-array')
const writeFileAsync = util.promisify(fs.writeFile)
const toArrayAsync = util.promisify(toArray)
module.exports = async (reporter, request, response) => {
let xlsxTemplateBuf
let tableXlsxBuf
reporter.logger.debug('Merging html-to-xlsx result into xlsx template is starting', request)
if (response.stream.path == null) {
throw reporter.createError('No path was found in xlsx response stream', {
weak: true
})
}