【问题标题】:How to Convert nested JSON into excel in nodejs如何在nodejs中将嵌套的JSON转换为excel
【发布时间】:2022-08-11 20:10:00
【问题描述】:

我正在尝试将以下 JSON 转换为 excel,我正在使用XLSX对于它,它正在将我的 JSON 转换为 excel,但是,dailyPointsArray 的嵌套数组在转换为 excel 后是空白的。

试过的代码

 const XLSX = require(\"xlsx\");
 const workSheet = XLSX.utils.json_to_sheet(attendanceData);
    const workBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workBook, workSheet, \"attendance\");
    XLSX.write(workBook, { bookType: \"xlsx\", type: \"buffer\" });
    XLSX.write(workBook, { bookType: \"xlsx\", type: \"binary\" });
    XLSX.writeFile(workBook,\"newExcel.xlsx\");
attendanceData:[
  {
    workerId: \'1230\',
    workerFullName: \'A\',
    workerDepartment: \'INFORMATION TECHNOLOGY\',
    workerDesignation: \'ASSISTANT MANAGER\',
    Location: \'locationA\',
    dailyPointsArray: [
    {
      inTime: \'-\',
      Date: \'23/03/2022\',
      outTime: \'-\',
      Points: null,
      createdAs: \'ABSENT\'
    },
    {
      inTime: \'-\',
      Date: \'24/03/2022\',
      outTime: \'-\',
      Points: null,
      createdAs: \'ABSENT\'
    }
   ],
    total_duration: 0,
    total_shift_points: 0
  },
  {
    workerId: \'1128\',
    workerFullName: \'B\',
    workerDepartment: \'INFORMATION TECHNOLOGY\',
    workerDesignation: \'MANAGER\',
    Location: \'LocationA\',
    dailyPointsArray: [
    {
      inTime: \'-\',
      Date: \'23/03/2022\',
      outTime: \'-\',
      Points: null,
      createdAs: \'ABSENT\'
    },
    {
      inTime: \'-\',
      Date: \'24/03/2022\',
      outTime: \'-\',
      Points: null,
      createdAs: \'ABSENT\'
    }
   ],
    total_duration: 17,
    total_shift_points: 2
  },
]

下面是excel文件输出

如您所见,dailyPointsArray 的列是空的。 我想我的 excel 文件应该像下图

    标签: javascript node.js excel express xlsx


    【解决方案1】:

    尝试展平数组:过滤嵌套数组,按您想要的顺序获取您想要的键

    尝试这个:

    const filtered = attendanceData.map(obj => {
    
        // get totals to add them later to keep column order (or use `header` param for columns order)
        const {
            dailyPointsArray,
            total_duration,
            total_shift_points,
            ...rest
        } = obj;
    
        // flatten..
        dailyPointsArray.map(el => {
            rest[el['Date']] = el.createdAs;
        });
    
        return {...rest,
            total_duration,
            total_shift_points
        };
    });
    
    const XLSX = require("xlsx");
    const workSheet = XLSX.utils.json_to_sheet(filtered);
    const workBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workBook, workSheet, "attendance");
    XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
    XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
    XLSX.writeFile(workBook,"newExcel.xlsx");
    

    【讨论】:

      【解决方案2】:

      您还可以使用 typeof 运算符

      const mappedArr = attendanceData.map(item => {
         
          if (item.dailyPointsArray == null) {
              item.dailyPointsArray = "";
          } else if (typeof item.dailyPointsArray == "object") {
              item.dailyPointsArray = JSON.stringify(item.dailyPointsArray);
          }
          return {
          ...item,
          item.dailyPointsArray
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-29
        • 2020-02-20
        • 2020-03-07
        • 2015-04-01
        • 2021-10-21
        • 1970-01-01
        • 2019-08-26
        • 2021-06-13
        • 2022-01-16
        相关资源
        最近更新 更多