Returns the used range on the specified worksheet.
In the Spreadsheet document model, empty cells in a worksheet do not exist until they are accessed in some way or modified. The GetUsedRange method returns a worksheet range that complies with the requirements listed below.
The used range is a continuous range of non-empty cells. The top-left cell of the used range is a cell located at the intersection of the topmost row and the leftmost column containing non-empty cells. The bottom-right cell of the used range is a cell located at the intersection of the lowest row and the rightmost column containing non-empty cells.
The used range is extended to include the leftmost and rightmost empty columns containing visual formatting (fill or border). However, the returned range cannot include more than 8192 columns. Hidden columns that contain no data or formatting options applied, as well as empty columns with a custom width are not included in the used range.
The used range is extended to include the topmost and bottommost empty rows containing visual formatting (fill or border).
The used range includes all the tables regardless their visibility state (a table occupies a range determined by the Table.Range property).
Pictures are not included in the used range.
Merged cells are included in the used range, even if they are located in hidden columns or rows.
- Conditional formatting is ignored.
The following example shows how to use the GetUsedRange method to restrict the number of visible rows and columns in a worksheet:
// Access the active worksheet. Worksheet worksheet = spreadsheetControl.ActiveWorksheet; // Get the range containing non-empty cells. CellRange usedRange = worksheet.GetUsedRange(); // Restrict the worksheet's visible area to the used range. spreadsheetControl.WorksheetDisplayArea.SetSize(worksheet.Index, usedRange.ColumnCount, usedRange.RowCount);