我需要用JavaScript创建一个Web应用程序来将JSON文件转换为Excel文件,以这种方式:JSON文件包含一些简单的属性,每个值都应该放在Excel文件的列中,也有一些2D数组或嵌套,嵌套的JSON属性,我想在Excel文件中为JSON文件中的父属性的孩子添加额外的列或行。
"name": "processingPriority",
"type": [
"null",
"string"
],
"doc": "",
"default": null,
"since": "1.0"
},
{
"name": "flags",
"type": [
"null",
{
"type": "array",
"items": "string",
"java-class": "java.util.List"
}
],
"doc": "Mandatory. Empty list allowed, see below for allowed values, List of <<TransactionFlags, allowed ENUM values>>",
"default": null,
"since": "1.0"
},
{
"name": "customerData",
"type": [
"null",
"string"
],
"doc": "",
"default": null,
"since": "1.0"
},
{
"name": "account",
"type": [
"null",
{
"type": "record",
"name": "TransactionAccount",
"fields": [
{
"name": "id",
"type": [
"null",
"string"
],
"doc": "Mandatory. Unique ID (needs to match the one used by WebAPI)",
"default": null,
"since": "1.0"
},
字符串
使用下面的函数,我可以生成这样的Excel文件:excel file
// ... my existing code
function downloadAsExcel() {
// ... my existing code
function getTypeAsString(type, parentName = '') {
if (Array.isArray(type)) {
return type.map(t => getTypeAsString(t)).join(', ');
} else if (typeof type === 'object' && type !== null) {
if (type.type === 'record' && Array.isArray(type.fields)) {
const fields = type.fields.map(field => {
const fieldName = field.name;
const fieldComments = field.doc ? ` (${field.doc})` : '';
return `${parentName}.${fieldName}${fieldComments}:${getTypeAsString(field.type, `${parentName}.${fieldName}`)}`;
}).join(', ');
return `{${fields}}`;
} else {
const objKeys = Object.keys(type).map(key => {
if (typeof type[key] === 'object' && type[key] !== null) {
return `${key}:${getTypeAsString(type[key], `${parentName}.${key}`)}`;
} else {
return `${key}:${type[key]}`;
}
}).join(', ');
return `{${objKeys}}`;
}
} else {
return typeof type === 'undefined' ? 'null' : type.toString();
}
}
function processNestedAttributes(fields, nestedLevel = 0, parentName = '') {
let nestedRows = [];
fields.forEach(field => {
const typeValue = getTypeAsString(field.type, `${parentName}.${field.name}`);
if (typeof field.type === 'object' && field.type !== null && field.type.type === 'record' && Array.isArray(field.type.fields)) {
field.type = `{Nested Table}`;
nestedRows.push([field.name, 'null', typeValue, field.doc, field.default, field.since, nestedLevel]);
const nestedFieldRows = processNestedAttributes(field.type.fields, nestedLevel + 1, `${parentName}.${field.name}`);
nestedRows = nestedRows.concat(nestedFieldRows);
} else {
if (typeValue === 'null') {
nestedRows.push([field.name, typeValue, '', field.doc, field.default, field.since, nestedLevel]);
} else {
nestedRows.push([field.name, 'other', typeValue, field.doc, field.default, field.since, nestedLevel]);
}
}
});
return nestedRows;
}
for (var i = 0; i < jsonContent.fields.length; i++) {
const field = jsonContent.fields[i];
if (typeof field.type === 'object' && field.type !== null && field.type.type === 'record' && Array.isArray(field.type.fields)) {
field.type = `{Nested Table}`;
dataRows.push([field.name, 'null', getTypeAsString(field.type), field.doc, field.default, field.since, 0]);
const nestedRows = processNestedAttributes(field.type.fields, 1, field.name);
dataRows = dataRows.concat(nestedRows);
} else {
const typeValue = getTypeAsString(field.type);
if (typeValue === 'null') {
dataRows.push([field.name, typeValue, '', field.doc, field.default, field.since, 0]);
} else {
dataRows.push([field.name, 'other', typeValue, field.doc, field.default, field.since, 0]);
}
}
}
// ... my existing code
}
// ... my existing code
型
但我想有一个新的列嵌套属性值在类型列.我怎么能改变代码有这样的Excel文件?
1条答案
按热度按时间dgtucam11#
我喜欢分裂逻辑..
字符串