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.

Looping through rows and columns

See original GitHub issue

How do I loop through rows and columns without converting the worksheet to csv or json before doing so?

Is it not possible to do something like

var worksheet = workbook.Sheets['Sheet1'];

for (var row in worksheet) {
  for (var col in row) {
    console.log(worksheet[row][col]);
  }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

74reactions
reviewhercommented, Mar 3, 2017

@gregpinero the JSON conversion has a little bit of “intelligence” and is poorly documented 😕

For example consider this worksheet:

|foo|bar|baz|
| 1 | 2 | 3 |
| 4 | 5 | 6 |

Let’s say we are in node (the relevant parts work the same way in browser). Read the workbook:

var XLSX = require('xlsx');
var wb = XLSX.readFile('Workbook4.xlsx');
var ws = wb.Sheets.Sheet1;

So by default, the conversion will read the first row and use them as keys in the row object:

> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1)
[ { foo: '1', bar: '2', baz: '3' },
  { foo: '4', bar: '5', baz: '6' } ]

If a field is missing, the data won’t show up. If there’s data in a cell with no header (say, data in cell D2 but not D1), data won’t show up.

passing an options argument with header controls the output:

Setting header to 1 will generate arrays:

> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {header:1})
[ [ 'foo', 'bar', 'baz' ], [ '1', '2', '3' ], [ '4', '5', '6' ] ]

Setting header to A will use the column names:

> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {header:'A'})
[ { A: 'foo', B: 'bar', C: 'baz' },
  { A: '1', B: '2', C: '3' },
  { A: '4', B: '5', C: '6' } ]

You can even give custom labels by passing an array:

> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {header:["what", "the", "flip"]})
[ { what: 'foo', the: 'bar', flip: 'baz' },
  { what: '1', the: '2', flip: '3' },
  { what: '4', the: '5', flip: '6' } ]

The easiest way to control the range is to change the !ref value on the sheet. It’s expected to be an A1-style range like “A1:C4”. Changing the value will change the range that the utils look at:

> wb.Sheets.Sheet1['!ref']
'A1:C3'
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1)
[ { foo: '1', bar: '2', baz: '3' },
  { foo: '4', bar: '5', baz: '6' } ]

// now change to A1:A3
> wb.Sheets.Sheet1['!ref'] = 'A1:A3';
'A1:A3'
// the output will change
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1)
[ { foo: '1' }, { foo: '4' } ]

The same parameter also affects writing workbooks – cells outside of that range are not included:

> wb.Sheets.Sheet1['!ref'] = 'A1:C3';
'A1:C3'
> XLSX.read(XLSX.write(wb, {type:'binary'}), {type:'binary'}).Sheets.Sheet1
{ '!ref': 'A1:C3',
  A1: { t: 's', v: 'foo', h: 'foo', w: 'foo' },
  B1: { t: 's', v: 'bar', h: 'bar', w: 'bar' },
  C1: { t: 's', v: 'baz', h: 'baz', w: 'baz' },
  A2: { t: 'n', v: 1, w: '1' },
  B2: { t: 'n', v: 2, w: '2' },
  C2: { t: 'n', v: 3, w: '3' },
  A3: { t: 'n', v: 4, w: '4' },
  B3: { t: 'n', v: 5, w: '5' },
  C3: { t: 'n', v: 6, w: '6' } }
> wb.Sheets.Sheet1['!ref'] = 'A1:A3';
'A1:A3'
> XLSX.read(XLSX.write(wb, {type:'binary'}), {type:'binary'}).Sheets.Sheet1
{ '!ref': 'A1:A3',
  A1: { t: 's', v: 'foo', h: 'foo', w: 'foo' },
  A2: { t: 'n', v: 1, w: '1' },
  A3: { t: 'n', v: 4, w: '4' } }
34reactions
gregpinerocommented, Mar 3, 2017

Thanks. That does work.

I would like something just like sheet2arr above in the utils. That what I thought XLSX.utils.sheet_to_row_object_array would do but it seemed to give me strange results missing most of the data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iterating over rows and columns in Pandas DataFrame
In order to iterate over rows, we use iteritems() function this function iterates over each column as key, value pair with the label...
Read more >
Pandas Iterate Over Rows with Examples
Like any other data structure, Pandas DataFrame also has a way to iterate (loop through row by row) over rows and access columns/elements...
Read more >
How to iterate over rows in a DataFrame in Pandas
The trick is to loop over zip(df['A'], df['B']) instead of df.iterrows() . Under List Comprehensions, the "iterating over multiple columns" example needs a ......
Read more >
Pandas Tutorial Part #13 – Iterate over Rows & Columns of ...
Iterate over columns of DataFrame by column numbers · Get the count of total columns in the DataFrame. · Loop over 0 to...
Read more >
How to Iterate Over Rows in a Pandas DataFrame - Stack Abuse
Iterating DataFrames with iterrows(). While df.items() iterates over the rows in column-wise, doing a cycle for each column, we can use iterrows ......
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