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.

Trying to remove whitespace from a column header

See original GitHub issue

This is really two questions in one. Hopefully someone can help me…

I have a lambda function written in node.js which uses the library to convert an excel spreadsheet into JSON. This was working fine except for that some of the column headers had whitespace at the end. I was trying to remove it by adding the code below (in visual studio code), which I found in another issue.

https://github.com/SheetJS/js-xlsx/issues/443#event-1012916154

 var range = XLSX.utils.decode_range(ws['!ref']);
    for(var R = range.s.r; R <= range.e.r; ++R){
      for(var C = range.s.c; C <= range.e.c; ++C) {
        var coord = XLSX.utils.encode_cell({r:R, c:C}), cell = ws[coord];
        if(!cell || !cell.v) continue;
        // clean up raw value of string cells
        if(cell.t == 's') cell.v = cell.v.trim();
        // clean up formatted text
        if(cell.w) cell.w = cell.w.trim();
      }
    }

Instead of removing white space it removed the entire column. When I reverse these changes the column was still missing when I ran the previously working code.

  1. Does anyone know why column is still missing when I have reverted to the original code?
  2. How can I trim just the white space from a column header with this library?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
SheetJSDevcommented, Oct 21, 2019

If a column is missing, likely the header label is missing or the range is incorrect.

To trim column headers:

function trim_headers(ws) {
	if(!ws || !ws["!ref"]) return;
	var ref = XLSX.utils.decode_range(ws["!ref"]);
	for(var C = ref.s.c; C <= ref.e.c; ++C) {
		var cell = ws[XLSX.utils.encode_cell({r:ref.s.r, c:C})];
		if(cell.t == "s") {
			cell.v = cell.v.trim();
			if(cell.w) cell.w = cell.w.trim();
		}
	}
}
2reactions
MelvinVermeercommented, Oct 17, 2019

We also wanted to trim headers, this is what we used. Lodash - mapKeys (docs) Hope this helps.

const json = XLSX.utils
        .sheet_to_json(workbook.Sheets['Sheet1'])	        
        .map(row => mapKeys(row, (value, key) => key.trim()))
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I strip the whitespace from Pandas DataFrame ...
I am parsing data from an Excel file that has extra white space in some of the column headings. When ...
Read more >
How to remove trailing whitespaces from column headers in ...
In this blog post we'll do a small thing to remove trailing or leading whitespaces from a DataFrame's column names.
Read more >
Remove spaces from column names in Pandas - GeeksforGeeks
Removing spaces from column names in pandas is not very hard we easily remove spaces from column names in pandas using replace() function....
Read more >
Need to remove whitespace from datatable specific column
Above query helps you remove all extra white spaces and write data back to the DataTable. Replace ColumnName with actual column the one...
Read more >
Pyspark – Remove Spaces From DataFrame Column Header
In modern era of data world, when we load data into our data lake we try to implement some standardization's example: Naming Standards, ......
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