79526762

Date: 2025-03-22 00:11:44
Score: 0.5
Natty:
Report link

I know it's been a long time since this question is up but here is a possible soultion.

The only way I could work around it was to save the style object attached to the original cell and then apply these styles back to each cell in the newly inserted row. I know it's very tedious but that's the only way I could do it and it works.

The code below gets sampleStyles via getCellStyles() from designated cells defined in a sampleRowNum and then applying these styles to the new row cells using writeTransactionToRow()

function writeTransactionToRow(row, transaction, sampleStyles) {
    row.getCell(dateColumn).value = transaction.date;
    row.getCell(descColumn).value = transaction.description;
    row.getCell(amountColumn).value = transaction.amount;

    // Apply original cell format styles to the row
    const { dateCellStyle, descCellStyle, amountCellStyle } = sampleStyles;
    row.getCell(dateColumn).style = { ...dateCellStyle };
    row.getCell(descColumn).style = { ...descCellStyle };
    row.getCell(amountColumn).style = { ...amountCellStyle };
}

function getCellStyles(worksheet) {
    // get format style object from sample row
    const dateCellStyle = worksheet
        .getRow(sampleRowNum)
        .getCell(dateColumn).style;
    const descCellStyle = worksheet
        .getRow(sampleRowNum)
        .getCell(descColumn).style;
    const amountCellStyle = worksheet
        .getRow(sampleRowNum)
        .getCell(amountColumn).style;
    return { dateCellStyle, descCellStyle, amountCellStyle };
}

Reasons:
  • Blacklisted phrase (1): I know it's been
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Hamid Ali