const fs = require("fs"); const xlsx = require("xlsx"); const workbook =…
- User
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; }
const sheet = workbook.Sheets[workbook.SheetNames[0]]; const mergedCells = sheet["!merges"] ? sheet["!merges"] : []; const mergedIndexes = { 2: [], 4: [], }; for (const mergedCell of mergedCells) { if (mergedCell.s.r === 1 && mergedCell.s.c >= 7 && mergedCell.e.c <= 9) { // merged cell in second row starting at H column mergedIndexes[2].push(mergedCell.s.c); } if (mergedCell.s.r === 3 && mergedCell.s.c >= 14 && mergedCell.e.c <= 16) { // merged cell in fourth row starting at O column mergedIndexes[4].push(mergedCell.s.c); } }
const 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)) { // merged cell, get value from merged header cell 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]); } }
// create a worksheet from the unmerged data newWorksheet = xlsx.utils.aoa_to_sheet(unmergedData);
// Save the new workbook as a CSV const newWorkbook = { SheetNames: ["Sheet1"], Sheets: { Sheet1: newWorksheet, }, };
const csvData = xlsx.utils.sheet_to_csv(newWorksheet); fs.writeFileSync("./imports/AFR_Index_0115.csv", csvData, "utf8"); this code should only check for merged cells in H2:I2 and keep goin to the next cel until it is null then unmerged the cell and copy the values of the merged cell to both h2 and i2.
- Assistant
To modify the code to only check for merged cells in H2:I2 and copy the values of the merged cell to both cells H2 and I2 if they are merged, you can make the following changes:
- Instead of iterating through all merged cells in the sheet, only check for merged cells in the range H2:I2:
const mergedCells = sheet["!merges"] ? sheet["!merges"].filter( (mergedCell) => mergedCell.s.r === 1 && mergedCell.s.c >= 7 && mergedCell.e.c <= 8 ) : [];This filters out all merged cells outside of the H2:I2 range.
- If there are merged cells in the range H2:I2, unmerge them and copy the values of the merged cell to both cells:
mergedCells.forEach((mergedCell) => { const startCell = xlsx.utils.encode_cell({ r: mergedCell.s.r, c: mergedCell.s.c }); const endCell = xlsx.utils.encode_cell({ r: mergedCell.e.r, c: mergedCell.e.c }); // Get the value of the merged cell const mergedCellValue = worksheet[startCell].v; // Unmerge the cell delete sheet[startCell].s.merge; sheet[endCell] = Object.assign({}, sheet[startCell]); delete sheet[startCell]; // Set the value of both cells to the merged cell value sheet[`${xlsx.utils.encode_col(mergedCell.s.c)}2`] = { t: "s", v: mergedCellValue }; sheet[`${xlsx.utils.encode_col(mergedCell.s.c + 1)}2`] = { t: "s", v: mergedCellValue }; });