diff --git a/main/HSSF/UserModel/HSSFSheet.cs b/main/HSSF/UserModel/HSSFSheet.cs index 893eae0f0..c04ef6780 100644 --- a/main/HSSF/UserModel/HSSFSheet.cs +++ b/main/HSSF/UserModel/HSSFSheet.cs @@ -3374,9 +3374,20 @@ IEnumerator IEnumerable.GetEnumerator() { return rows.Values.GetEnumerator(); } - public CellRangeAddressList GetCells(string cellranges) + + public NCellRange Cells { - return CellRangeAddressList.Parse(cellranges); + get + { + return new NCellRange(this, 0, 0, this.Workbook.SpreadsheetVersion.MaxRows, this.Workbook.SpreadsheetVersion.MaxColumns); + } + } + public NRowRange Rows + { + get + { + return new NRowRange(this, 0, this.Workbook.SpreadsheetVersion.MaxRows); + } } } } diff --git a/main/SS/UserModel/NCellRange.cs b/main/SS/UserModel/NCellRange.cs new file mode 100644 index 000000000..b18dc51fb --- /dev/null +++ b/main/SS/UserModel/NCellRange.cs @@ -0,0 +1,562 @@ +using NPOI.SS.Util; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace NPOI.SS.UserModel +{ + public class NCellRange: IEnumerable + { + private ISheet _sheet; + private CellRangeAddress _address; + + public int Width => _address.LastColumn - _address.FirstColumn+1; + + public int Height => _address.LastRow - _address.FirstRow+1; + + public int Size => _address.NumberOfCells; + + public string Address => _address.FormatAsString(); + public ICell TopLeftCell => GetCell(0,0); + public ISheet Sheet => _sheet; + + /// + /// Set this cell range as active + /// + /// + public NCellRange SetActive() + { + _sheet.SetActiveCellRange(_address.FirstRow, _address.LastRow, _address.FirstColumn, _address.LastColumn); + return this; + } + + public string Formula { get => throw new NotImplementedException(); set => this.SetCellFormula(value); } + + public NCellRange SetCellStyle(ICellStyle style, bool createMissingRowAndCol) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.CellStyle=style; + } + } + return this; + } + + public void SetCellComment(IComment comment) { + if(comment==null) + { + RemoveCellComment(); + return; + } + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + comment.SetAddress(i, j); + } + } + } + public NCellRange SetHyperlink(IHyperlink hyperlink, bool createMissingRowAndCol=true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.Hyperlink = hyperlink; + } + } + return this; + } + + public NCellRange(ISheet sheet, int fromRow, int fromCol, int toRow, int toCol) + { + _sheet = sheet; + validateRowCol(fromRow, fromCol); + validateRowCol(toRow, toCol); + _address = new CellRangeAddress(fromRow, toRow, fromCol, toCol); + } + private void validateRowCol(int row, int col) + { + if(row<0||row>_sheet.Workbook.SpreadsheetVersion.MaxRows) + throw new ArgumentException($"row index {row} is out of range"); + if(col<0||col>_sheet.Workbook.SpreadsheetVersion.MaxColumns) + throw new ArgumentException($"column index {col} is out of range"); + } + + public ICell GetCell(int rowInRange, int colInRange) + { + var row = _sheet.GetRow(_address.FirstRow+rowInRange); + if(row==null) + row = _sheet.CreateRow(_address.FirstRow+rowInRange); + return row.GetCell(_address.FirstColumn+colInRange, MissingCellPolicy.CREATE_NULL_AS_BLANK); + } + + public List Cells + { + get { + List cells = new List(); + for(int i = _address.FirstRow; i<=((_address.LastRow<_sheet.LastRowNum)?_address.LastRow: _sheet.LastRowNum); i++) + { + var row = _sheet.GetRow(i); + if(row==null) + continue; + for(int j = _address.FirstColumn; j<=((_address.LastColumn GetEnumerator() + { + return Cells.GetEnumerator(); + } + + public NCellRange SetCellType(CellType cellType, bool createMissingRowAndCol= true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellType(cellType); + } + } + return this; + } + + public NCellRange SetBlank(bool createMissingRowAndCol = false) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetBlank(); + } + } + return this; + } + + public NCellRange SetCellValue(double value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellValue(value); + } + } + return this; + } + + public NCellRange SetCellErrorValue(byte value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + + cell = row.CreateCell(j); + cell.SetCellErrorValue(value); + } + } + return this; + } + + public NCellRange SetCellValue(DateTime value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellValue(value); + } + } + return this; + } + + public NCellRange SetCellValue(IRichTextString value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellValue(value); + } + } + return this; + } + + public NCellRange SetCellValue(string value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellValue(value); + } + } + return this; + } + + public NCellRange RemoveFormula() + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + continue; + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j); + if(cell!=null&&cell.CellFormula!=null) + { + cell.RemoveFormula(); + } + } + } + return this; + } + + public NCellRange SetCellFormula(string formula, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.RETURN_NULL_AND_BLANK); + if(cell==null) + { + if(!createMissingRowAndCol) + continue; + else + cell = row.CreateCell(j); + } + cell.SetCellFormula(formula); + } + } + return this; + } + + public NCellRange SetCellValue(bool value, bool createMissingRowAndCol = true) + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + if(!createMissingRowAndCol) + continue; + else + row = _sheet.CreateRow(i); + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.CREATE_NULL_AS_BLANK); + if(cell==null&&!createMissingRowAndCol) + continue; + + cell = row.CreateCell(j); + cell.SetCellValue(value); + } + } + return this; + } + public string[][] Texts + { + get + { + string[][] texts= new string[Height][]; + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + bool emptyRow = false; + var row=_sheet.GetRow(i); + if(row==null) + { + emptyRow=true; + } + texts[i-_address.FirstRow]=new string[Width]; + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + if(emptyRow) + { + texts[i-_address.FirstRow][j-_address.FirstColumn]=null; + break; + } + var c=row.GetCell(j); + if(c==null) + { + texts[i-_address.FirstRow][j-_address.FirstColumn]=null; + } + else + { + texts[i-_address.FirstRow][j-_address.FirstColumn]=c.ToString(); + } + } + } + return texts; + } + } + public object Value { + set { + if(value is double || value is Double) + { + SetCellValue((double) value); + return; + } + else if(value is string || value is String) + { + SetCellValue((string) value); + return; + } + else if(value is bool || value is Boolean) + { + SetCellValue((bool) value); + return; + } + else if(value is DateTime) + { + SetCellValue((DateTime) value); + return; + } + else if(value is IRichTextString) + { + SetCellValue((IRichTextString) value); + return; + } + throw new InvalidOperationException("invalid value type for cell value"); + } + } + public NCellRange RemoveCellComment() + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + continue; + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j,MissingCellPolicy.CREATE_NULL_AS_BLANK); + if(cell.CellComment!=null) + { + cell.RemoveCellComment(); + } + } + } + return this; + } + + public NCellRange RemoveHyperlink() + { + for(int i = _address.FirstRow; i<=_address.LastRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + { + continue; + } + for(int j = _address.FirstColumn; j<=_address.LastColumn; j++) + { + var cell = row.GetCell(j); + if(cell==null) + continue; + if(cell.Hyperlink!=null) + { + cell.RemoveHyperlink(); + } + } + } + return this; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public NCellRange this[string address] { + get { + _address = CellRangeAddress.ValueOf(address); + return this; + } + } + + public NCellRange this[int row, int col] + { + get + { + validateRowCol(row, col); + _address.FirstRow = row; + _address.LastRow = row; + _address.FirstColumn = col; + _address.LastColumn = col; + return this; + } + } + } +} diff --git a/main/SS/UserModel/NRowRange.cs b/main/SS/UserModel/NRowRange.cs new file mode 100644 index 000000000..6e6a47dd2 --- /dev/null +++ b/main/SS/UserModel/NRowRange.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace NPOI.SS.UserModel +{ + public class NRowRange:IEnumerable + { + private int _fromRow; + private int _toRow; + private ISheet _sheet; + + public NRowRange(ISheet sheet, int fromRow, int toRow) + { + _fromRow=fromRow; + _toRow=toRow; + _sheet=sheet; + } + + private void validateRow(int row) + { + if(row<0||row>_sheet.Workbook.SpreadsheetVersion.MaxRows) + throw new ArgumentException($"row index {row} is out of range"); + } + + public IEnumerator GetEnumerator() + { + return Rows.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public int NumberOfRows { get => _toRow-_fromRow+1; } + + public int PhysicalNumberOfRows { get => this.Rows.Count; } + + public short Height + { + get=> throw new NotImplementedException(); + set { + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + row=_sheet.CreateRow(i); + row.Height=value; + } + } + } + public float HeightInPoints + { + get => throw new NotImplementedException(); + set + { + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + row=_sheet.CreateRow(i); + row.HeightInPoints=value; + } + } + } + public bool ZeroHeight + { + get => throw new NotImplementedException(); + set + { + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + row=_sheet.CreateRow(i); + row.ZeroHeight=value; + } + } + } + public ICellStyle RowStyle + { + get => throw new NotImplementedException(); + set + { + if(RowStyle==null) + return; + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + row=_sheet.CreateRow(i); + row.RowStyle=value; + } + } + } + public NRowRange Group() + { + _sheet.GroupRow(_fromRow, _toRow); + return this; + } + public NRowRange Ungroup() + { + _sheet.UngroupRow(_fromRow, _toRow); + return this; + } + + public List Rows + { + get + { + var rows= new List(); + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + continue; + rows.Add(row); + } + return rows; + } + } + public NRowRange this[int fromRow, int toRow] + { + get + { + validateRow(fromRow); + validateRow(toRow); + _fromRow = fromRow; + _toRow = toRow; + return this; + } + } + public void Create() + { + for(int i = _fromRow; i<=_toRow; i++) + { + var row = _sheet.GetRow(i); + if(row==null) + row=_sheet.CreateRow(i); + } + } + + public IRow GetRow(int relativeRow) + { + validateRow(_fromRow+relativeRow); + return _sheet.GetRow(_fromRow+relativeRow); + } + } +} diff --git a/main/SS/UserModel/Sheet.cs b/main/SS/UserModel/Sheet.cs index d183fa5c8..3c518dcd3 100644 --- a/main/SS/UserModel/Sheet.cs +++ b/main/SS/UserModel/Sheet.cs @@ -86,7 +86,6 @@ public enum PanePosition : byte /// public interface ISheet : IEnumerable { - /// /// Create a new row within the sheet and return the high level representation /// @@ -297,15 +296,9 @@ public interface ISheet : IEnumerable /// Call on each row /// if you care which one it is. /// + [Obsolete("Use ISheet.GetEnumerator() instead")] IEnumerator GetRowEnumerator(); - - /// - /// Get the row enumerator - /// - /// - IEnumerator GetEnumerator(); - /// /// Gets the flag indicating whether the window should show 0 (zero) in cells Containing zero value. /// When false, cells with zero value appear blank instead of showing the number zero. @@ -937,7 +930,8 @@ bool Autobreaks CellAddress ActiveCell { get; set; } void CopyTo(IWorkbook dest, string name, bool copyStyle, bool keepFormulas); - - CellRangeAddressList GetCells(string cellranges); + + NCellRange Cells { get; } + NRowRange Rows { get; } } } diff --git a/ooxml/XSSF/Streaming/SXSSFRow.cs b/ooxml/XSSF/Streaming/SXSSFRow.cs index 18b4d0ded..10f92bc42 100644 --- a/ooxml/XSSF/Streaming/SXSSFRow.cs +++ b/ooxml/XSSF/Streaming/SXSSFRow.cs @@ -370,7 +370,7 @@ public int GetCellIndex(SXSSFCell cell) IEnumerator IEnumerable.GetEnumerator() { - throw new NotImplementedException(); + return this.GetEnumerator(); } /** diff --git a/ooxml/XSSF/Streaming/SXSSFSheet.cs b/ooxml/XSSF/Streaming/SXSSFSheet.cs index 2d4fedca3..5446dcf42 100644 --- a/ooxml/XSSF/Streaming/SXSSFSheet.cs +++ b/ooxml/XSSF/Streaming/SXSSFSheet.cs @@ -786,11 +786,6 @@ public List GetDataValidations() return _sh.GetDataValidations(); } - public IEnumerator GetEnumerator() - { - return (IEnumerator)new SortedDictionary(_rows).Values.GetEnumerator(); - } - public double GetMargin(MarginType margin) { return _sh.GetMargin(margin); @@ -811,7 +806,7 @@ public IRow GetRow(int rownum) public IEnumerator GetRowEnumerator() { - return GetEnumerator(); + return _rows.GetEnumerator(); } public void GroupColumn(int fromColumn, int toColumn) @@ -1446,6 +1441,7 @@ public XSSFColor TabColor _sh.TabColor = value; } } + public void CopyTo(IWorkbook dest, string name, bool copyStyle, bool keepFormulas) { throw new NotImplementedException(); @@ -1660,11 +1656,27 @@ public void SetTabColor(int colorIndex) IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable) _sh).GetEnumerator(); + return _rows.Values.GetEnumerator(); + } + + public IEnumerator GetEnumerator() + { + return _rows.Values.GetEnumerator(); } - public CellRangeAddressList GetCells(string cellranges) + + public NCellRange Cells { - return CellRangeAddressList.Parse(cellranges); + get + { + return new NCellRange(this, 0, 0, this.Workbook.SpreadsheetVersion.MaxRows, this.Workbook.SpreadsheetVersion.MaxColumns); + } + } + public NRowRange Rows + { + get + { + return new NRowRange(this, 0, this.Workbook.SpreadsheetVersion.MaxRows); + } } } } diff --git a/ooxml/XSSF/UserModel/XSSFRow.cs b/ooxml/XSSF/UserModel/XSSFRow.cs index c2e2e0f41..c03285c19 100644 --- a/ooxml/XSSF/UserModel/XSSFRow.cs +++ b/ooxml/XSSF/UserModel/XSSFRow.cs @@ -518,7 +518,7 @@ public void CopyRowFrom(IRow srcRow, CellCopyPolicy policy) /// public void SetRowStyle(ICellStyle style) { - + this.RowStyle = style; } /// diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index d28e74ebc..acb92a6e3 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -3839,7 +3839,11 @@ public void SetActiveCellRange(List cellranges, int active public void SetActiveCellRange(int firstRow, int lastRow, int firstColumn, int lastColumn) { - throw new NotImplementedException(); + var ctSelection = this.GetSheetTypeSelection(); + var address = new CellRangeAddress(firstRow,lastRow, firstColumn, lastColumn); + string reference= address.FormatAsString(); + ctSelection.activeCell = reference; + ctSelection.sqref = reference; } public short TabColorIndex @@ -6608,11 +6612,6 @@ int x #endregion - public CellRangeAddressList GetCells(string cellranges) - { - return CellRangeAddressList.Parse(cellranges); - } - /// /// called when a sheet is being deleted/removed from a workbook, to clean up relations and other document pieces tied to the sheet /// @@ -6626,5 +6625,19 @@ internal void OnSheetDelete() { RemoveRelation(part.DocumentPart, true); } } + + public NCellRange Cells + { + get { + return new NCellRange(this, 0, 0, this.Workbook.SpreadsheetVersion.MaxRows, this.Workbook.SpreadsheetVersion.MaxColumns); + } + } + + public NRowRange Rows + { + get { + return new NRowRange(this, 0, this.Workbook.SpreadsheetVersion.MaxRows); + } + } } } diff --git a/testcases/main/SS/UserModel/BaseTestSheet.cs b/testcases/main/SS/UserModel/BaseTestSheet.cs index d921c55c9..f79f3f611 100644 --- a/testcases/main/SS/UserModel/BaseTestSheet.cs +++ b/testcases/main/SS/UserModel/BaseTestSheet.cs @@ -59,7 +59,7 @@ public void TestCreateRow() IRow row2 = sheet.CreateRow(1); ClassicAssert.AreEqual(0, row1.RowNum); ClassicAssert.AreEqual(1, row2.RowNum); - IEnumerator it = sheet.GetRowEnumerator(); + var it = sheet.GetEnumerator(); ClassicAssert.IsTrue(it.MoveNext()); ClassicAssert.AreSame(row1, it.Current); ClassicAssert.IsTrue(it.MoveNext()); @@ -76,11 +76,11 @@ public void TestCreateRow() IRow row2_ovrewritten = sheet.CreateRow(1); ICell cell = row2_ovrewritten.CreateCell(0); cell.SetCellValue(100); - IEnumerator it2 = sheet.GetRowEnumerator(); + var it2 = sheet.GetEnumerator(); ClassicAssert.IsTrue(it2.MoveNext()); ClassicAssert.AreSame(row1, it2.Current); ClassicAssert.IsTrue(it2.MoveNext()); - IRow row2_ovrewritten_ref = (IRow)it2.Current; + IRow row2_ovrewritten_ref = it2.Current; ClassicAssert.AreSame(row2_ovrewritten, row2_ovrewritten_ref); ClassicAssert.AreEqual(100.0, row2_ovrewritten_ref.GetCell(0).NumericCellValue, 0.0); @@ -118,7 +118,8 @@ public void CreateRowAfterLastRow() sh.CreateRow(version.LastRowIndex); try { - Assert.Throws(()=> { + Assert.Throws(() => + { sh.CreateRow(version.LastRowIndex + 1); }); } @@ -193,11 +194,11 @@ public virtual void CloneSheet() //Check that the cells are not somehow linked cell.SetCellValue(factory.CreateRichTextString("Difference Check")); cell2.CellFormula = (/*setter*/"cos(2)"); - if ("Difference Check".Equals(clonedRow.GetCell(0).RichStringCellValue.String)) + if("Difference Check".Equals(clonedRow.GetCell(0).RichStringCellValue.String)) { Assert.Fail("string cell not properly Cloned"); } - if ("COS(2)".Equals(clonedRow.GetCell(1).CellFormula)) + if("COS(2)".Equals(clonedRow.GetCell(1).CellFormula)) { Assert.Fail("formula cell not properly Cloned"); } @@ -256,7 +257,7 @@ public void TestPrintSetupLandscapeNew() // Change one on each sheetL.PrintSetup.Landscape = (/*setter*/true); - sheetP.PrintSetup.Copies = (/*setter*/(short)3); + sheetP.PrintSetup.Copies = (/*setter*/(short) 3); // Check taken ClassicAssert.IsTrue(sheetL.PrintSetup.Landscape); @@ -300,9 +301,9 @@ public void AddOverlappingMergedRegions() Assert.Fail("Should not be able to add a merged region (" + duplicateRegion.FormatAsString() + ") " + "if sheet already contains the same merged region (" + baseRegion.FormatAsString() + ")"); } - catch (InvalidOperationException) + catch(InvalidOperationException) { - } + } try { @@ -311,9 +312,9 @@ public void AddOverlappingMergedRegions() Assert.Fail("Should not be able to add a merged region (" + partiallyOverlappingRegion.FormatAsString() + ") " + "if it partially overlaps with an existing merged region (" + baseRegion.FormatAsString() + ")"); } - catch (InvalidOperationException) + catch(InvalidOperationException) { - } + } try { @@ -322,9 +323,9 @@ public void AddOverlappingMergedRegions() Assert.Fail("Should not be able to add a merged region (" + subsetRegion.FormatAsString() + ") " + "if it is a formal subset of an existing merged region (" + baseRegion.FormatAsString() + ")"); } - catch (InvalidOperationException) + catch(InvalidOperationException) { - } + } try { @@ -333,7 +334,7 @@ public void AddOverlappingMergedRegions() Assert.Fail("Should not be able to add a merged region (" + supersetRegion.FormatAsString() + ") " + "if it is a formal superset of an existing merged region (" + baseRegion.FormatAsString() + ")"); } - catch (InvalidOperationException) + catch(InvalidOperationException) { } @@ -357,7 +358,7 @@ public void AddMergedRegionWithSingleCellShouldFail() sheet.AddMergedRegion(region); Assert.Fail("Should not be able to add a single-cell merged region (" + region.FormatAsString() + ")"); } - catch (ArgumentException) + catch(ArgumentException) { // expected } @@ -388,7 +389,7 @@ public void TestAddMerged() sheet.AddMergedRegion(region); Assert.Fail("Expected exception"); } - catch (ArgumentException) + catch(ArgumentException) { // TODO ClassicAssert.AreEqual("Minimum row number is 0.", e.Message); } @@ -398,7 +399,7 @@ public void TestAddMerged() sheet.AddMergedRegion(region); Assert.Fail("Expected exception"); } - catch (ArgumentException e) + catch(ArgumentException e) { ClassicAssert.AreEqual("Maximum column number is " + ssVersion.LastColumnIndex, e.Message); } @@ -408,7 +409,7 @@ public void TestAddMerged() sheet.AddMergedRegion(region); Assert.Fail("Expected exception"); } - catch (ArgumentException e) + catch(ArgumentException e) { ClassicAssert.AreEqual("Maximum row number is " + ssVersion.LastRowIndex, e.Message); } @@ -469,7 +470,7 @@ public void RemoveMergedRegions() ISheet sheet = wb.CreateSheet(); Dictionary mergedRegions = new Dictionary(); - for (int r = 0; r < 10; r++) + for(int r = 0; r < 10; r++) { CellRangeAddress region = new CellRangeAddress(r, r, 0, 1); mergedRegions.Add(r, region); @@ -479,7 +480,7 @@ public void RemoveMergedRegions() assertCollectionEquals(mergedRegions.Values.ToList(), sheet.MergedRegions); IList removed = new List { 0, 2, 3, 6, 8 }; - foreach (int rx in removed) + foreach(int rx in removed) mergedRegions.Remove(rx); sheet.RemoveMergedRegions(removed); assertCollectionEquals(mergedRegions.Values.ToList(), sheet.MergedRegions); @@ -553,7 +554,7 @@ public void AddMergedRegionUnsafe() sh.AddMergedRegion(region3); Assert.Fail("Expected InvalidOperationException. region3 overlaps already added merged region2."); } - catch (InvalidOperationException) + catch(InvalidOperationException) { // expected ClassicAssert.IsFalse(sh.MergedRegions.Contains(region3)); @@ -566,7 +567,7 @@ public void AddMergedRegionUnsafe() sh.ValidateMergedRegions(); Assert.Fail("Expected validation to Assert.Fail. Sheet contains merged regions A1:B2 and B2:C3, which overlap at B2."); } - catch (InvalidOperationException) + catch(InvalidOperationException) { // expected } @@ -619,7 +620,7 @@ public void TestColumnWidth() ClassicAssert.AreEqual(256 * 10, sheet.GetColumnWidth(2)); //set custom width for D-F - for (char i = 'D'; i <= 'F'; i++) + for(char i = 'D'; i <= 'F'; i++) { //Sheet#setColumnWidth accepts the width in units of 1/256th of a character width int w = 256 * 12; @@ -632,7 +633,7 @@ public void TestColumnWidth() ClassicAssert.AreEqual(256 * 20, sheet.GetColumnWidth(0)); ClassicAssert.AreEqual(256 * 20, sheet.GetColumnWidth(1)); ClassicAssert.AreEqual(256 * 20, sheet.GetColumnWidth(2)); - for (char i = 'D'; i <= 'F'; i++) + for(char i = 'D'; i <= 'F'; i++) { int w = 256 * 12; ClassicAssert.AreEqual(w, sheet.GetColumnWidth(i)); @@ -648,7 +649,7 @@ public void TestColumnWidth() sheet.SetColumnWidth(/*setter*/9, 256 * 256); Assert.Fail("expected exception"); } - catch (ArgumentException e) + catch(ArgumentException e) { ClassicAssert.AreEqual("The maximum column width for an individual cell is 255 characters.", e.Message); } @@ -664,7 +665,7 @@ public void TestColumnWidth() ClassicAssert.AreEqual(256 * 20, sheet.GetColumnWidth(1)); ClassicAssert.AreEqual(256 * 20, sheet.GetColumnWidth(2)); //columns D-F have custom width - for (char i = 'D'; i <= 'F'; i++) + for(char i = 'D'; i <= 'F'; i++) { short w = (256 * 12); ClassicAssert.AreEqual(w, sheet.GetColumnWidth(i)); @@ -679,7 +680,7 @@ public void TestDefaultRowHeight() IWorkbook workbook = _testDataProvider.CreateWorkbook(); ISheet sheet = workbook.CreateSheet(); sheet.DefaultRowHeightInPoints = (/*setter*/15); - ClassicAssert.AreEqual((short)300, sheet.DefaultRowHeight); + ClassicAssert.AreEqual((short) 300, sheet.DefaultRowHeight); ClassicAssert.AreEqual(15.0F, sheet.DefaultRowHeightInPoints, 0.01F); IRow row = sheet.CreateRow(1); @@ -687,19 +688,19 @@ public void TestDefaultRowHeight() ClassicAssert.AreEqual(sheet.DefaultRowHeight, row.Height); // Set a new default row height in twips and Test Getting the value in points - sheet.DefaultRowHeight = (/*setter*/(short)360); + sheet.DefaultRowHeight = (/*setter*/(short) 360); ClassicAssert.AreEqual(18.0f, sheet.DefaultRowHeightInPoints, 0.01F); - ClassicAssert.AreEqual((short)360, sheet.DefaultRowHeight); + ClassicAssert.AreEqual((short) 360, sheet.DefaultRowHeight); // Test that defaultRowHeight is a tRuncated short: E.G. 360inPoints -> 18; 361inPoints -> 18 - sheet.DefaultRowHeight = (/*setter*/(short)361); - ClassicAssert.AreEqual((float)361 / 20, sheet.DefaultRowHeightInPoints, 0.01F); - ClassicAssert.AreEqual((short)361, sheet.DefaultRowHeight); + sheet.DefaultRowHeight = (/*setter*/(short) 361); + ClassicAssert.AreEqual((float) 361 / 20, sheet.DefaultRowHeightInPoints, 0.01F); + ClassicAssert.AreEqual((short) 361, sheet.DefaultRowHeight); // Set a new default row height in points and Test Getting the value in twips sheet.DefaultRowHeightInPoints = (/*setter*/17.5f); ClassicAssert.AreEqual(17.5f, sheet.DefaultRowHeightInPoints, 0.01F); - ClassicAssert.AreEqual((short)(17.5f * 20), sheet.DefaultRowHeight); + ClassicAssert.AreEqual((short) (17.5f * 20), sheet.DefaultRowHeight); workbook.Close(); } @@ -866,10 +867,10 @@ public void BaseTestGetSetMargin(double[] defaultMargins) // incorrect margin constant try { - sheet.SetMargin((MarginType)65, 15); + sheet.SetMargin((MarginType) 65, 15); Assert.Fail("Expected exception"); } - catch (InvalidOperationException e) + catch(InvalidOperationException e) { ClassicAssert.AreEqual("Unknown margin constant: 65", e.Message); } @@ -925,11 +926,11 @@ public void TestColumnBreaks() ClassicAssert.IsTrue(sheet.IsColumnBroken(11)); ClassicAssert.IsTrue(sheet.IsColumnBroken(12)); - sheet.RemoveColumnBreak((short)11); + sheet.RemoveColumnBreak((short) 11); ClassicAssert.AreEqual(1, sheet.ColumnBreaks.Length); - sheet.RemoveColumnBreak((short)15); //remove non-existing + sheet.RemoveColumnBreak((short) 15); //remove non-existing ClassicAssert.AreEqual(1, sheet.ColumnBreaks.Length); - sheet.RemoveColumnBreak((short)12); + sheet.RemoveColumnBreak((short) 12); ClassicAssert.AreEqual(0, sheet.ColumnBreaks.Length); ClassicAssert.IsFalse(sheet.IsColumnBroken(11)); @@ -1111,7 +1112,7 @@ public void TestSetRepeatingRowsAndColumns() private void CheckRepeatingRowsAndColumns( ISheet s, String expectedRows, String expectedCols) { - if (expectedRows == null) + if(expectedRows == null) { ClassicAssert.IsNull(s.RepeatingRows); } @@ -1119,7 +1120,7 @@ private void CheckRepeatingRowsAndColumns( { ClassicAssert.AreEqual(expectedRows, s.RepeatingRows.FormatAsString()); } - if (expectedCols == null) + if(expectedCols == null) { ClassicAssert.IsNull(s.RepeatingColumns); } @@ -1259,11 +1260,11 @@ public void GetCellComments() int nRows = 5; int nCols = 6; - for (int r = 0; r < nRows; r++) + for(int r = 0; r < nRows; r++) { sheet.CreateRow(r); // Create columns in reverse order - for (int c = nCols - 1; c >= 0; c--) + for(int c = nCols - 1; c >= 0; c--) { // When the comment box is visible, have it show in a 1x3 space anchor.Col1 = c; @@ -1289,7 +1290,7 @@ public void GetCellComments() Dictionary cellComments = sh.GetCellComments(); ClassicAssert.AreEqual(nRows * nCols, cellComments.Count); - foreach (KeyValuePair e in cellComments) + foreach(KeyValuePair e in cellComments) { CellAddress ref1 = e.Key; IComment aComment = e.Value; @@ -1326,7 +1327,7 @@ public void GetHyperlink() } [Test] - public void RemoveAllHyperlinks() + public void RemoveAllHyperlinks() { IWorkbook workbook = _testDataProvider.CreateWorkbook(); IHyperlink hyperlink = workbook.GetCreationHelper().CreateHyperlink(HyperlinkType.Url); @@ -1339,11 +1340,11 @@ public void RemoveAllHyperlinks() // Save a workbook with a hyperlink IWorkbook workbook2 = _testDataProvider.WriteOutAndReadBack(workbook); ClassicAssert.AreEqual(1, workbook2.GetSheetAt(0).GetHyperlinkList().Count); - + // Remove all hyperlinks from a saved workbook workbook2.GetSheetAt(0).GetRow(5).GetCell(1).RemoveHyperlink(); ClassicAssert.AreEqual(0, workbook2.GetSheetAt(0).GetHyperlinkList().Count); - + // Verify that hyperlink was removed from workbook after writing out IWorkbook workbook3 = _testDataProvider.WriteOutAndReadBack(workbook2); ClassicAssert.AreEqual(0, workbook3.GetSheetAt(0).GetHyperlinkList().Count); @@ -1372,7 +1373,7 @@ public void ShowInPaneManyRowsBug55248() sheet.ShowInPane(0, 0); int i; - for (i = ROW_COUNT / 2; i < ROW_COUNT; i++) + for(i = ROW_COUNT / 2; i < ROW_COUNT; i++) { sheet.CreateRow(i); sheet.ShowInPane(i, 0); @@ -1418,7 +1419,7 @@ public void TestNoMergedRegionsIsEmptyList() { IWorkbook wb = _testDataProvider.CreateWorkbook(); ISheet sheet = wb.CreateSheet(); - ClassicAssert.IsTrue(sheet.MergedRegions.Count ==0 ); + ClassicAssert.IsTrue(sheet.MergedRegions.Count ==0); wb.Close(); } [Test] @@ -1441,7 +1442,7 @@ public void SetActiveCell() // active cell behavior is undefined if not set. // HSSFSheet defaults to A1 active cell, while XSSFSheet defaults to null. - if (sheet.ActiveCell != null && !sheet.ActiveCell.Equals(CellAddress.A1)) + if(sheet.ActiveCell != null && !sheet.ActiveCell.Equals(CellAddress.A1)) { Assert.Fail("If not set, active cell should default to null or A1"); } @@ -1496,32 +1497,152 @@ public void TestGetCells_SingleCell() { var wb1 = _testDataProvider.CreateWorkbook(); var sheet = wb1.CreateSheet(); - var cellRanges= sheet.GetCells("A1"); - ClassicAssert.AreEqual(1, cellRanges.CountRanges()); + var cellRanges= sheet.Cells["A1"]; + cellRanges.SetCellValue(1.0); + ClassicAssert.AreEqual(1, cellRanges.Size); + ClassicAssert.AreEqual("A1", cellRanges.Address); + ClassicAssert.AreEqual(0, cellRanges.TopLeftCell.RowIndex); + ClassicAssert.AreEqual(0, cellRanges.TopLeftCell.ColumnIndex); + ClassicAssert.AreEqual(1.0, cellRanges.TopLeftCell.NumericCellValue); } [Test] - public void TestGetCells_MultipleCellRange() + public void TestGetCells_SingleCell_UseRowColIndexer() { var wb1 = _testDataProvider.CreateWorkbook(); var sheet = wb1.CreateSheet(); - var cellRanges = sheet.GetCells("A1:B2, D5:F7"); - ClassicAssert.AreEqual(2, cellRanges.CountRanges()); - ClassicAssert.AreEqual(4+9, cellRanges.NumberOfCells()); + var cellRanges = sheet.Cells[0,5]; + + ClassicAssert.AreEqual(1, cellRanges.Size); + ClassicAssert.AreEqual("F1", cellRanges.Address); + ClassicAssert.AreEqual(0, cellRanges.TopLeftCell.RowIndex); + ClassicAssert.AreEqual(5, cellRanges.TopLeftCell.ColumnIndex); } + [Test] - public void TestGetCells_SingleCellRange() + public void TestGetCells_SingleCellRange_SetBlank() { var wb1 = _testDataProvider.CreateWorkbook(); var sheet = wb1.CreateSheet(); - var cellRanges = sheet.GetCells("Sheet1!B1:D3"); - ClassicAssert.AreEqual(1, cellRanges.CountRanges()); - ClassicAssert.AreEqual(1, cellRanges.GetCellRangeAddress(0).FirstColumn); - ClassicAssert.AreEqual(3, cellRanges.GetCellRangeAddress(0).LastColumn); - ClassicAssert.AreEqual(0, cellRanges.GetCellRangeAddress(0).FirstRow); - ClassicAssert.AreEqual(2, cellRanges.GetCellRangeAddress(0).LastRow); - ClassicAssert.AreEqual(9, cellRanges.GetCellRangeAddress(0).NumberOfCells); - } - } + var cellRanges = sheet.Cells["D5:F6"]; + ClassicAssert.AreEqual(3, cellRanges.Width); + ClassicAssert.AreEqual(2, cellRanges.Height); + ClassicAssert.AreEqual(6, cellRanges.Size); + + sheet.CreateRow(5).CreateCell(5).SetCellValue(1.0); //Set F5 value + + cellRanges.SetBlank(false); //don't create row and cells + ClassicAssert.AreEqual(1, cellRanges.Cells.Count); + cellRanges.SetBlank(true); //Create row and cells + ClassicAssert.AreEqual(6, cellRanges.Cells.Count); + foreach(var cell in cellRanges) + { + ClassicAssert.AreEqual(CellType.Blank, cell.CellType); + ClassicAssert.AreEqual("", cell.ToString()); + } + } + [Test] + public void TestGetCells_SingleCellRange_SetCellValue() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + var cellRanges = sheet.Cells["Sheet1!B1:D3"]; + ClassicAssert.AreEqual(9, cellRanges.Size); + cellRanges.SetCellValue("Hello"); + + foreach(var cell in cellRanges) + { + ClassicAssert.AreEqual("Hello", cell.StringCellValue); + } + var C2 = cellRanges.GetCell(1, 1); //get relative coordinate of B1:D3 range + ClassicAssert.AreEqual(1, C2.RowIndex); + ClassicAssert.AreEqual(2, C2.ColumnIndex); + ClassicAssert.AreEqual("C2", C2.Address.FormatAsString()); + } + [Test] + public void TestGetCells_SingleCellRange_SetCellValueWithValueProperty() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + var cellRanges = sheet.Cells["Sheet1!B1:D3"]; + ClassicAssert.AreEqual(9, cellRanges.Size); + cellRanges.Value = "Hello"; + + foreach(var cell in cellRanges) + { + ClassicAssert.AreEqual("Hello", cell.StringCellValue); + } + } + [Test] + public void TestGetCells_SingleCellRange_SetCellFormula() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + var cellRanges = sheet.Cells["Sheet1!B1:D3"]; + ClassicAssert.AreEqual(9, cellRanges.Size); + cellRanges.Formula="A1+B1"; + + foreach(var cell in cellRanges) + { + ClassicAssert.AreEqual("A1+B1", cell.CellFormula); + } + } + [Test] + public void TestGetCells_SingleCellRange_ColumnSelector() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + var cellRanges = sheet.Cells["A:B"]; + ClassicAssert.AreEqual(2, cellRanges.Size); + } + + [Test] + public void TestGetRows_BasicTest() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + + Assert.Throws(() => + { + var x=sheet.Rows[-1, 100]; + }); + Assert.Throws(() => + { + var x=sheet.Rows[1, 100000000]; + }); + var rowRange = sheet.Rows[1, 5]; + ClassicAssert.AreEqual(5, rowRange.NumberOfRows); + ClassicAssert.AreEqual(0, rowRange.PhysicalNumberOfRows); + ClassicAssert.IsNull(rowRange.GetRow(0)); + rowRange.Create(); //create rows 1-5 + ClassicAssert.AreEqual(5, rowRange.PhysicalNumberOfRows); + ClassicAssert.AreEqual(1, rowRange.GetRow(0).RowNum); + ClassicAssert.AreEqual(3, rowRange.GetRow(2).RowNum); + ClassicAssert.AreEqual(5, rowRange.GetRow(4).RowNum); + } + [Test] + public void TestGetRows_SetHeight() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + var rowRange=sheet.Rows[1, 3]; + rowRange.Height = 500; + foreach(var row in rowRange) + { + ClassicAssert.AreEqual(500, row.Height); + ClassicAssert.AreEqual(25, row.HeightInPoints); + } + } + [Test] + public void TestGetRows_ExistingRow() + { + var wb1 = _testDataProvider.CreateWorkbook(); + var sheet = wb1.CreateSheet(); + sheet.CreateRow(1); + sheet.CreateRow(3); + var rowRange=sheet.Rows[1, 3]; //Rows[1,3] will not create rows + ClassicAssert.AreEqual(2, rowRange.Rows.Count); + } + } } \ No newline at end of file diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs index 658b08ed5..cdd2e0e74 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs @@ -3096,5 +3096,32 @@ public void TestCopyRepeatingRowsAndColumns() } } + + [Test] + public void TestGetCells_SingleCellRange_GetText() + { + var workbook = XSSFTestDataSamples.OpenSampleWorkbook("SampleSS.xlsx"); + var sheet = workbook.GetSheetAt(1); + var cellRange = sheet.Cells["A6:D7"]; + var texts=cellRange.Texts; + ClassicAssert.AreEqual(2, texts.Length); + ClassicAssert.AreEqual(4, texts[0].Length); + + ClassicAssert.AreEqual("cb=1", texts[0][0]); + ClassicAssert.AreEqual("cb=10", texts[0][1]); + ClassicAssert.AreEqual("cb=2", texts[0][2]); + ClassicAssert.AreEqual("cb=sum", texts[0][3]); + ClassicAssert.AreEqual("1", texts[1][0]); + ClassicAssert.AreEqual("10", texts[1][1]); + ClassicAssert.AreEqual("2", texts[1][2]); + ClassicAssert.AreEqual("SUM(A7:C7)", texts[1][3]); + + var texts2 = sheet.Cells["A5:D6"].Texts; + for(var i = 0; i