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.

How to get empty string when the cell is empty and sits at the last column

See original GitHub issue

Discussed in https://github.com/exceljs/exceljs/discussions/1921

<div type='discussions-op-text'>

Originally posted by kmorte December 3, 2021 Hi

I’m trying to build a json object per row. But rows with an empty cell as the last onw don’t get parsed be the library and then values don’t match with headers.

Thanks.

const book = new ExcelJS.stream.xlsx.WorkbookReader(fileRoute, {
        sharedStrings: 'cache',
        hyperlinks: 'emit',
        worksheets: 'emit',
        entries: 'emit'
    });
for await (const sheet of book) {

        const headers = {};
        const values = {};
        for await (const row of sheet) {

            const model = row.model;
            const cells = model.cells;

            if(model.number === 1) {
                cells.forEach((c, i)=> {

                    headers[i] = c.value;
                });
            };

            if(model.number !== 1) {
                cells.forEach((c, i)=> {

                     // if the last cell is empty, it does not appear here

                    values[i] = c.value;
                });
            };

            const item = {};
            Object.keys(headers).forEach(h=> {

                Object.keys(values).forEach(v => {

                    if(h === v) {
                        item[headers[h]] = values[v]

                    };
                });
            });

            // console.log('item ', item );

        };

    };
```</div>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
skypeskycommented, Dec 9, 2021

@kmorte

i && headers.push(c.value)

Equivalent to

if(i) {
  headers.push(c.value)
}

Equivalent to

if(i > 0) {
  headers.push(c.value)
}
2reactions
skypeskycommented, Dec 7, 2021

Is the result of the following code running what you expect?

    for await (const sheet of book) {

      const headers = [];

      for await (const row of sheet) {

        const values = [];
        const model = row.model;

        if (model.number === 1) {
          row.eachCell({includeEmpty: true}, (c, i) => {
            i && headers.push(c.value);
          });
        };

        if (model.number > 1) {
          row.eachCell({includeEmpty: true}, (c, i) => {
            i && values.push(c.value);
          });
        };

        const item = {};

        for (let i = 0; i < headers.length; ++i) {
          item[headers[i]] = values[i];
        }

        console.log(headers, values, item);

      };

    };

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel conditional formatting for blank cells - Ablebits
Open the Rule Manager (Conditional Formatting > Manage Rules), make sure the "Blanks" rule is at the top of the list, and tick...
Read more >
Excel Formula Master Tricks for Empty Cells, Zeros ... - YouTube
Excel Formula Master Tricks for Empty Cells, Zeros, Zero Length Text String and 'Blanks' EMT 1764.
Read more >
How to Apply Conditional Formatting to Blank Cells | Excelchat
Want to learn how to apply conditional formatting to blank cells in Excel? This post will give you an overview of how to...
Read more >
Display empty cells, null (#N/A) values, and hidden worksheet ...
By default, data that is hidden in rows and columns in the worksheet is not displayed in a chart, and empty cells or...
Read more >
How to use NULL or empty string in SQL - Stack Overflow
17 Answers 17 · 3. SELECT * FROM TableName WHERE columnNAme IS NULL OR RTRIM(columnName) = '' · 2. As Xavier points out,...
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