Sure, here’s how you can solve this problem using Node.js and the xlsx package: 1.…
- User
Sure, here’s how you can solve this problem using Node.js and the xlsx package:
-
Install the xlsx package using npm install xlsx.
-
Load the Excel file into a workbook with const workbook = xlsx.readFile('your_filename.xlsx').
-
Get the data from the first sheet with const worksheet = workbook.Sheets[workbook.SheetNames[0]]; const data = xlsx.utils.sheet_to_json(worksheet, { header: 1 }).
-
Find the merged header row with const mergedIndex = data.findIndex(row => row[0] === undefined).
-
Unmerge the header row with const columnHeaders = data[mergedIndex + 1].map((cellValue, index) => [data[mergedIndex][index] || '', cellValue]); const unmergedData = data.slice(mergedIndex + 2); unmergedData.unshift(columnHeaders.map(header => header[0] || header[1]));.
-
Create a new worksheet with the unmerged data with const newWorksheet = xlsx.utils.aoa_to_sheet(unmergedData).
-
Save the new workbook as a CSV with xlsx.writeFile({ SheetNames: ['Sheet1'], Sheets: { Sheet1: newWorksheet } }, 'your_filename.csv').
-
- Assistant
Note: This solution assumes that the merged cells are only located in the first row of the first sheet in the Excel file. If merged cells are located in other sheets, you’ll need to modify the code accordingly.
- User
give the updated code by assuming the merged cells are only the 2nd and 4th row and they start at the H column and O column
- Assistant
- User
if (mergedIndexes[i].includes(j)) { ^
TypeError: Cannot read property 'includes' of undefined
- Assistant
It looks like the
mergedIndexesobject does not have a key for the current row (i) that is being processed. You can add a check to ensure that the key exists before using theincludes()method. Here is the updated code:codeconst unmergedData = []; let currentRowIndex = 0; for (let i = 0; i < data.length; i++) { if (i === 1 || i === 3) { // header rows let currentColumnIndex = 0; const columnHeaders = []; for (let j = 0; j < data[i].length; j++) { if (isMergedCell({ r: i, c: j }, mergedCells)) { // skip merged cells continue; } if (mergedIndexes[i] && mergedIndexes[i].includes(j)) { // check if key exists and value is in array columnHeaders.push([data[i - 1][currentColumnIndex], data[i][j]]); currentColumnIndex++; } else { // regular cell, use value as column header columnHeaders.push([data[i][j], data[i][j]]); currentColumnIndex++; } } unmergedData.push(columnHeaders.map(header => header[0])); currentRowIndex = i + 1; } else { // data rows unmergedData.push(data[i]); } } - User
.writeFile( ^
TypeError: xlsx.utils.aoa_to_sheet(...).writeFile is not a function
- Assistant
Sorry about that! It looks like there is a mistake in the previous solution. The
writeFilemethod can only be called on thexlsxobject, not on a sheet object.Here's the corrected code:
codeconst fs = require('fs'); // ... all the previous code ... // Save the new workbook as a CSV const newWorkbook = { SheetNames: ["Sheet1"], Sheets: { Sheet1: newWorksheet } }; const csvData = xlsx.utils.sheet_to_csv(newWorksheet); fs.writeFileSync('your_filename.csv', csvData, 'utf8');Instead of calling
xlsx.writeFile, create a new object that represents the entire workbook, including all sheets. In this case, we only have one sheet, so it's very simple.Then, we convert the sheet data to CSV using
xlsx.utils.sheet_to_csv. Finally, we write the CSV data to a file usingfs.writeFileSync.Let me know if this works for you!
- User
its returning an empty csv , check the complete code const fs = require("fs"); const xlsx = require("xlsx");
const workbook = xlsx.readFile("./imports/AFR_Index_0115.xlsx"); let newWorksheet;
const worksheet = workbook.Sheets[workbook.SheetNames[0]]; const data = xlsx.utils.sheet_to_json(worksheet, { header: 1, });
function isMergedCell(cell, mergedCells) { for (let i = 0; i < mergedCells.length; i++) { if ( cell.r >= mergedCells[i].s.r && cell.r <= mergedCells[i].e.r && cell.c >= mergedCells[i].s.c && cell.c <= mergedCells[i].e.c ) { return true; } } return false; }
- Assistant
It looks like you forgot to assign the unmerged data to the
newWorksheetvariable. Here's the updated code:code