nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.74k stars 1.44k forks source link

fixed "Cannot add more than 65535 shapes" #1366

Closed htoyama closed 5 months ago

htoyama commented 5 months ago

NPOI Version

2.7.0

File Type

Upload the Excel File

Please attach your original Excel File to help us reproduce the issue

Reproduce Steps

Issue Description

Shape Id is int.

diff --git a/main/HSSF/UserModel/HSSFComment.cs b/main/HSSF/UserModel/HSSFComment.cs
index 5886c92a..ae9081f7 100644
--- a/main/HSSF/UserModel/HSSFComment.cs
+++ b/main/HSSF/UserModel/HSSFComment.cs
@@ -131,8 +131,8 @@ public override int ShapeId
             get { return base.ShapeId; }
             set
             {
-                if (value > 65535)
-                    throw new ArgumentException("Cannot add more than 65535 shapes");
+                if (value > Int32.MaxValue)
+                    throw new ArgumentException("Cannot add more than Int32.MaxValue shapes");
                 base.ShapeId = (value);
                 CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord)GetObjRecord().SubRecords[0];
                 cod.ObjectId = value;
tonyqus commented 5 months ago

It should be a limitation of xls files.

May I ask why do you need so many shapes?

htoyama commented 5 months ago

Shape id is not a sequential number. 1024 shape id per cluster. see also AllocateShapeId function.

The group is different, a new cluster will be assigned. cluseter=0, shape id=0 cluseter=1, shape id=1024 ... cluseter=65, shape id=65536 For one image per cluster, the upper limit is 64.

htoyama commented 5 months ago

book1.xls has a shape id greater than 65535. Workbook.CloneSheet("Sheet1") <-- New group id.(next cluster is 74, shape id=74 * 1024) workbook.Write(New_File)

Book1.xls

tonyqus commented 5 months ago

Sorry, I think I misunderstand you. If you are provide a fix for NPOI, please follow the following tutorial.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork

tonyqus commented 5 months ago

Btw, if you are not good at English (I see your name is a Japanese name), you can directly use Japanese. Github has translation feature or I can use Google translation instead although my Japanese is so poor.

htoyama commented 5 months ago

Hi, I'm not good at English.

Last Shape id is not equal number of shapes(images). Shape idとshape(image)の数は、一致しません。

The shape(image) limit is 65535, but Shape Id is int32. shape(image)の上限は、65535ですが、Shape Idは、int32です。

The attached patch can work around the exception, but it's maybe wrong. 添付のパッチは、例外を回避できますが、たぶん正しいものではありません。

Correctly, count the number of shapes(images). 正しくは、shape(image)の数をカウントしなければなりません。

tonyqus commented 5 months ago

結果は同じで実際の変更はないため、これを >65535 から >Int32.MaxValue に変更する必要はないと思います。(I use Google translation)

htoyama commented 5 months ago

添付のbook1.xls で例外が発生するので、修正する必要がある。

Staninbui commented 5 months ago

hi,@htoyamaさん, このissueをreportし、NPOIのavailibilityを検証するとのですか、それともbugの修復を含めcodeを貢献したいですか?tonyqusさんが今この問題について検討していますが、もし修復策があればぜひともcodeも含めPRしてください😀。

htoyama commented 5 months ago

パッチは、例外を回避するためのもので、良くありませんでした。 いつになるかお約束はできませんが、修正コードは作成するかもしれません。

70 shapes程度で、例外が発生する問題を共有できればと思っております。