Excel columns shifting incorrectly when deleting a column.



  • We have several excel spread sheets where we need to have a dynamic number of columns. Usually we do this by adding an additional columns and removing the ones that are not needed. We have now run into a scenario where when we delete a column the data in the cells to the right of the column shift unexpectantly to the left. Please note that this is just a simple example that explains the issue.

    Example
    We have this spread sheet:
    0_1731371602584_upload-ee3456a8-dedf-4ef5-892f-21524e029c5b
    We then have something like this on the template to remove the column:
    0_1731371565909_upload-30610a97-a631-4888-bc21-e31e2e0b06b8
    With this standard removeColumn from the forum:

    function removeColumn(index, opts) {
        // just to get an idea whats inside
        console.log(JSON.stringify(opts.data.root.$xlsxTemplate['xl/worksheets/sheet1.xml']))
        const rows = opts.data.root.$xlsxTemplate['xl/worksheets/sheet1.xml'].worksheet.sheetData[0].row
         for (const r of rows) {
            r.c.splice(index, 1)
            for (let c of r.c) {
                console.log (c.$.r);
                // let excel recalculate cell names
                delete c.$.r
            }
        }    
    }
    

    So in this example we are just trying to remove column B (ordinal 1). When this run we get this as a result.
    0_1731371645821_upload-3d477e66-6e44-4076-b567-9c94737db45d
    What I would expact is this.
    0_1731371705602_upload-d0b7ca53-31bc-432f-a9af-c4156751fd63
    I am not sure what happens here, Everything gets compressed to the left and not sure what happens to the value of E3.

    I thought maybe it had to do with the delete c.$.r function call so I change it to delete c.r to see what would happen. I get this for a result them:
    0_1731372553036_upload-19d4cb27-9b51-4247-8fb0-6dd339fea107
    It removed the values in column B, but did not delete the column. Also it still completely lost the value of E3.

    Where can I find more documentation on what the delete c.$.r does or any other help on how to remove columns.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.