sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

sheet_to_csv skips first blank line with blankrows: true option

Open pibi opened this issue 2 years ago • 9 comments

sheet_to_csv skips the first blank line of a sheet, even with an explicit {blankrows: true} set.

Code sample:

  var ws = XLSX.utils.aoa_to_sheet([
    [  ,   ,    ,    ,   ,   ,   ],
    ["S", "h", "e", "e", "t", "J", "S"],
    [  ,   ,    ,    ,   ,   ,   ],
    [  1,   2,    ,    ,   5,   6,   7],
    [  2,   3,    ,    ,   6,   7,   8],
    [  3,   4,    ,    ,   7,   8,   9],
    [  4,   5,   6,   7,   8,   9,   0]
  ]);
  
  console.log(XLSX.utils.sheet_to_csv(ws, {blankrows: true}));

Result:

S,h,e,e,t,J,S
,,,,,,
1,2,,,5,6,7
2,3,,,6,7,8
3,4,,,7,8,9
4,5,6,7,8,9,0

Expected:

,,,,,,
S,h,e,e,t,J,S
,,,,,,
1,2,,,5,6,7
2,3,,,6,7,8
3,4,,,7,8,9
4,5,6,7,8,9,0

pibi avatar Aug 02 '22 16:08 pibi

This is an aoa_to_sheet issue. It will generate a sheet where the starting cell is A2 rather than A1. See https://github.com/SheetJS/sheetjs/issues/2737 for more details on why the range is relevant.

The simplest fix is to pin the starting point to the origin in https://github.com/SheetJS/sheetjs/blob/master/bits/27_csfutils.js#L176 (we'll accept a PR):

  if(range.s.c < 10000000) {
    if(range.s.c > _C) range.s.c = _C;
    if(range.s.r > _R) range.s.r = _R;
    ws['!ref'] = encode_range(range);
  }

SheetJSDev avatar Aug 02 '22 20:08 SheetJSDev

Is the issue still open? I would like to take it up if that is the case.

anmol5varma avatar Aug 14 '22 13:08 anmol5varma

Is the issue still open? I would like to take it up if that is the case.

anshul137 avatar Oct 05 '22 05:10 anshul137

Hi @pibi , I'd like to work on this, could you please assign me :)

PrathyushaModala avatar Jan 30 '23 14:01 PrathyushaModala

is it still open?

naikmohit13 avatar May 31 '23 15:05 naikmohit13

Has this issue been solved? I am seeing a PR request for a fix. If the issue has not been solved I would like to take it up.

sayantandasgupta avatar Jul 05 '23 17:07 sayantandasgupta

"Hello, I have a solution for this issue. If it has not been solved yet, I would love to solve it."

VanshSutariya avatar Aug 12 '23 07:08 VanshSutariya

by using Array(7).fill('') as a blank row we can solve this issue

VanshSutariya avatar Aug 12 '23 07:08 VanshSutariya

could you assign this issue to me? @pibi

adesh1998 avatar Nov 07 '23 23:11 adesh1998