我试图导出Excel文件与动态列和行一些列包含值或一些没有。请帮我解决这个问题。我正在使用nodejs exceljs库。
它正确地导出了静态列的数据,但是当我试图Map动态列时,worksheet.addRow
创建了动态列Map的问题。
先谢谢你了。
当我分配动态列键Map时出现问题。这在这里是不支持的。
worksheet.addRow({
goal_name: m.name,
goal_description: m.purpose,
created: m.created,
privacy_types_id: m.privacy_types_id,
priority: m.priority,
progress: m.stats.progress,
/*should be dyanmic key mapping all below
fields*/
CustomField1: item.value,
CustomField2: item.value,
CustomField3: item.value,
CustomField4: item.value,
CustomField5: item.value,
});
这是我的nodejs代码:
const stream = new Stream.PassThrough();
const workbook = new excel.stream.xlsx.WorkbookWriter({
stream: stream,
});
let worksheet = workbook.addWorksheet("Goals");
// mapping static columns names
worksheet.columns = [
{ header: "Name", key: "goal_name", width: 15 },
{ header: "Description", key: "goal_description", width: 15 },
{ header: "Created Date", key: "created", width: 20 },
{ header: 'Privacy Type', key: 'privacy_types_id', width: 20 },
{ header: 'Priority', key: 'priority', width: 20 },
{ header: 'Progress', key: 'progress', width: 20 },
];
// let rows = [];
// Adding dynamic columns
_goals.forEach((m, pIndex) => {
if (m.fields !== null && m.fields.length > 0) {
let i = 1;
m.fields.forEach((item, index) => {
let customFieldName = item.field_name.replace(/\s/g, '_');;
let customFieldValue = item.value;
let getExistingColumns = worksheet.columns;
const found = getExistingColumns.some(el => el._header === item.field_name);
var CustomField = 'CustomField' + i;
if (!found) {
let newColumn = worksheet.getColumn(worksheet.columns.length + 1);
newColumn.header = item.field_name;
newColumn.key = 'CustomField' + i;
newColumn.width = 15;
//worksheet.columns.push(newColumn);
i++;
}
});
}
});
// Adding goals rows
_goals.forEach((m, pIndex) => {
if (m.fields !== null && m.fields.length > 0) {
let i = 1;
m.fields.forEach((item, index) => {
let customFieldName = item.field_name.replace(/\s/g, '_');;
let customFieldValue = item.value;
let getExistingColumns = worksheet.columns;
const found = getExistingColumns.some(el => el._header === item.field_name);
var CustomField = 'CustomField' + i;
worksheet.addRow({
goal_name: m.name,
goal_description: m.purpose,
created: m.created,
privacy_types_id: m.privacy_types_id,
priority: m.priority,
progress: m.stats.progress,
CustomField1: item.value,
CustomField2: item.value,
CustomField3: item.value,
CustomField4: item.value,
CustomField5: item.value,
});
});
}
else {
worksheet.addRow({
goal_name: m.name,
goal_description: m.purpose,
created: m.created,
privacy_types_id: m.privacy_types_id,
priority: m.priority,
progress: m.stats.progress,
});
}
});
worksheet.commit();
workbook.commit();
const file_uuid = uuidv4();
const name = 'Goals list';
const mimetype = 'application/vnd.ms-excel';
const ext = 'xlsx';
1条答案
按热度按时间sbdsn5lh1#
要使用Node.js中的exceljs库动态MapExcel文件中的列,您可以尝试这样做: