question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Corrupt xlsx after updating

See original GitHub issue

Hello, great lib, I have an issue with the following code whereby the updated file become corrupt. The val you see in code is a string version of a bool so “true” or “false”, I did hardcode a string as val (“hello”), just to see if it was the bool that caused the issue, but the file still got corrupted. Can you please take a look and see what I may be doing wrong, thanks. If I am able to recover the file, it has the updated value, but my app is no longer able to read the file for subsequent parsing

function updateExcelEntry(inputPage,sheet,colKeyIndex,colIndexToUpdate,rowKey,val){
    //console.log("in excel " + inputPage);
    var workbook = new Excel.Workbook();
    workbook.xlsx.readFile(inputPage).then(function() {
            // use workbook
        //console.log("getting sheet " + sheet);
        var worksheet = workbook.getWorksheet(sheet);
        //console.log("getting keyCol " + colKeyIndex);
        var keyCol = worksheet.getColumn(colKeyIndex);
        keyCol.key = "key";
        var col = worksheet.getColumn(colIndexToUpdate);
        col.key = "colToUpdate";

        worksheet.eachRow(function(row, rowNumber) {

            //console.log("checking key val " + row.getCell("key").value);
            //console.log("vs rowKey " + rowKey);
            if(rowNumber !== 1 && row.getCell("key").value === rowKey){
                //console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
                row.getCell("colToUpdate").value = val;

            }
        });


    }).then(function() {
        return workbook.xlsx.writeFile(inputPage)
    }).then(function() {
        console.log("workbook updated " + inputPage);
    });
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:5
  • Comments:19

github_iconTop GitHub Comments

7reactions
mercmobilycommented, Jul 25, 2019

I solved the problem by using:

https://www.npmjs.com/package/xlsx-populate

All sorts of small formatting problems simply disappeared. I cannot recommend it enough.

4reactions
SergioDonaticommented, Mar 1, 2017

I have same issue. After read the latest comments i make a test. Make a simple script that only read and write on different file.

First test:

  1. Create new file with LibreOffice
  2. run the script
  3. file corrupted

Second test:

  1. Create new file with Google Docs and download as xlsx
  2. run the script
  3. works!!

So something is different in LibreOffice/OpenOffice files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Repairing a corrupted workbook - Microsoft Support
Repair a corrupted workbook manually · On the File tab, click Open. · In Excel 2013 or Excel 2016, click on the location...
Read more >
7 Ways to Repair Damaged and Corrupt Excel Files
This page offers the 7 best ways to repair damaged and corrupt excel files, the best Excel file repair tool to help you...
Read more >
How to Open Corrupt Excel XLS and XLSX Files?
Click Browse to select the corrupt Excel XLS or XLSX file. The File Open window will pop on-screen now; select the corrupt Excel...
Read more >
Get rid of "The file is corrupt and cannot be opened" in Excel
How to open a corrupt xls. file in Excel 2010 - 365 · Open Excel. · Click on File -> Options. · Select...
Read more >
[Fixed] The File Is Corrupted and Cannot Be Opened in Excel ...
The changes of settings in Microsoft Office after upgrading or reinstallation · Protection of your computer against the files come from another ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found