/// <summary> /// Create instance with alphabet code of position. (e.g. new CellPosition("A10")) /// </summary> /// <param name="address">an address string to locate the cell in spreadsheet. (e.g. 'A10')</param> /// <exception cref="ArgumentException">if address is not in correct format.</exception> /// <example>var pos = new CellPosition("A10");</example> public CellPosition(string address) { Match m = RGUtility.CellReferenceRegex.Match(address); if (!m.Success) { throw new ArgumentException("invalid address for cell: " + address, "id"); } this.row = 0; int.TryParse(m.Groups["row"].Value, out row); row--; this.col = RGUtility.GetNumberOfChar(m.Groups["col"].Value); this.positionProperties = 0; if (m.Groups["abs_row"].Success) { this.positionProperties |= PositionAbsoluteBits.StartRow; } if (m.Groups["abs_col"].Success) { this.positionProperties |= PositionAbsoluteBits.StartCol; } }
///// <summary> ///// Create filter on specified column. ///// </summary> ///// <param name="column">Column code that locates a column to create filter.</param> ///// <param name="titleRows">Indicates how many title rows exist at the top of worksheet, ///// title rows will not be included in filter range.</param> ///// <returns>Instance of column filter.</returns> //public AutoColumnFilter CreateColumnFilter(string column, int titleRows = 0, // AutoColumnFilterUI columnFilterUI = AutoColumnFilterUI.DropdownButtonAndPane) //{ // return CreateColumnFilter(column, column, titleRows, columnFilterUI); //} /// <summary> /// Create column filter. /// </summary> /// <param name="startColumn">First column specified by an address to create filter.</param> /// <param name="endColumn">Last column specified by an address to the filter.</param> /// <param name="titleRows">Indicates that how many title rows exist at the top of spreadsheet, /// title rows will not be included in filter apply range.</param> /// <param name="columnFilterUI">Indicates whether allow to create graphics user interface (GUI), /// by default the dropdown-button on the column and candidates dropdown-panel will be created. /// Set this argument as NoGUI to create filter without GUI.</param> /// <returns>Instance of column filter.</returns> public AutoColumnFilter CreateColumnFilter(string startColumn, string endColumn, int titleRows = 0, AutoColumnFilterUI columnFilterUI = AutoColumnFilterUI.DropdownButtonAndPanel) { int startIndex = RGUtility.GetNumberOfChar(startColumn); int endIndex = RGUtility.GetNumberOfChar(endColumn); return(CreateColumnFilter(startIndex, endIndex, titleRows, columnFilterUI)); }
/// <summary> /// Sort data on specified column. /// </summary> /// <param name="columnAddress">Base column specified by an address to sort data.</param> /// <param name="order">Order of data sort.</param> /// <param name="cellDataComparer">Custom cell data comparer, compares two cells and returns an integer. /// Set this value to null to use default built-in comparer.</param> /// <returns>Data changed range</returns> public RangePosition SortColumn(string columnAddress, SortOrder order = SortOrder.Ascending, CellElementFlag moveElementFlag = CellElementFlag.Data, Func <int, int, object, object, int> cellDataComparer = null) { return(SortColumn(RGUtility.GetNumberOfChar(columnAddress), order, moveElementFlag, cellDataComparer)); }
/// <summary> /// Create range position from a range or cell address. /// </summary> /// <param name="address">Address to locate the range position.</param> /// <exception cref="ArgumentException">Throw when specified address is invalid.</exception> public RangePosition(string address) { this.positionProperties = 0; Match m = RGUtility.RangeReferenceRegex.Match(address); if (!m.Success) { m = RGUtility.SingleAbsoulteRangeRegex.Match(address); } if (!m.Success && CellPosition.IsValidAddress(address)) { var pos = new CellPosition(address); this.row = pos.Row; this.col = pos.Col; this.rows = 1; this.cols = 1; this.StartRowProperty = pos.RowProperty; this.StartColumnProperty = pos.ColumnProperty; this.EndRowProperty = pos.RowProperty; this.EndColumnProperty = pos.ColumnProperty; } else { if (!m.Success //|| (m.Groups["to_col"].Length <= 0 || m.Groups["to_row"].Length <= 0 //|| m.Groups["from_col"].Length <= 0 || m.Groups["from_row"].Length <= 0) ) { throw new ArgumentException("range is invalid: " + address); } int fromCol = 0, fromRow = 0, toCol = 0, toRow = 0; bool fullRows = false, fullCols = false; if (m.Groups["from_row"].Success) { if (!int.TryParse(m.Groups["from_row"].Value, out fromRow)) { throw new ArgumentException("range is invalid: " + address); } fromRow--; } else { fullRows = true; } if (!fullRows && m.Groups["to_row"].Success) { if (!int.TryParse(m.Groups["to_row"].Value, out toRow)) { throw new ArgumentException("range is invalid: " + address); } toRow--; } if (m.Groups["from_col"].Success) { fromCol = RGUtility.GetNumberOfChar(m.Groups["from_col"].Value); } else { fullCols = true; } if (!fullCols && m.Groups["to_col"].Success) { toCol = RGUtility.GetNumberOfChar(m.Groups["to_col"].Value); } if (fullCols) { this.row = Math.Min(fromRow, toRow); this.rows = Math.Max(fromRow, toRow) - this.row + 1; this.col = 0; this.cols = -1; } else if (fullRows) { this.row = 0; this.rows = -1; this.col = Math.Min(fromCol, toCol); this.cols = Math.Max(fromCol, toCol) - this.col + 1; } else { this.row = Math.Min(fromRow, toRow); this.col = Math.Min(fromCol, toCol); this.rows = Math.Max(fromRow, toRow) - this.row + 1; this.cols = Math.Max(fromCol, toCol) - this.col + 1; } if (m.Groups["abs_from_row"].Success) { this.positionProperties |= PositionAbsoluteBits.StartRow; } if (m.Groups["abs_from_col"].Success) { this.positionProperties |= PositionAbsoluteBits.StartCol; } if (m.Groups["abs_to_row"].Success) { this.positionProperties |= PositionAbsoluteBits.EndRow; } if (m.Groups["abs_to_col"].Success) { this.positionProperties |= PositionAbsoluteBits.EndCol; } } }