XLSX Nodejs 不识别逗号 ',' 作为分隔符,而是将其视为整数

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

我有一个 Node js 服务来读取和处理 xls 或 xlsx 文件中的数据

`private fromBankStatementFileToManualJournalRowsJSONFromTTK =(文件:任意)=> { const fileJSON = []; const Sheet = this.readBankStatement(file);

const description = sheet['C2']?.v;

const unformatedDate = sheet['A2']?.v;
const splittedDatePart = unformatedDate?.split('.');
const formattedDate = `${splittedDatePart[2]}-${splittedDatePart[1]}-${splittedDatePart[0]}`;

let data = XLSX.utils.sheet_to_json(sheet, { header: 1 });

let start_index = null;
let end_index = null;

// Find the index of 'должи' and 'Вкупно:'
data.forEach((sublist: any, index: any) => {
  if (sublist.includes('Должи')) {
    start_index = index;
  } else if (sublist.includes('Вкупно:')) {
    end_index = index;
  }
});

// Process data between 'должи' and 'Вкупно:'
if (start_index !== null && end_index !== null) {
  const extractedData = data.slice(start_index, end_index + 0);

  data = extractedData;
}

for (let i = 1; i < data.length; i++) {
  const debit = data[i][5];
  const credit = data[i][4];
  const partner = data[i][1]; // Check if debit and credit are numeric values
  const isDebitNumeric = !isNaN(parseFloat(debit));
  const isCreditNumeric = !isNaN(parseFloat(credit));

  if (isDebitNumeric && isCreditNumeric && partner !== undefined) {
    // Round only if the values are numeric
    const roundedDebit = parseFloat(debit).toFixed(2);
    const roundedCredit = parseFloat(credit).toFixed(2);

    fileJSON.push({
      description,
      transactionDate: formattedDate,
      debit: roundedDebit,
      credit: roundedCredit,
      partnerNameFromFile: partner.toString(),
    });
  }
}

return fileJSON;

};`

文件中的借方和贷方如下: 700,00。 我认为问题在于当我读取文件并将其保存到数据变量时。

这是数据数组对象:

"translationOfFile": [ {``your text`` "description": "[AutoProv]Надомест за водење на сметка за 9.2023", "transactionDate": "2023-09-30", "debit": "70000.00", "credit": "0.00", "partnerNameFromFile": "Company" "clientPartner": null }, { "description": "[AutoProv]Надомест за водење на сметка за 9.2023", "transactionDate": "2023-09-30", "debit": "5000.00", "credit": "0.00", "partnerNameFromFile": "Company", "clientPartner": null }, 
我不想在最后两个数字处添加点,如下所示: 70000 700.00 因为我担心情况不会总是如此。

javascript node.js xlsx exceljs
1个回答
0
投票

我认为我们使用“马其顿”语言。 我用英文转换并回答你的问题。

这是您的输入数据.xlsx

这是寻找输出数据

[
  {
    "description": "Compensation 1",
    "transactionDate": "2023-09-30",
    "debit": "70000.00",
    "credit": "0.00",
    "partnerNameFromFile": "Company",
    "clientPartner": "null"
  },
  {
    "description": "Compensation 2",
    "transactionDate": "2023-09-30",
    "debit": "5000.00",
    "credit": "0.00",
    "partnerNameFromFile": "Company",
    "clientPartner": "null"
  },
  {
    "description": "Another Desc",
    "transactionDate": "2023-09-30",
    "debit": "3000.00",
    "credit": "0.00",
    "partnerNameFromFile": "Another Company",
    "clientPartner": "null"
  },
  {
    "description": "Yet Another Desc",
    "transactionDate": "2023-09-30",
    "debit": "1000.00",
    "credit": "0.00",
    "partnerNameFromFile": "Yet Another Comp",
    "clientPartner": "null"
  }
]

这是我使用 “xlsx”

的演示代码

演示代码

另存为“read-excel.js”

const XLSX = require('xlsx');
const fs = require('fs');

const fromBankStatementFileToManualJournalRowsJSONFromTTK = function (file) {
  const fileJSON = [];
  const workbook = XLSX.readFile(file);
  const sheet = workbook.Sheets[workbook.SheetNames[0]];

  // Loop through each row in the Excel sheet
  for (let i = 2; ; i++) { // Start from row 2 (assuming the header is in row 1)
    const descriptionCell = sheet['A' + i];
    const transactionDateCell = sheet['B' + i];
    const debitCell = sheet['C' + i];
    const creditCell = sheet['D' + i];
    const partnerNameFromFileCell = sheet['E' + i];
    const clientPartnerCell = sheet['F' + i];

    // Check if we have reached the end of the data
    if (!descriptionCell) {
      break;
    }

    const description = descriptionCell.v;
    const transactionDate = transactionDateCell ? transactionDateCell.v : '';
    const debit = debitCell ? debitCell.v.toString().replace(',', '.') : ''; // Replace commas with dots 
    const credit = creditCell ? creditCell.v.toString().replace(',', '.') : ''; // Replace commas with dots 
    const partnerNameFromFile = partnerNameFromFileCell ? partnerNameFromFileCell.v : '';
    const clientPartner = clientPartnerCell ? clientPartnerCell.v : '';

    // Check if transactionDate, debit, and credit are non-empty strings
    if (transactionDate !== '' && debit !== '' && credit !== '') {
      fileJSON.push({
        description,
        transactionDate,
        debit,
        credit,
        partnerNameFromFile,
        clientPartner,
      });
    }
  }

  return fileJSON;
};

const inputFilePath = 'data.xlsx';

const outputJSON = fromBankStatementFileToManualJournalRowsJSONFromTTK(inputFilePath);

fs.writeFileSync('output.json', JSON.stringify(outputJSON, null, 2), 'utf-8');
console.log('Processing complete. Output saved as output.json');

安装依赖项

npm install xlsx

运行它

node read-excel.js

结果

© www.soinside.com 2019 - 2024. All rights reserved.