sheet_to_csv skips first blank line with blankrows: true option #2752

Open
opened 2022-08-02 16:51:47 +00:00 by pibi · 2 comments
pibi commented 2022-08-02 16:51:47 +00:00 (Migrated from github.com)

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
**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 ```
SheetJSDev commented 2022-08-02 20:55:05 +00:00 (Migrated from github.com)

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);
  }
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): ```js 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); } ```
anmol5varma commented 2022-08-14 13:48:05 +00:00 (Migrated from github.com)

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

Is the issue still open? I would like to take it up if that is the case.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2752
No description provided.