如何使我的XL转换后的文本在nodejs中全部小写和修剪?

问题描述 投票:0回答:1

我试着将我的xl转换为json。转换是发生好,但虽然我已经写了代码转换为小写和修剪,它没有发生。我得到的输出如下......但我希望输出的都是小写和修剪的文字......谁能帮帮我?

   var exceltojson = require("xlsx-to-json");
          exceltojson(
              {
            input: "test.xlsx",
            output: 'test.txt', 
            sheet: "Sheet1",  

          }, function(err, result) 
          {
            if(err) 
            {
              console.error(err);
            }
          });

输出。

[
  {
    Email: '[email protected]',
    'First Name': 'a',
    'Middle Name': 'b',
    'Last Name': 'c',
    'Address 1': 'd',
    'Address 2': 'lol',
    'Phone No': '123456789'
  },
  {
    Email: '[email protected]',
    'First Name': 'g',
    'Middle Name': 'h',
    'Last Name': 'i',
    'Address 1': 'j',
    'Address 2': 'lol',
    'Phone No': '458745'
  }
]

我希望有这样的输出

[
  {
    email: '[email protected]',
    firstname: 'a',
    middlename: 'b',
    lastname: 'c',
    'address 1': 'd',
    'address 2': 'lol',
    phoneno: '123456789',
  },
  {
    email: '[email protected]',
    firstname: 'g',
    middlename: 'h',
    lastname: 'i',
    address1: 'j',
    'address 2': 'lol',
    phoneno: '458745',
  },
];
node.js excel text trim lowercase
1个回答
1
投票

该库没有任何选项,如trimtolowercase。你必须手动完成。

const exceltojson = require('xlsx-to-json');
const fs = require('fs');
exceltojson({
    input: 'test.xlsx',
    // output: 'test.txt', Don't need output
    sheet: 'Sheet1'
  },
  function(err, result) {
    if (err) {
      console.error(err);
      return;
    }
    const newResult = result.map(obj => {
      const newObj = Object.keys(obj).reduce((acc, key) => {
        const newKey = key.replace(/ /g, '').toLowerCase();
        acc[newKey] = obj[key];
        return acc;
      }, {});
      return newObj;
    });
    fs.writeFileSync('file.txt', JSON.stringify(newResult));
  }
);
© www.soinside.com 2019 - 2024. All rights reserved.