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

Implement GetCells interface for ISheet #1460

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenXmlFormats/NPOI.OpenXmlFormats.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0;netstandard2.1;net472;net8.0</TargetFrameworks>
<AssemblyName>NPOI.OpenXmlFormats</AssemblyName>
<RootNamespace>NPOI.OpenXmlFormats</RootNamespace>
<SignAssembly>true</SignAssembly>
Expand Down
6 changes: 6 additions & 0 deletions main/HSSF/UserModel/HSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ the License. You may obtain a copy of the License at
limitations Under the License.
==================================================================== */

using System.Collections.ObjectModel;

namespace NPOI.HSSF.UserModel
{
using System;
Expand Down Expand Up @@ -3394,5 +3396,9 @@ IEnumerator<IRow> IEnumerable<IRow>.GetEnumerator()
{
return rows.Values.GetEnumerator();
}
public CellRangeAddressList GetCells(string cellranges)
{
return CellRangeAddressList.Parse(cellranges);
}
}
}
8 changes: 3 additions & 5 deletions main/HSSF/Util/RangeAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ public int Width
{
return 0;
}
else
return toX - fromX + 1;
return toX - fromX + 1;
}
return 0;
}
Expand All @@ -199,8 +198,7 @@ public int Height
{
return 0;
}
else
return toY - fromY + 1;
return toY - fromY + 1;
}
return 0;
}
Expand Down Expand Up @@ -434,7 +432,7 @@ public int Get26Sys(String _s)
for (int i = _s.Length - 1; i >= 0; i--)
{
char ch = _s[i];
int val = (int)(Char.GetNumericValue(ch) - Char.GetNumericValue('A') + 1);
int val = (ch - 'A') + 1;
sum = sum + val * multiplier;
multiplier = multiplier * 26;
}
Expand Down
2 changes: 1 addition & 1 deletion main/NPOI.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0;netstandard2.1;net472;net8.0</TargetFrameworks>
<RootNamespace>NPOI</RootNamespace>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\npoi.snk</AssemblyOriginatorKeyFile>
Expand Down
5 changes: 4 additions & 1 deletion main/SS/UserModel/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ the License. You may obtain a copy of the License at
limitations under the License.
==================================================================== */

using System.Collections.ObjectModel;

namespace NPOI.SS.UserModel
{

Expand Down Expand Up @@ -950,7 +952,8 @@ bool Autobreaks
/// </summary>
CellAddress ActiveCell { get; set; }


void CopyTo(IWorkbook dest, string name, bool copyStyle, bool keepFormulas);

CellRangeAddressList GetCells(string cellranges);
}
}
67 changes: 42 additions & 25 deletions main/SS/Util/CellRangeAddressList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using NPOI.SS.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace NPOI.SS.Util
{
Expand All @@ -12,11 +15,11 @@ public class CellRangeAddressList
/**
* List of <c>CellRangeAddress</c>es. Each structure represents a cell range
*/
private ArrayList _list;
private List<CellRangeAddress> _list;

public CellRangeAddressList()
{
_list = new ArrayList();
_list = new List<CellRangeAddress>();
}
/**
* Convenience constructor for creating a <c>CellRangeAddressList</c> with a single
Expand All @@ -30,35 +33,56 @@ public CellRangeAddressList(int firstRow, int lastRow, int firstCol, int lastCol
}

/**
* @param in the RecordInputstream to read the record from
* @param in the RecordInputStream to read the record from
*/
public CellRangeAddressList(RecordInputStream in1)
internal CellRangeAddressList(RecordInputStream in1)
{
int nItems = in1.ReadUShort();
_list = new ArrayList(nItems);
_list = new List<CellRangeAddress>(nItems);

for (int k = 0; k < nItems; k++)
{
_list.Add(new CellRangeAddress(in1));
}
}

public static CellRangeAddressList Parse(string cellRanges)
{
if(string.IsNullOrWhiteSpace(cellRanges))
{
throw new ArgumentException("cell range cannot be null or empty");
}
var ranges = cellRanges.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var list = new CellRangeAddressList();
foreach(var range in ranges)
{
var ca = CellRangeAddress.ValueOf(range.Trim());
list.AddCellRangeAddress(ca);
}
return list;
}

/**
* Get the number of following ADDR structures. The number of this
* Get the number of following ADDR structures. The number of these
* structures is automatically set when reading an Excel file and/or
* increased when you manually Add a new ADDR structure . This is the reason
* there isn't a set method for this field .
*
*
* @return number of ADDR structures
*/
public int CountRanges()
{
return _list.Count;
}

public int NumberOfCells()
{
return _list.Sum(cr => cr.NumberOfCells);
}

/**
* Add a cell range structure.
*
*
* @param firstRow - the upper left hand corner's row
* @param firstCol - the upper left hand corner's col
* @param lastRow - the lower right hand corner's row
Expand Down Expand Up @@ -86,7 +110,7 @@ public CellRangeAddress Remove(int rangeIndex)
+ ") is outside allowable range (0.." + (_list.Count - 1) + ")");
}
CellRangeAddress cra = (CellRangeAddress)_list[rangeIndex];
_list.Remove(rangeIndex);
_list.Remove(_list[rangeIndex]);
return cra;
}

Expand All @@ -97,24 +121,24 @@ public CellRangeAddress GetCellRangeAddress(int index)
{
return (CellRangeAddress)_list[index];
}
public int Serialize(int offset, byte[] data)
internal int Serialize(int offset, byte[] data)
{
int totalSize = this.Size;
Serialize(new LittleEndianByteArrayOutputStream(data, offset, totalSize));
return totalSize;
}
public void Serialize(ILittleEndianOutput out1)
internal void Serialize(ILittleEndianOutput out1)
{
int nItems = _list.Count;
out1.WriteShort(nItems);
for (int k = 0; k < nItems; k++)
{
CellRangeAddress region = (CellRangeAddress)_list[k];
region.Serialize(out1);
region?.Serialize(out1);
}
}

public int Size
internal int Size
{
get
{
Expand All @@ -125,7 +149,7 @@ public int Size
* @return the total size of for the specified number of ranges,
* including the initial 2 byte range count
*/
public static int GetEncodedSize(int numberOfRanges)
internal static int GetEncodedSize(int numberOfRanges)
{
return 2 + CellRangeAddress.GetEncodedSize(numberOfRanges);
}
Expand All @@ -137,18 +161,11 @@ public CellRangeAddressList Copy()
for (int k = 0; k < nItems; k++)
{
CellRangeAddress region = (CellRangeAddress)_list[k];
result.AddCellRangeAddress(region.Copy());
if (region != null)
result.AddCellRangeAddress(region.Copy());
}
return result;
}
public CellRangeAddress[] CellRangeAddresses
{
get
{
CellRangeAddress[] result =
(CellRangeAddress[])_list.ToArray(typeof(CellRangeAddress));
return result;
}
}
public CellRangeAddress[] CellRangeAddresses => _list.ToArray();
}
}
4 changes: 2 additions & 2 deletions main/SS/Util/SSCellRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public K[][] Cells
itemCls = itemCls.GetElementType();
for (int r = _height - 1; r >= 0; r--)
{
K[] row = (K[])Array.CreateInstance(itemCls, _width);
result[r] = (K[])Array.CreateInstance(itemCls, _width);
int flatIndex = _width * r;
Array.Copy(_flattenedArray, flatIndex, row, 0, _width);
Array.Copy(_flattenedArray, flatIndex, result[r], 0, _width);
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion ooxml/NPOI.OOXML.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0;netstandard2.1;net472;net8.0</TargetFrameworks>
<AssemblyName>NPOI.OOXML</AssemblyName>
<RootNamespace>NPOI</RootNamespace>
<SignAssembly>true</SignAssembly>
Expand Down
6 changes: 6 additions & 0 deletions ooxml/XSSF/Streaming/SXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ the License. You may obtain a copy of the License at
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

using NPOI.HSSF.Util;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -1479,5 +1481,9 @@ IEnumerator<IRow> IEnumerable<IRow>.GetEnumerator()
{
return ((IEnumerable<IRow>) _sh).GetEnumerator();
}
public CellRangeAddressList GetCells(string cellranges)
{
return CellRangeAddressList.Parse(cellranges);
}
}
}
Loading
Loading