如何在exceljs中将数组放在相同的单元格中



我正在收到一份报告,这份报告如下。

"reports": [
{
"id": 3,
"operatorName": "string",
"systemName": "string",
"serverName": "string",
"alertTypeName": "string",
"procedureName": "string",
"alertDate": "2023-04-02T06:55:59.446Z",
"departmentName": "string",
"noticeTypeName": "string",
"ticket": "string",
"alertCount": 1,
"alertDetail": "string",
"actions": [
{
"id": 1,
"processId": 3,
"actionTypeName": "string",
"actionDescription": "string",
"actionDate": "2023-04-02T06:55:59.446Z"
}]]

我用下面的excel服务导出这个报告。


/* Get Data from Json */
let columnsArray: any[];
for (const key in json) {
if (json.hasOwnProperty(key)) {
columnsArray = Object.keys(json[key]);
}
}
/* Add Data */
data.forEach((element : any) => {
const eachRow: any[] = [];
columnsArray.forEach((column) => {
eachRow.push(element[column]);
});

if (element.isDeleted === 'Y') {
const deletedRow = worksheet.addRow(eachRow);
deletedRow.eachCell((cell) => {
cell.font = {name: 'Calibri', family: 4, size: 11, bold: false, strike: true};
});
} else {
worksheet.addRow(eachRow);
}
});
worksheet.addRow([]);

/* Save Excel */
workbook.xlsx.writeBuffer().then((data: ArrayBuffer) => {
const blob = new Blob([data], {type: EXCEL_TYPE});
fs.saveAs(blob, excelFileName + EXCEL_EXTENSION);
});
}
}

但是我不能得到我想要的actions列。

输入图片描述

我想在同一个单元格中看到actions数组

输入图片描述

谢谢你的帮助。

动作数组必须转换为字符串。你必须使用"n"返回新行

const eachRow: any[] = [];
columnsArray.forEach((column) => {
if (column === 'actions') {
const actions = element[column].map((action: any) => {
//format each action here. I you want all object, try:
//return JSON.stringify(action)
return `${action.actionTypeName} (${action.actionDate})`;
}).join('n');
eachRow.push(actions);
} else {
eachRow.push(element[column]);
}
});

相关内容

  • 没有找到相关文章