/// <summary> /// Gets the bounding rectangle of the Excel cell. /// </summary> /// <param name="cellInfo">The cell info.</param> /// <returns>The bounding rectangle as an array. /// The values are relative to the parent window and in Points (instead of Pixels).</returns> public double[] GetBoundingRectangle(ExcelCellInfo cellInfo) { // Use Excel's Object Model to get the required. double[] rect = new double[4]; rect[0] = rect[1] = rect[2] = rect[3] = -1; Range cell = GetCell(cellInfo); if (cell != null) { const double xOffset = 25.6; // The constant width of row name column. const double yOffset = 36.0; // The constant height of column name row. rect[0] = (double)cell.Left + xOffset; rect[1] = (double)cell.Top + yOffset; rect[2] = (double)cell.Width; rect[3] = (double)cell.Height; Range visibleRange = this.application.ActiveWindow.VisibleRange; if (visibleRange != null) { rect[0] -= (double)visibleRange.Left; rect[1] -= (double)visibleRange.Top; } } return rect; }
/// <summary> /// Gets the property of a given cell. /// </summary> /// <param name="cellInfo">The cell info.</param> /// <param name="propertyName">The name of the property.</param> /// <returns>The value of the property.</returns> public object GetCellProperty(ExcelCellInfo cellInfo, string propertyName) { // Use Excel's Object Model to get the required. Range cell = GetCell(cellInfo); if (cell == null) { throw new InvalidOperationException(); } switch (propertyName) { case PropertyNames.Enabled: return true; // TODO - Needed to add support for "locked" cells. case PropertyNames.Value: return cell.Value; case PropertyNames.Text: return cell.Text; case PropertyNames.WidthInChars: return cell.ColumnWidth; case PropertyNames.HeightInPoints: return cell.RowHeight; case PropertyNames.Formula: return cell.Formula; case PropertyNames.WrapText: return cell.WrapText; default: throw new NotSupportedException(); } }
/// <summary> /// Creates an ExcelCellElement instance. /// </summary> /// <param name="windowHandle">The window handle.</param> /// <param name="cellInfo">The cell info.</param> /// <param name="manager">The technology manager.</param> internal ExcelCellElement(IntPtr windowHandle, ExcelCellInfo cellInfo, ExcelTechnologyManager manager) : base(windowHandle, manager) { this.CellInfo = cellInfo; }
/// <summary> /// Gets the Range (cell) from the cell info. /// </summary> /// <param name="cellInfo">The cell info.</param> /// <returns>The Range.</returns> private Range GetCell(ExcelCellInfo cellInfo) { Range cell = null; Worksheet ws = GetWorksheet(cellInfo.Parent); if (ws != null) { cell = ws.Cells[cellInfo.RowIndex, cellInfo.ColumnIndex] as Range; } return cell; }
/// <summary> /// Sets focus on a given cell. /// </summary> /// <param name="cellInfo">The cell info.</param> public void SetFocus(ExcelCellInfo cellInfo) { // Use Excel's Object Model to get the required. Worksheet ws = GetWorksheet(cellInfo.Parent); if (ws != null) { // There could be some other cell under editing. Exit that mode. ExitPreviousEditing(ws); ws.Activate(); Range cell = GetCell(cellInfo); if (cell != null) { cell.Activate(); } } }
/// <summary> /// Sets the property of a given cell. /// </summary> /// <param name="cellInfo">The cell info.</param> /// <param name="propertyName">The name of the property.</param> /// <param name="propertyValue">The value of the property.</param> public void SetCellProperty(ExcelCellInfo cellInfo, string propertyName, object propertyValue) { // Use Excel's Object Model to get the required. Range cell = GetCell(cellInfo); if (cell == null) { throw new InvalidOperationException(); } switch (propertyName) { case PropertyNames.Value: cell.Value = propertyValue; break; case PropertyNames.WidthInChars: cell.ColumnWidth = propertyValue; break; case PropertyNames.HeightInPoints: cell.RowHeight = propertyValue; break; case PropertyNames.Formula: cell.Formula = propertyValue; break; case PropertyNames.WrapText: cell.WrapText = propertyValue; break; default: throw new NotSupportedException(); } }
/// <summary> /// Scrolls a given cell into view. /// </summary> /// <param name="cellInfo">The cell info.</param> public void ScrollIntoView(ExcelCellInfo cellInfo) { // Use Excel's Object Model to get the required. Worksheet ws = GetWorksheet(cellInfo.Parent); if (ws != null) { ws.Activate(); double[] rect = this.GetBoundingRectangle(cellInfo); this.application.ActiveWindow.ScrollIntoView((int)rect[0], (int)rect[1], (int)rect[2], (int)rect[3]); } }