diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs index 5b720b9..d2bc288 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs @@ -5,56 +5,28 @@ { internal class XLCellsCollection { - private readonly Dictionary _cellsDictionary = new Dictionary(); + public Dictionary ColumnsUsed = new Dictionary(); + public HashSet Deleted = new HashSet(); public Int32 MaxColumnUsed; public Int32 MaxRowUsed; - public Int32 Count { get; private set; } - public HashSet Deleted = new HashSet(); public Dictionary RowsUsed = new Dictionary(); - public Dictionary ColumnsUsed = new Dictionary(); public XLCellsCollection() { Clear(); } - //private void ResizeIfNecessary(Int32 row, Int32 column) - //{ - // if (row >= _rowCapacity || column >= _columnCapacity) - // { - // if (row >= _rowCapacity) - // { - // _rowCapacity = (Int32)((Double)_rowCapacity * 2); - - // if (_rowCapacity < row) - // _rowCapacity = (Int32)((Double)row * 1.5); + public Int32 Count { get; private set; } - // if (_rowCapacity > ExcelHelper.MaxRowNumber) - // _rowCapacity = ExcelHelper.MaxRowNumber; - // } - - // if (column >= _columnCapacity) - // { - // _columnCapacity = (Int32)((Double)_columnCapacity * 2); - - // if (_columnCapacity < column) - // _columnCapacity = (Int32)((Double)column * 1.5); - - // if (_columnCapacity > ExcelHelper.MaxColumnNumber) - // _columnCapacity = ExcelHelper.MaxColumnNumber; - // } - - // _cells = ExcelHelper.ResizeArray(_cells, _rowCapacity + 1, _columnCapacity + 1); - // } - //} public void Add(XLSheetPoint sheetPoint, XLCell cell) { Add(sheetPoint.Row, sheetPoint.Column, cell); } + public void Add(Int32 row, Int32 column, XLCell cell) { Count++; @@ -62,9 +34,7 @@ IncrementUsage(RowsUsed, row); IncrementUsage(ColumnsUsed, column); - //ResizeIfNecessary(row, column); - _cellsDictionary.Add(new XLSheetPoint(row, column),cell ); - //_cells[row, column] = cell; + _cellsDictionary.Add(new XLSheetPoint(row, column), cell); if (row > MaxRowUsed) MaxRowUsed = row; if (column > MaxColumnUsed) MaxColumnUsed = column; var sp = new XLSheetPoint(row, column); @@ -72,7 +42,7 @@ Deleted.Remove(sp); } - private static void IncrementUsage(Dictionary dictionary, Int32 key) + private static void IncrementUsage(Dictionary dictionary, Int32 key) { if (dictionary.ContainsKey(key)) dictionary[key]++; @@ -83,13 +53,12 @@ private static void DecrementUsage(Dictionary dictionary, Int32 key) { Int32 count; - if (dictionary.TryGetValue(key, out count)) - { - if (count > 0) - dictionary[key]--; - else - dictionary.Remove(key); - } + if (!dictionary.TryGetValue(key, out count)) return; + + if (count > 0) + dictionary[key]--; + else + dictionary.Remove(key); } public void Clear() @@ -105,8 +74,9 @@ public void Remove(XLSheetPoint sheetPoint) { - Remove(sheetPoint.Row,sheetPoint.Column); + Remove(sheetPoint.Row, sheetPoint.Column); } + public void Remove(Int32 row, Int32 column) { Count--; @@ -114,7 +84,7 @@ DecrementUsage(ColumnsUsed, row); var sp = new XLSheetPoint(row, column); Deleted.Add(sp); - _cellsDictionary.Remove(sp); + _cellsDictionary.Remove(sp); //_cells[row, column] = null; } @@ -128,16 +98,14 @@ for (int co = columnStart; co <= finalColumn; co++) { XLCell cell; - if (_cellsDictionary.TryGetValue(new XLSheetPoint(ro,co), out cell )) - { + if (_cellsDictionary.TryGetValue(new XLSheetPoint(ro, co), out cell)) yield return cell; - } } } } public void RemoveAll(Int32 rowStart, Int32 columnStart, - Int32 rowEnd, Int32 columnEnd) + Int32 rowEnd, Int32 columnEnd) { int finalRow = rowEnd > MaxRowUsed ? MaxRowUsed : rowEnd; int finalColumn = columnEnd > MaxColumnUsed ? MaxColumnUsed : columnEnd; @@ -147,15 +115,13 @@ { var sp = new XLSheetPoint(ro, co); if (_cellsDictionary.ContainsKey(sp)) - { Remove(sp); - } } } } public IEnumerable GetSheetPoints(Int32 rowStart, Int32 columnStart, - Int32 rowEnd, Int32 columnEnd) + Int32 rowEnd, Int32 columnEnd) { int finalRow = rowEnd > MaxRowUsed ? MaxRowUsed : rowEnd; int finalColumn = columnEnd > MaxColumnUsed ? MaxColumnUsed : columnEnd; @@ -166,9 +132,7 @@ { var sp = new XLSheetPoint(ro, co); if (_cellsDictionary.ContainsKey(sp)) - { yield return sp; - } } } } @@ -179,21 +143,13 @@ return null; var sp = new XLSheetPoint(row, column); XLCell cell; - if (_cellsDictionary.TryGetValue(sp, out cell)) - return cell; - - return null; - //return _cells[row, column]; + return _cellsDictionary.TryGetValue(sp, out cell) ? cell : null; } public XLCell GetCell(XLSheetPoint sheetPoint) { XLCell cell; - if (_cellsDictionary.TryGetValue(sheetPoint, out cell)) - return cell; - - return null; - //return _cells[sheetPoint.Row, sheetPoint.Column]; + return _cellsDictionary.TryGetValue(sheetPoint, out cell) ? cell : null; } internal void SwapRanges(XLSheetRange sheetRange1, XLSheetRange sheetRange2) @@ -209,26 +165,23 @@ var cell1 = GetCell(sp1); var cell2 = GetCell(sp2); - if (cell1 != null || cell2 != null) + if (cell1 == null && cell2 == null) continue; + + if (cell1 != null) { - if (cell1 != null) - { - cell1.Address = new XLAddress(cell1.Worksheet, sp2.Row, sp2.Column, false, false); - _cellsDictionary.Remove(sp1); - if (cell2 != null) - Add(sp1, cell2); - } - + cell1.Address = new XLAddress(cell1.Worksheet, sp2.Row, sp2.Column, false, false); + _cellsDictionary.Remove(sp1); if (cell2 != null) - { - cell2.Address = new XLAddress(cell2.Worksheet, sp1.Row, sp1.Column, false, false); - _cellsDictionary.Remove(sp2); - if (cell1 != null) - Add(sp2, cell1); - } - + Add(sp1, cell2); } - } + + if (cell2 == null) continue; + + cell2.Address = new XLAddress(cell2.Worksheet, sp1.Row, sp1.Column, false, false); + _cellsDictionary.Remove(sp2); + if (cell1 != null) + Add(sp2, cell1); + } } } @@ -245,9 +198,7 @@ { var cell = GetCell(ro, co); if (cell != null && predicate(cell)) - { yield return cell; - } } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs index c57c915..87a0496 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs @@ -88,10 +88,7 @@ { get { - if (IsReference) - return (Worksheet).Internals.ColumnsCollection[ColumnNumber()].InnerStyle; - - return new XLStyle(new XLStylizedContainer(_style, this), _style); + return IsReference ? Worksheet.Internals.ColumnsCollection[ColumnNumber()].InnerStyle : new XLStyle(new XLStylizedContainer(_style, this), _style); } set { @@ -120,10 +117,7 @@ { get { - if (IsReference) - return (Worksheet).Internals.ColumnsCollection[ColumnNumber()].Width; - - return _width; + return IsReference ? Worksheet.Internals.ColumnsCollection[ColumnNumber()].Width : _width; } set { @@ -179,10 +173,7 @@ { get { - if (IsReference) - return (Worksheet).Internals.ColumnsCollection[ColumnNumber()].Style; - - return _style; + return IsReference ? Worksheet.Internals.ColumnsCollection[ColumnNumber()].Style : _style; } set { @@ -275,35 +266,21 @@ Double colMaxWidth = minWidth; foreach (XLCell c in Column(startRow, endRow).CellsUsed()) { - if (!c.IsMerged()) + if (c.IsMerged()) continue; + + Double thisWidthMax = 0; + Int32 textRotation = c.Style.Alignment.TextRotation; + if (c.HasRichText || textRotation != 0 || c.InnerText.Contains(Environment.NewLine)) { - Double thisWidthMax = 0; - Int32 textRotation = c.Style.Alignment.TextRotation; - if (c.HasRichText || textRotation != 0 || c.InnerText.Contains(Environment.NewLine)) + var kpList = new List>(); + + #region if (c.HasRichText) + + if (c.HasRichText) { - var kpList = new List>(); - - #region if (c.HasRichText) - - if (c.HasRichText) + foreach (IXLRichString rt in c.RichText) { - foreach (IXLRichString rt in c.RichText) - { - String formattedString = rt.Text; - var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); - Int32 arrCount = arr.Count(); - for (Int32 i = 0; i < arrCount; i++) - { - String s = arr[i]; - if (i < arrCount - 1) - s += Environment.NewLine; - kpList.Add(new KeyValuePair(rt, s)); - } - } - } - else - { - String formattedString = c.GetFormattedString(); + String formattedString = rt.Text; var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); Int32 arrCount = arr.Count(); for (Int32 i = 0; i < arrCount; i++) @@ -311,118 +288,135 @@ String s = arr[i]; if (i < arrCount - 1) s += Environment.NewLine; - kpList.Add(new KeyValuePair(c.Style.Font, s)); + kpList.Add(new KeyValuePair(rt, s)); } } - - #endregion - - #region foreach (var kp in kpList) - - Double runningWidth = 0; - Boolean rotated = false; - Double maxLineWidth = 0; - Int32 lineCount = 1; - foreach (KeyValuePair kp in kpList) + } + else + { + String formattedString = c.GetFormattedString(); + var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); + Int32 arrCount = arr.Count(); + for (Int32 i = 0; i < arrCount; i++) { - var f = kp.Key; - String formattedString = kp.Value; + String s = arr[i]; + if (i < arrCount - 1) + s += Environment.NewLine; + kpList.Add(new KeyValuePair(c.Style.Font, s)); + } + } - Int32 newLinePosition = formattedString.IndexOf(Environment.NewLine); - if (textRotation == 0) + #endregion + + #region foreach (var kp in kpList) + + Double runningWidth = 0; + Boolean rotated = false; + Double maxLineWidth = 0; + Int32 lineCount = 1; + foreach (KeyValuePair kp in kpList) + { + var f = kp.Key; + String formattedString = kp.Value; + + Int32 newLinePosition = formattedString.IndexOf(Environment.NewLine); + if (textRotation == 0) + { + #region if (newLinePosition >= 0) + + if (newLinePosition >= 0) { - #region if (newLinePosition >= 0) + if (newLinePosition > 0) + runningWidth += f.GetWidth(formattedString.Substring(0, newLinePosition)); + + if (runningWidth > thisWidthMax) + thisWidthMax = runningWidth; + + runningWidth = newLinePosition < formattedString.Length - 2 + ? f.GetWidth(formattedString.Substring(newLinePosition + 2)) + : 0; + } + else + runningWidth += f.GetWidth(formattedString); + + #endregion + } + else + { + #region if (textRotation == 255) + + if (textRotation == 255) + { + if (runningWidth <= 0) + runningWidth = f.GetWidth("X"); + + if (newLinePosition >= 0) + runningWidth += f.GetWidth("X"); + } + else + { + rotated = true; + Double vWidth = f.GetWidth("X"); + if (vWidth > maxLineWidth) + maxLineWidth = vWidth; if (newLinePosition >= 0) { + lineCount++; + if (newLinePosition > 0) runningWidth += f.GetWidth(formattedString.Substring(0, newLinePosition)); if (runningWidth > thisWidthMax) thisWidthMax = runningWidth; - runningWidth = newLinePosition < formattedString.Length - 2 ? f.GetWidth(formattedString.Substring(newLinePosition + 2)) : 0; + runningWidth = newLinePosition < formattedString.Length - 2 + ? f.GetWidth(formattedString.Substring(newLinePosition + 2)) + : 0; } else runningWidth += f.GetWidth(formattedString); - - #endregion } - else - { - #region if (textRotation == 255) - if (textRotation == 255) - { - if (runningWidth == 0) - runningWidth = f.GetWidth("X"); - - if (newLinePosition >= 0) - runningWidth += f.GetWidth("X"); - } - else - { - rotated = true; - Double vWidth = f.GetWidth("X"); - if (vWidth > maxLineWidth) - maxLineWidth = vWidth; - - if (newLinePosition >= 0) - { - lineCount++; - - if (newLinePosition > 0) - runningWidth += f.GetWidth(formattedString.Substring(0, newLinePosition)); - - if (runningWidth > thisWidthMax) - thisWidthMax = runningWidth; - - runningWidth = newLinePosition < formattedString.Length - 2 ? f.GetWidth(formattedString.Substring(newLinePosition + 2)) : 0; - } - else - runningWidth += f.GetWidth(formattedString); - } - - #endregion - } + #endregion } - - #endregion - - if (runningWidth > thisWidthMax) - thisWidthMax = runningWidth; - - #region if (rotated) - - if (rotated) - { - Int32 rotation; - if (textRotation == 90 || textRotation == 180 || textRotation == 255) - rotation = 90; - else - rotation = textRotation % 90; - - Double r = DegreeToRadian(rotation); - - thisWidthMax = (thisWidthMax * Math.Cos(r)) + (maxLineWidth * lineCount); - } - - #endregion } - else - thisWidthMax = c.Style.Font.GetWidth(c.GetFormattedString()); - if (thisWidthMax >= maxWidth) + + #endregion + + if (runningWidth > thisWidthMax) + thisWidthMax = runningWidth; + + #region if (rotated) + + if (rotated) { - colMaxWidth = maxWidth; - break; + Int32 rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + Double r = DegreeToRadian(rotation); + + thisWidthMax = (thisWidthMax * Math.Cos(r)) + (maxLineWidth * lineCount); } - - if (thisWidthMax > colMaxWidth) - colMaxWidth = thisWidthMax + 1; + + #endregion } + else + thisWidthMax = c.Style.Font.GetWidth(c.GetFormattedString()); + if (thisWidthMax >= maxWidth) + { + colMaxWidth = maxWidth; + break; + } + + if (thisWidthMax > colMaxWidth) + colMaxWidth = thisWidthMax + 1; } - if (colMaxWidth == 0) + if (colMaxWidth <= 0) colMaxWidth = Worksheet.ColumnWidth; Width = colMaxWidth; @@ -445,10 +439,7 @@ { get { - if (IsReference) - return (Worksheet).Internals.ColumnsCollection[ColumnNumber()].IsHidden; - - return _isHidden; + return IsReference ? Worksheet.Internals.ColumnsCollection[ColumnNumber()].IsHidden : _isHidden; } set { @@ -668,57 +659,58 @@ return Math.PI * angle / 180.0; } - public override void CopyTo(IXLCell target) - { - CopyTo(target); - } - - public override void CopyTo(IXLRangeBase target) - { - CopyTo(target); - } - - #region XLColumn Left - public XLColumn ColumnLeft() - { - return ColumnLeft(1); - } - IXLColumn IXLColumn.ColumnLeft() - { - return ColumnLeft(); - } - public XLColumn ColumnLeft(Int32 step) - { - return ColumnShift(step * -1); - } - IXLColumn IXLColumn.ColumnLeft(Int32 step) - { - return ColumnLeft(step); - } - #endregion - - #region XLColumn Right - public XLColumn ColumnRight() - { - return ColumnRight(1); - } - IXLColumn IXLColumn.ColumnRight() - { - return ColumnRight(); - } - public XLColumn ColumnRight(Int32 step) - { - return ColumnShift(step); - } - IXLColumn IXLColumn.ColumnRight(Int32 step) - { - return ColumnRight(step); - } - #endregion private XLColumn ColumnShift(Int32 columnsToShift) { return Worksheet.Column(ColumnNumber() + columnsToShift); } + + #region XLColumn Left + + IXLColumn IXLColumn.ColumnLeft() + { + return ColumnLeft(); + } + + IXLColumn IXLColumn.ColumnLeft(Int32 step) + { + return ColumnLeft(step); + } + + public XLColumn ColumnLeft() + { + return ColumnLeft(1); + } + + public XLColumn ColumnLeft(Int32 step) + { + return ColumnShift(step * -1); + } + + #endregion + + #region XLColumn Right + + IXLColumn IXLColumn.ColumnRight() + { + return ColumnRight(); + } + + IXLColumn IXLColumn.ColumnRight(Int32 step) + { + return ColumnRight(step); + } + + public XLColumn ColumnRight() + { + return ColumnRight(1); + } + + public XLColumn ColumnRight(Int32 step) + { + return ColumnShift(step); + } + + #endregion } } \ No newline at end of file diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index e06693b..36dcf5e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -30,6 +30,44 @@ get { return RangeAddress.Worksheet; } } + public XLDataValidation DataValidation + { + get + { + var thisRange = AsRange(); + if (Worksheet.DataValidations.ContainsSingle(thisRange)) + return + Worksheet.DataValidations.Where(dv => dv.Ranges.Contains(thisRange)).Single() as + XLDataValidation; + var dvEmpty = new List(); + foreach (IXLDataValidation dv in Worksheet.DataValidations) + { + foreach (IXLRange dvRange in dv.Ranges) + { + if (dvRange.Intersects(this)) + { + dv.Ranges.Remove(dvRange); + foreach (IXLCell c in dvRange.Cells()) + { + if (!Contains(c.Address.ToString())) + dv.Ranges.Add(c.AsRange()); + } + if (dv.Ranges.Count() == 0) + dvEmpty.Add(dv); + } + } + } + + dvEmpty.ForEach(dv => Worksheet.DataValidations.Delete(dv)); + + var newRanges = new XLRanges {AsRange()}; + var dataValidation = new XLDataValidation(newRanges, Worksheet); + + Worksheet.DataValidations.Add(dataValidation); + return dataValidation; + } + } + #region IXLRangeBase Members IXLRangeAddress IXLRangeBase.RangeAddress @@ -70,42 +108,6 @@ } } - public XLDataValidation DataValidation - { - get - { - var thisRange = AsRange(); - if (Worksheet.DataValidations.ContainsSingle(thisRange)) - return Worksheet.DataValidations.Where(dv => dv.Ranges.Contains(thisRange)).Single() as XLDataValidation; - var dvEmpty = new List(); - foreach (IXLDataValidation dv in Worksheet.DataValidations) - { - foreach (IXLRange dvRange in dv.Ranges) - { - if (dvRange.Intersects(this)) - { - dv.Ranges.Remove(dvRange); - foreach (IXLCell c in dvRange.Cells()) - { - if (!Contains(c.Address.ToString())) - dv.Ranges.Add(c.AsRange()); - } - if (dv.Ranges.Count() == 0) - dvEmpty.Add(dv); - } - } - } - - dvEmpty.ForEach(dv => Worksheet.DataValidations.Delete(dv)); - - var newRanges = new XLRanges {AsRange()}; - var dataValidation = new XLDataValidation(newRanges, Worksheet); - - Worksheet.DataValidations.Add(dataValidation); - return dataValidation; - } - } - IXLDataValidation IXLRangeBase.DataValidation { get { return DataValidation; } @@ -172,7 +174,7 @@ public IXLCells Cells() { - var cells = new XLCells( false, false) {RangeAddress}; + var cells = new XLCells(false, false) {RangeAddress}; return cells; } @@ -183,7 +185,7 @@ public IXLCells CellsUsed() { - var cells = new XLCells( true, false) {RangeAddress}; + var cells = new XLCells(true, false) {RangeAddress}; return cells; } @@ -204,7 +206,7 @@ Worksheet.Internals.MergedRanges.Add(asRange); // Call every cell in the merge to make sure they exist - asRange.Cells().ForEach(c=> { }); + asRange.Cells().ForEach(c => { }); return asRange; } @@ -441,15 +443,17 @@ var absoluteAddress = cellAddressInRange + RangeAddress.FirstAddress - 1; if (absoluteAddress.RowNumber <= 0 || absoluteAddress.RowNumber > ExcelHelper.MaxRowNumber) - throw new IndexOutOfRangeException(String.Format("Row number must be between 1 and {0}", ExcelHelper.MaxRowNumber)); + throw new IndexOutOfRangeException(String.Format("Row number must be between 1 and {0}", + ExcelHelper.MaxRowNumber)); if (absoluteAddress.ColumnNumber <= 0 || absoluteAddress.ColumnNumber > ExcelHelper.MaxColumnNumber) - throw new IndexOutOfRangeException(String.Format("Column number must be between 1 and {0}", ExcelHelper.MaxColumnNumber)); + throw new IndexOutOfRangeException(String.Format("Column number must be between 1 and {0}", + ExcelHelper.MaxColumnNumber)); var cell = Worksheet.Internals.CellsCollection.GetCell(absoluteAddress.RowNumber, absoluteAddress.ColumnNumber); - if(cell!=null) + if (cell != null) return cell; var style = Style; @@ -591,7 +595,7 @@ public XLCells CellsUsed(bool includeFormats) { - var cells = new XLCells( true, includeFormats) {RangeAddress}; + var cells = new XLCells(true, includeFormats) {RangeAddress}; return cells; } @@ -696,9 +700,8 @@ var oldKey = new XLAddress(Worksheet, ro, co, false, false); int newColumn = co + numberOfColumns; var newKey = new XLAddress(Worksheet, ro, newColumn, false, false); - XLCell oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co); - if (oldCell == null) - oldCell = Worksheet.Cell(oldKey); + var oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co) ?? + Worksheet.Cell(oldKey); var newCell = new XLCell(Worksheet, newKey, oldCell.Style); newCell.CopyValues(oldCell); @@ -712,7 +715,10 @@ } else { - foreach (var c in Worksheet.Internals.CellsCollection.GetCells(firstRow,firstColumn,lastRow,Worksheet.Internals.CellsCollection.MaxColumnUsed)) + foreach ( + XLCell c in + Worksheet.Internals.CellsCollection.GetCells(firstRow, firstColumn, lastRow, + Worksheet.Internals.CellsCollection.MaxColumnUsed)) { int newColumn = c.Address.ColumnNumber + numberOfColumns; var newKey = new XLAddress(Worksheet, c.Address.RowNumber, newColumn, false, false); @@ -724,8 +730,9 @@ cellsToBlank.Add(c.Address); } } - cellsToDelete.ForEach(c => Worksheet.Internals.CellsCollection.Remove(c.RowNumber,c.ColumnNumber)); - cellsToInsert.ForEach(c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); + cellsToDelete.ForEach(c => Worksheet.Internals.CellsCollection.Remove(c.RowNumber, c.ColumnNumber)); + cellsToInsert.ForEach( + c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); foreach (IXLAddress c in cellsToBlank) { var styleToUse = Worksheet.Internals.RowsCollection.ContainsKey(c.RowNumber) @@ -844,9 +851,8 @@ var oldKey = new XLAddress(Worksheet, ro, co, false, false); int newRow = ro + numberOfRows; var newKey = new XLAddress(Worksheet, newRow, co, false, false); - XLCell oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co); - if (oldCell == null) - oldCell = Worksheet.Cell(oldKey); + var oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co) ?? + Worksheet.Cell(oldKey); var newCell = new XLCell(Worksheet, newKey, oldCell.Style); newCell.CopyFrom(oldCell); @@ -860,7 +866,11 @@ } else { - foreach (var c in Worksheet.Internals.CellsCollection.GetCells(firstRow, firstColumn, Worksheet.Internals.CellsCollection.MaxRowUsed, lastColumn)) + foreach ( + XLCell c in + Worksheet.Internals.CellsCollection.GetCells(firstRow, firstColumn, + Worksheet.Internals.CellsCollection.MaxRowUsed, + lastColumn)) { int newRow = c.Address.RowNumber + numberOfRows; var newKey = new XLAddress(Worksheet, newRow, c.Address.ColumnNumber, false, false); @@ -873,14 +883,13 @@ } } cellsToDelete.ForEach(c => Worksheet.Internals.CellsCollection.Remove(c.RowNumber, c.ColumnNumber)); - cellsToInsert.ForEach(c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); + cellsToInsert.ForEach( + c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); foreach (IXLAddress c in cellsToBlank) { - IXLStyle styleToUse; - if (Worksheet.Internals.ColumnsCollection.ContainsKey(c.ColumnNumber)) - styleToUse = Worksheet.Internals.ColumnsCollection[c.ColumnNumber].Style; - else - styleToUse = Worksheet.Style; + var styleToUse = Worksheet.Internals.ColumnsCollection.ContainsKey(c.ColumnNumber) + ? Worksheet.Internals.ColumnsCollection[c.ColumnNumber].Style + : Worksheet.Style; Worksheet.Cell(c.RowNumber, c.ColumnNumber).Style = styleToUse; } Worksheet.NotifyRangeShiftedRows((XLRange)AsRange(), numberOfRows); @@ -894,12 +903,7 @@ private void ClearMerged() { - var mergeToDelete = new List(); - foreach (IXLRange merge in Worksheet.Internals.MergedRanges) - { - if (Intersects(merge)) - mergeToDelete.Add(merge); - } + var mergeToDelete = Worksheet.Internals.MergedRanges.Where(Intersects).ToList(); mergeToDelete.ForEach(m => Worksheet.Internals.MergedRanges.Remove(m)); } @@ -925,22 +929,7 @@ RangeAddress.FirstAddress.ColumnNumber, RangeAddress.LastAddress.RowNumber, RangeAddress.LastAddress.ColumnNumber); - //if (shiftDeleteCells == XLShiftDeletedCells.ShiftCellsUp) - //{ - // var lastCell = Worksheet.Cell(ExcelHelper.MaxRowNumber, RangeAddress.LastAddress.ColumnNumber); - // shiftedRangeFormula = Worksheet.Range(RangeAddress.FirstAddress, lastCell.Address); - // if (StringExtensions.IsNullOrWhiteSpace(lastCell.GetString()) && - // StringExtensions.IsNullOrWhiteSpace(lastCell.FormulaA1)) - // Worksheet.Internals.CellsCollection.Remove(lastCell.Address.RowNumber, lastCell.Address.ColumnNumber); - //} - //else - //{ - // var lastCell = Worksheet.Cell(RangeAddress.LastAddress.RowNumber, ExcelHelper.MaxColumnNumber); - // shiftedRangeFormula = Worksheet.Range(RangeAddress.FirstAddress, lastCell.Address); - // if (StringExtensions.IsNullOrWhiteSpace(lastCell.GetString()) && - // StringExtensions.IsNullOrWhiteSpace(lastCell.FormulaA1)) - // Worksheet.Internals.CellsCollection.Remove(lastCell.Address.RowNumber, lastCell.Address.ColumnNumber); - //} + foreach (IXLWorksheet ws in Worksheet.Workbook.Worksheets) { @@ -976,9 +965,10 @@ int columnModifier = shiftDeleteCells == XLShiftDeletedCells.ShiftCellsLeft ? ColumnCount() : 0; int rowModifier = shiftDeleteCells == XLShiftDeletedCells.ShiftCellsUp ? RowCount() : 0; var cellsQuery = shiftDeleteCells == XLShiftDeletedCells.ShiftCellsLeft ? shiftLeftQuery : shiftUpQuery; - foreach (var c in cellsQuery) + foreach (XLCell c in cellsQuery) { - var newKey = new XLAddress(Worksheet, c.Address.RowNumber - rowModifier, c.Address.ColumnNumber - columnModifier, + var newKey = new XLAddress(Worksheet, c.Address.RowNumber - rowModifier, + c.Address.ColumnNumber - columnModifier, false, false); var newCell = new XLCell(Worksheet, newKey, c.Style); newCell.CopyValues(c); @@ -993,22 +983,13 @@ cellsToInsert.Add(newKey, newCell); } cellsToDelete.ForEach(c => Worksheet.Internals.CellsCollection.Remove(c.RowNumber, c.ColumnNumber)); - cellsToInsert.ForEach(c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); + cellsToInsert.ForEach( + c => Worksheet.Internals.CellsCollection.Add(c.Key.RowNumber, c.Key.ColumnNumber, c.Value)); - var mergesToRemove = new List(); - foreach (IXLRange merge in Worksheet.Internals.MergedRanges) - { - if (Contains(merge)) - mergesToRemove.Add(merge); - } + var mergesToRemove = Worksheet.Internals.MergedRanges.Where(Contains).ToList(); mergesToRemove.ForEach(r => Worksheet.Internals.MergedRanges.Remove(r)); - var hyperlinksToRemove = new List(); - foreach (XLHyperlink hl in Worksheet.Hyperlinks) - { - if (Contains(hl.Cell.AsRange())) - hyperlinksToRemove.Add(hl); - } + var hyperlinksToRemove = Worksheet.Hyperlinks.Where(hl => Contains(hl.Cell.AsRange())).ToList(); hyperlinksToRemove.ForEach(hl => Worksheet.Hyperlinks.Delete(hl)); var shiftedRange = (XLRange)AsRange(); @@ -1183,10 +1164,7 @@ public void SetAutoFilter(Boolean autoFilter) { - if (autoFilter) - Worksheet.AutoFilterRange = this; - else - Worksheet.AutoFilterRange = null; + Worksheet.AutoFilterRange = autoFilter ? this : null; } //public IXLChart CreateChart(Int32 firstRow, Int32 firstColumn, Int32 lastRow, Int32 lastColumn) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs index 69d8481..bffff3a 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs @@ -6,7 +6,6 @@ { internal class XLRow : XLRangeBase, IXLRow { - #region Private fields private Boolean _collapsed; @@ -30,7 +29,7 @@ if (IsReference) { //SMELL: Leak may occur - (Worksheet).RangeShiftedRows += WorksheetRangeShiftedRows; + Worksheet.RangeShiftedRows += WorksheetRangeShiftedRows; } else { @@ -92,9 +91,9 @@ { get { - if (IsReference) - return (Worksheet).Internals.RowsCollection[RowNumber()].InnerStyle; - return new XLStyle(new XLStylizedContainer(_style, this), _style); + return IsReference + ? Worksheet.Internals.RowsCollection[RowNumber()].InnerStyle + : new XLStyle(new XLStylizedContainer(_style, this), _style); } set { @@ -107,13 +106,7 @@ public Boolean Collapsed { - get - { - if (IsReference) - return (Worksheet).Internals.RowsCollection[RowNumber()].Collapsed; - - return _collapsed; - } + get { return IsReference ? Worksheet.Internals.RowsCollection[RowNumber()].Collapsed : _collapsed; } set { if (IsReference) @@ -127,12 +120,7 @@ public Double Height { - get - { - if (IsReference) - return (Worksheet).Internals.RowsCollection[RowNumber()].Height; - return _height; - } + get { return IsReference ? Worksheet.Internals.RowsCollection[RowNumber()].Height : _height; } set { if (IsReference) @@ -236,35 +224,18 @@ public IXLRow AdjustToContents(Int32 startColumn, Int32 endColumn, Double minHeight, Double maxHeight) { Double rowMaxHeight = minHeight; - foreach (IXLCell cell in Row(startColumn, endColumn).CellsUsed()) + foreach (XLCell c in from XLCell c in Row(startColumn, endColumn).CellsUsed() where !c.IsMerged() select c) { - var c = (XLCell)cell; - if (!c.IsMerged()) + Double thisHeight; + Int32 textRotation = c.Style.Alignment.TextRotation; + if (c.HasRichText || textRotation != 0 || c.InnerText.Contains(Environment.NewLine)) { - Double thisHeight; - Int32 textRotation = c.Style.Alignment.TextRotation; - if (c.HasRichText || textRotation != 0 || c.InnerText.Contains(Environment.NewLine)) + var kpList = new List>(); + if (c.HasRichText) { - var kpList = new List>(); - if (c.HasRichText) + foreach (IXLRichString rt in c.RichText) { - foreach (IXLRichString rt in c.RichText) - { - String formattedString = rt.Text; - var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); - Int32 arrCount = arr.Count(); - for (Int32 i = 0; i < arrCount; i++) - { - String s = arr[i]; - if (i < arrCount - 1) - s += Environment.NewLine; - kpList.Add(new KeyValuePair(rt, s)); - } - } - } - else - { - String formattedString = c.GetFormattedString(); + String formattedString = rt.Text; var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); Int32 arrCount = arr.Count(); for (Int32 i = 0; i < arrCount; i++) @@ -272,45 +243,58 @@ String s = arr[i]; if (i < arrCount - 1) s += Environment.NewLine; - kpList.Add(new KeyValuePair(c.Style.Font, s)); - } - } - - Double maxLongCol = kpList.Max(kp => kp.Value.Length); - Double maxHeightCol = kpList.Max(kp => kp.Key.GetHeight()); - Int32 lineCount = kpList.Count(kp => kp.Value.Contains(Environment.NewLine)); - if (textRotation == 0) - thisHeight = maxHeightCol * lineCount; - else - { - if (textRotation == 255) - thisHeight = maxLongCol * maxHeightCol; - else - { - Double rotation; - if (textRotation == 90 || textRotation == 180 || textRotation == 255) - rotation = 90; - else - rotation = textRotation % 90; - - thisHeight = (rotation / 90.0) * maxHeightCol * maxLongCol * 0.5; + kpList.Add(new KeyValuePair(rt, s)); } } } else - thisHeight = c.Style.Font.GetHeight(); - - if (thisHeight >= maxHeight) { - rowMaxHeight = maxHeight; - break; + String formattedString = c.GetFormattedString(); + var arr = formattedString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); + Int32 arrCount = arr.Count(); + for (Int32 i = 0; i < arrCount; i++) + { + String s = arr[i]; + if (i < arrCount - 1) + s += Environment.NewLine; + kpList.Add(new KeyValuePair(c.Style.Font, s)); + } } - if (thisHeight > rowMaxHeight) - rowMaxHeight = thisHeight; + + Double maxLongCol = kpList.Max(kp => kp.Value.Length); + Double maxHeightCol = kpList.Max(kp => kp.Key.GetHeight()); + Int32 lineCount = kpList.Count(kp => kp.Value.Contains(Environment.NewLine)); + if (textRotation == 0) + thisHeight = maxHeightCol * lineCount; + else + { + if (textRotation == 255) + thisHeight = maxLongCol * maxHeightCol; + else + { + Double rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + thisHeight = (rotation / 90.0) * maxHeightCol * maxLongCol * 0.5; + } + } } + else + thisHeight = c.Style.Font.GetHeight(); + + if (thisHeight >= maxHeight) + { + rowMaxHeight = maxHeight; + break; + } + if (thisHeight > rowMaxHeight) + rowMaxHeight = thisHeight; } - if (rowMaxHeight == 0) + if (rowMaxHeight <= 0) rowMaxHeight = Worksheet.RowHeight; Height = rowMaxHeight; @@ -341,13 +325,7 @@ public override IXLStyle Style { - get - { - if (IsReference) - return (Worksheet).Internals.RowsCollection[RowNumber()].Style; - - return _style; - } + get { return IsReference ? Worksheet.Internals.RowsCollection[RowNumber()].Style : _style; } set { if (IsReference) @@ -390,12 +368,7 @@ public Int32 OutlineLevel { - get - { - if (IsReference) - return (Worksheet).Internals.RowsCollection[RowNumber()].OutlineLevel; - return _outlineLevel; - } + get { return IsReference ? Worksheet.Internals.RowsCollection[RowNumber()].OutlineLevel : _outlineLevel; } set { if (value < 1 || value > 8) @@ -603,57 +576,57 @@ } } - public override void CopyTo(IXLCell target) - { - CopyTo(target); - } - - public override void CopyTo(IXLRangeBase target) - { - CopyTo(target); - } - - #region XLRow Above - public XLRow RowAbove() - { - return RowAbove(1); - } - IXLRow IXLRow.RowAbove() - { - return RowAbove(); - } - public XLRow RowAbove(Int32 step) - { - return RowShift(step * -1); - } - IXLRow IXLRow.RowAbove(Int32 step) - { - return RowAbove(step); - } - #endregion - - #region XLRow Below - public XLRow RowBelow() - { - return RowBelow(1); - } - IXLRow IXLRow.RowBelow() - { - return RowBelow(); - } - public XLRow RowBelow(Int32 step) - { - return RowShift(step); - } - IXLRow IXLRow.RowBelow(Int32 step) - { - return RowBelow(step); - } - #endregion - private XLRow RowShift(Int32 rowsToShift) { return Worksheet.Row(RowNumber() + rowsToShift); } + + #region XLRow Above + + IXLRow IXLRow.RowAbove() + { + return RowAbove(); + } + + IXLRow IXLRow.RowAbove(Int32 step) + { + return RowAbove(step); + } + + public XLRow RowAbove() + { + return RowAbove(1); + } + + public XLRow RowAbove(Int32 step) + { + return RowShift(step * -1); + } + + #endregion + + #region XLRow Below + + IXLRow IXLRow.RowBelow() + { + return RowBelow(); + } + + IXLRow IXLRow.RowBelow(Int32 step) + { + return RowBelow(step); + } + + public XLRow RowBelow() + { + return RowBelow(1); + } + + public XLRow RowBelow(Int32 step) + { + return RowShift(step); + } + + #endregion } } \ No newline at end of file diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/IXLTableRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/IXLTableRow.cs index a69e520..58cb25e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/IXLTableRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/IXLTableRow.cs @@ -12,6 +12,9 @@ new IXLTableRow Sort(XLSortOrder sortOrder); new IXLTableRow Sort(XLSortOrder sortOrder, Boolean matchCase); - + new IXLTableRow RowAbove(); + new IXLTableRow RowAbove(Int32 step); + new IXLTableRow RowBelow(); + new IXLTableRow RowBelow(Int32 step); } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs index 96f6daa..dd18433 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs @@ -10,7 +10,7 @@ #region Private fields private readonly Dictionary _fieldNames = new Dictionary(); - internal Dictionary _fields = new Dictionary(); + private readonly Dictionary _fields = new Dictionary(); private string _name; internal bool _showTotalsRow; internal HashSet _uniqueNames; @@ -55,10 +55,7 @@ { get { - if (_showTotalsRow) - return Range(2, 1, RowCount() - 1, ColumnCount()); - - return Range(2, 1, RowCount(), ColumnCount()); + return _showTotalsRow ? Range(2, 1, RowCount() - 1, ColumnCount()) : Range(2, 1, RowCount(), ColumnCount()); } } @@ -77,8 +74,10 @@ set { if (Worksheet.Tables.Any(t => t.Name == value)) + { throw new ArgumentException(String.Format("This worksheet already contains a table named '{0}'", value)); + } _name = value; } @@ -107,8 +106,8 @@ { if (ShowTotalsRow) return new XLTableRow(this, (XLRangeRow)base.LastRow()); - - throw new InvalidOperationException("Cannot access TotalsRow if ShowTotals property is false"); + + throw new InvalidOperationException("Cannot access TotalsRow if ShowTotals property is false"); } public new IXLTableRow FirstRow() @@ -123,10 +122,7 @@ public new IXLTableRow LastRow() { - if (ShowTotalsRow) - return new XLTableRow(this, base.Row(RowCount() - 1)); - - return new XLTableRow(this, base.Row(RowCount())); + return ShowTotalsRow ? new XLTableRow(this, base.Row(RowCount() - 1)) : new XLTableRow(this, base.Row(RowCount())); } public new IXLTableRow LastRowUsed() @@ -134,13 +130,9 @@ return new XLTableRow(this, (XLRangeRow)(DataRange.LastRowUsed())); } - public new IXLTableRow Row(int row) + IXLTableRow IXLTable.Row(int row) { - if (row <= 0 || row > ExcelHelper.MaxRowNumber) - throw new IndexOutOfRangeException(String.Format("Row number must be between 1 and {0}", - ExcelHelper.MaxRowNumber)); - - return new XLTableRow(this, base.Row(row + 1)); + return Row(row); } public new IXLTableRows Rows() @@ -164,17 +156,14 @@ { var retVal = new XLTableRows(Worksheet.Style); var rowPairs = rows.Split(','); - foreach (string pair in rowPairs) + foreach (string tPair in rowPairs.Select(pair => pair.Trim())) { - string tPair = pair.Trim(); String firstRow; String lastRow; if (tPair.Contains(':') || tPair.Contains('-')) { - if (tPair.Contains('-')) - tPair = tPair.Replace('-', ':'); + string[] rowRange = tPair.Contains('-') ? tPair.Replace('-', ':').Split(':') : tPair.Split(':'); - var rowRange = tPair.Split(':'); firstRow = rowRange[0]; lastRow = rowRange[1]; } @@ -199,10 +188,7 @@ if (ExcelHelper.IsValidColumn(column)) { Int32 coNum = ExcelHelper.GetColumnNumberFromLetter(column); - if (coNum > ColumnCount()) - return DataRange.Column(GetFieldIndex(column) + 1); - - return DataRange.Column(coNum); + return coNum > ColumnCount() ? DataRange.Column(GetFieldIndex(column) + 1) : DataRange.Column(coNum); } return DataRange.Column(GetFieldIndex(column) + 1); @@ -311,9 +297,8 @@ public new IXLRange Sort(String elementsToSortBy) { var toSortBy = new StringBuilder(); - foreach (String coPair in elementsToSortBy.Split(',')) + foreach (string coPairTrimmed in elementsToSortBy.Split(',').Select(coPair => coPair.Trim())) { - String coPairTrimmed = coPair.Trim(); String coString; String order; if (coPairTrimmed.Contains(' ')) @@ -430,6 +415,17 @@ #endregion + public new XLTableRow Row(int row) + { + if (row <= 0 || row > ExcelHelper.MaxRowNumber) + { + throw new IndexOutOfRangeException(String.Format("Row number must be between 1 and {0}", + ExcelHelper.MaxRowNumber)); + } + + return new XLTableRow(this, base.Row(row + 1)); + } + private void InitializeValues() { ShowRowStripes = true; @@ -439,19 +435,18 @@ private void AddToTables(XLRange range, Boolean addToTables) { - if (addToTables) + if (!addToTables) return; + + _uniqueNames = new HashSet(); + Int32 co = 1; + foreach (IXLCell c in range.Row(1).Cells()) { - _uniqueNames = new HashSet(); - Int32 co = 1; - foreach (IXLCell c in range.Row(1).Cells()) - { - if (StringExtensions.IsNullOrWhiteSpace(((XLCell)c).InnerText)) - c.Value = GetUniqueName("Column" + co.ToStringLookup()); - _uniqueNames.Add(c.GetString()); - co++; - } - Worksheet.Tables.Add(this); + if (StringExtensions.IsNullOrWhiteSpace(((XLCell)c).InnerText)) + c.Value = GetUniqueName("Column" + co.ToStringLookup()); + _uniqueNames.Add(c.GetString()); + co++; } + Worksheet.Tables.Add(this); } private String GetUniqueName(String originalName) @@ -476,26 +471,24 @@ { if (_fieldNames.ContainsKey(name)) return _fieldNames[name].Index; - else + + var headersRow = HeadersRow(); + Int32 cellCount = headersRow.CellCount(); + for (Int32 cellPos = 1; cellPos <= cellCount; cellPos++) { - var headersRow = HeadersRow(); - Int32 cellCount = headersRow.CellCount(); - for (Int32 cellPos = 1; cellPos <= cellCount; cellPos++) - { - if (headersRow.Cell(cellPos).GetString().Equals(name)) - { - if (_fieldNames.ContainsKey(name)) - throw new ArgumentException("The header row contains more than one field name '" + name + - "'."); - else - _fieldNames.Add(name, Field(cellPos - 1)); - } - } + if (!headersRow.Cell(cellPos).GetString().Equals(name)) continue; + if (_fieldNames.ContainsKey(name)) - return _fieldNames[name].Index; - else - throw new ArgumentOutOfRangeException("The header row doesn't contain field name '" + name + "'."); + { + throw new ArgumentException("The header row contains more than one field name '" + name + + "'."); + } + _fieldNames.Add(name, Field(cellPos - 1)); } + if (_fieldNames.ContainsKey(name)) + return _fieldNames[name].Index; + + throw new ArgumentOutOfRangeException("The header row doesn't contain field name '" + name + "'."); } } } \ No newline at end of file diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableRow.cs index a095dd9..43db4ef 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableRow.cs @@ -2,15 +2,18 @@ namespace ClosedXML.Excel { - internal class XLTableRow: XLRangeRow, IXLTableRow + internal class XLTableRow : XLRangeRow, IXLTableRow { - private XLTable table; + private readonly XLTable _table; + public XLTableRow(XLTable table, XLRangeRow rangeRow) : base(rangeRow.RangeParameters, false) { - this.table = table; + _table = table; } + #region IXLTableRow Members + public IXLCell Field(Int32 index) { return Cell(index + 1); @@ -18,30 +21,87 @@ public IXLCell Field(String name) { - Int32 fieldIndex = table.GetFieldIndex(name); + Int32 fieldIndex = _table.GetFieldIndex(name); return Cell(fieldIndex + 1); } public new IXLTableRow Sort() { - this.AsRange().Sort(XLSortOrientation.LeftToRight); - return this; - } - public new IXLTableRow Sort(XLSortOrder sortOrder) - { - this.AsRange().Sort(XLSortOrientation.LeftToRight, sortOrder); - return this; - } - public new IXLTableRow Sort(Boolean matchCase) - { - this.AsRange().Sort(XLSortOrientation.LeftToRight, matchCase); - return this; - } - public new IXLTableRow Sort(XLSortOrder sortOrder, Boolean matchCase) - { - this.AsRange().Sort(XLSortOrientation.LeftToRight, sortOrder, matchCase); + AsRange().Sort(XLSortOrientation.LeftToRight); return this; } + public new IXLTableRow Sort(XLSortOrder sortOrder) + { + AsRange().Sort(XLSortOrientation.LeftToRight, sortOrder); + return this; + } + + public new IXLTableRow Sort(Boolean matchCase) + { + AsRange().Sort(XLSortOrientation.LeftToRight, matchCase); + return this; + } + + public new IXLTableRow Sort(XLSortOrder sortOrder, Boolean matchCase) + { + AsRange().Sort(XLSortOrientation.LeftToRight, sortOrder, matchCase); + return this; + } + + #endregion + + private XLTableRow RowShift(Int32 rowsToShift) + { + return _table.Row(RowNumber() + rowsToShift); + } + + #region XLTableRow Above + + IXLTableRow IXLTableRow.RowAbove() + { + return RowAbove(); + } + + IXLTableRow IXLTableRow.RowAbove(Int32 step) + { + return RowAbove(step); + } + + public new XLTableRow RowAbove() + { + return RowAbove(1); + } + + public new XLTableRow RowAbove(Int32 step) + { + return RowShift(step * -1); + } + + #endregion + + #region XLTableRow Below + + IXLTableRow IXLTableRow.RowBelow() + { + return RowBelow(); + } + + IXLTableRow IXLTableRow.RowBelow(Int32 step) + { + return RowBelow(step); + } + + public XLTableRow RowBelow() + { + return RowBelow(1); + } + + public XLTableRow RowBelow(Int32 step) + { + return RowShift(step); + } + + #endregion } -} +} \ No newline at end of file