CellRange module

class CellRange.CellRange

Bases: XlsRange

Represents a range of cells in an Excel worksheet.

This class extends XlsRange and provides properties and methods for manipulating ranges of cells in Excel worksheets, including operations such as copying, moving, merging, and formatting. It also provides access to cell comments, borders, styles, and other range-specific functionality.

Activate() CellRange

Active single cell in the worksheet

AddComment

Adds a comment to the range.

Parameters:

comment – Comment to add

property Borders: BordersCollection

Returns a Borders collection that represents the borders of a style or a range of cells (including a range defined as part of a conditional format). Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Set text
worksheet["C2"].Text = "Sample"
#Set borders
borders = worksheet["C2"].Borders
#Set line style
borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin
borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
#Set border color
borders[BordersLineType.EdgeTop].Color = Color.Red
borders[BordersLineType.EdgeBottom].Color = Color.Red
#Save to file
workbook.SaveToFile("CellFormats.xlsx")
property Comment: ExcelComment

Returns a Comment object that represents the comment associated with the cell in the upper-left corner of the range. Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Adding comments to a cell
worksheet.Range["A1"].AddComment().Text = "Comments"
#Add Rich Text Comments
range = worksheet.Range["A6"]
range.AddComment().RichText.Text = "RichText"
rtf = range.Comment.RichText
#Formatting first 4 characters
redFont = workbook.CreateFont()
redFont.IsBold = true
redFont.Color = Color.Red
rtf.SetFont(0, 3, redFont)
#Save to file
workbook.SaveToFile("DataValidation.xlsx")
Copy

Copies the range to the specified range.

Parameters:

destRange – Destination range

Returns:

Destination range

property EndCell: CellRange

Returns a Range object that represents the cell at the end of the region that contains the source range.

property EntireColumn: CellRange

Returns a Range object that represents the entire column (or columns) that contains the specified range.

property EntireRow: CellRange

Returns a Range object that represents the entire row (or rows) that contains the specified range.

FindAll(findValue: str, flags: FindType, findOptions: ExcelFindOptions) ListCellRanges

Finds the all cells with specified string value.

Parameters:
  • findValue – Value to search.

  • flags – Type of value to search.

  • findOptions – Way to search.

Returns:

All found cells, or Null if value was not found.

FindAllBool(boolValue: bool) ListCellRanges

Finds the cell with the input bool.

Parameters:

boolValue – Bool value to search for

Returns:

Found ranges

FindAllDateTime(dateTimeValue: DateTime) ListCellRanges

Finds the cell with the input datetime.

Parameters:

dateTimeValue – DateTime value to search for

Returns:

Found ranges

FindAllNumber(doubleValue: float, formulaValue: bool) ListCellRanges

Finds the cell with the input double.

Parameters:
  • doubleValue – Double value to search for

  • formulaValue – Indicates whether to find formula value

Returns:

Found ranges

FindAllString(stringValue: str, formula: bool, formulaValue: bool) ListCellRanges

Finds the cell with the input string.

Parameters:
  • stringValue – String value to search for

  • formula – Indicates whether include formula

  • formulaValue – Indicates whether include formula value

Returns:

Found ranges

FindAllTimeSpan(timeSpanValue: TimeSpan) ListCellRanges

Finds the cell with input timespan

Parameters:

timeSpanValue – time span value to search for

Returns:

Found ranges

FindBool(boolValue: bool) CellRange

Finds the cell with the input bool.

Parameters:

boolValue – Bool value to search for

Returns:

Found range

FindDateTime(dateTimeValue: DateTime) CellRange

Finds the cell with the input datetime.

Parameters:

dateTimeValue – Datetime value to search for

Returns:

Found range

FindNumber(doubleValue: float, formulaValue: bool) CellRange

Finds the cell with the input double.

Parameters:
  • doubleValue – Double value to search for

  • formulaValue – Indicates whether includes formula value to search for

Returns:

Found range

FindString(stringValue: str, formula: bool, formulaValue: bool) CellRange

Finds the cell with the input string.

Parameters:
  • stringValue – String value to search for

  • formula – Indicates whether includes formula to search for

  • formulaValue – Indicates whether includes formula value to search for

Returns:

Found range

FindTimeSpan(timeSpanValue: TimeSpan) CellRange

Finds the cell with the input time span.

Parameters:

timeSpanValue – Time span value to search for.

Returns:

Found range.

GetDependentRanges(isAll: bool) ListCellRanges
GetReferRanges() ListReferRangeAreas
Intersect(range: CellRange) CellRange

Get intersection range with the specified range.

Parameters:

range – Range which to intersect.

Returns:

Range intersection.

Merge

Creates a merged cell from the specified Range object.

Parameters:

range – The Range to merge with.

Returns:

Merged ranges.

Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Merge range
worksheet["A2:B2"].Merge()
#Get mergable range
mergableRange = worksheet["A2"].MergeArea.Merge(worksheet["C2"])
#Check mergable Area
Console.Write(mergableRange.RangeAddressLocal)
#Save to file
workbook.SaveToFile("Intersect.xlsx")
property MergeArea: CellRange

Returns a Range object that represents the merged range containing the specified cell. Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Set text
worksheet["C2"].Text = "Sample text in cell"
#Set merge
worksheet["C2:D3"].Merge()
#Check merge area
Console.Write(worksheet["C2"].MergeArea.AddressLocal)
Move

Moves the cells to the specified Range.

Parameters:

destination – Destnation Range.

property RichText: RichText

Returns a RichTextString object that represents the rich text style. Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Create style
style = workbook.Styles.Add("CustomStyle")
#Set rich text
richText = worksheet["C2"].RichText
richText.Text = "Sample text"
#Set rich text font
font = style.Font
font.IsBold = true
richText.SetFont(0, 5, font)
#Save to file
workbook.SaveToFile("RichText.xlsx")
SetDataValidation(dataValidation: Validation)

Sets data validation for the range.

Parameters:

dv – Data validation to set.

property Style: CellStyle

Returns a Style object that represents the style of the specified range. Example:

#Create worksheet
workbook = Workbook()
worksheet = workbook.Worksheets[0]
#Set text
worksheet["C2"].Text = "Sample"
#Add and set style
style = workbook.Styles.Add("BorderStyle")
style.Color = Color.Red
worksheet["C2"].Style = style
#Save to file
workbook.SaveToFile("Style.xlsx")
property Worksheet: Worksheet

Returns a worksheet object that represents the worksheet containing the specified range.

get_Item

Gets cell range. Row and column indexes are one-based.

Parameters:
  • row

  • column

set_Item(row: int, column: int, value: CellRange)
class CellRange.ListCellRanges(ptr)

Bases: IList[CellRange]

Represents a collection of cell ranges in an Excel worksheet.

This class implements IList<CellRange> and provides functionality for working with collections of cell ranges, such as those returned by search operations or range selections. It allows for iterating through multiple cell ranges and accessing individual ranges within the collection.

GetEnumerator() IEnumerator