Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColumnIndex negative if culture is tr-TR #470

Closed
mganss opened this issue Dec 1, 2020 · 7 comments
Closed

ColumnIndex negative if culture is tr-TR #470

mganss opened this issue Dec 1, 2020 · 7 comments
Labels
Milestone

Comments

@mganss
Copy link

mganss commented Dec 1, 2020

I encountered this when investigating mganss/ExcelMapper#87 (see there for demo file). Demo code:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");

var wb = WorkbookFactory.Create(@"example01.xlsx");
var sheet = wb.GetSheet("Analyses");
var row = sheet.GetRow(1);
var cells = row.Cells;
var cell = cells[0];

// row.FirstCellNum -> -1
// cell.ColumnIndex -> -1

If you change the culture to en-US the issue does not occur. This might be because of the Turkish-I Problem.

@tonyqus
Copy link
Member

tonyqus commented Dec 2, 2020

This is weird. Can you check if you can reproduce the issue with 2.4.1? Looks to be a issue caused by ThreadLocal variable. Is it possible that something returns null and then FirstCellNum returns -1?

@tonyqus tonyqus added the bug label Dec 2, 2020
@mganss
Copy link
Author

mganss commented Dec 2, 2020

It doesn't occur in 2.4.1 but does in 2.5.0.

Is it possible that something returns null and then FirstCellNum returns -1?

What do you mean exactly?

@tonyqus tonyqus added this to the NPOI 2.5.3 milestone Apr 17, 2021
@tonyqus
Copy link
Member

tonyqus commented Apr 17, 2021

image

@mganss I followed your code. But I still cannot reproduce the issue with NPOI 2.5.2 release. Did I miss something?

@tonyqus tonyqus added question and removed bug labels Apr 17, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.3, NPOI 2.5.5 Apr 17, 2021
@mganss
Copy link
Author

mganss commented Apr 17, 2021

The CurrentCulture property needs to be set before the call to WorkbookFactory.Create().

@tonyqus
Copy link
Member

tonyqus commented Apr 18, 2021

@mganss Thank you for the tip. I can reproduce the issue now.

@tonyqus
Copy link
Member

tonyqus commented Apr 18, 2021

I found the root cause but it's very weird behavior of regular expression match.

The issue occurs in CellReference.SeparateRefParts method while handling a CT_Cell with r="I1" (Cell Address).

private static Regex CELL_REF_PATTERN = new Regex("(\\$?[A-Z]+)?" + "(\\$?[0-9]+)?", RegexOptions.IgnoreCase);

When CultureInfo is tr-TR, CELL_REF_PATTERN.Match partially works. Although Match.Success==true, the group values are still empty string instead of 'I' and '1'.

Source Link

@tonyqus
Copy link
Member

tonyqus commented Apr 18, 2021

https://stackoverflow.com/questions/3550213/in-c-sharp-what-is-the-difference-between-toupper-and-toupperinvariant

The first answer explains the issue. To fix this issue, RegexOptions.CultureInvariant shoud pass to the Regex constructor.

tonyqus added a commit that referenced this issue Apr 18, 2021
fix cell reference match issue when CultureInfo is tr-TR #470
@tonyqus tonyqus closed this as completed May 25, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.4 Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants