Worksheet class represents a single sheet within a workbook. It provides methods for managing cells, rows, columns, and worksheet-level properties.
Class Overview
Namespace:PhpOffice\PhpSpreadsheet\Worksheet
Source: src/PhpSpreadsheet/Worksheet/Worksheet.php
A Worksheet contains cells, drawings, charts, and various display and print settings.
Constants
Sheet State
Break Types
Constructor
__construct(?Spreadsheet $parent = null, string $title = 'Worksheet')
Create a new worksheet.
The parent spreadsheet object
The worksheet title (max 31 characters)
Cell Operations
getCell(CellAddress|string|array $coordinate)
Get a cell at a specific coordinate.
Cell coordinate as string (‘A1’), array ([1, 1]), or CellAddress object
The cell object (created if it doesn’t exist)
setCellValue(CellAddress|string|array $coordinate, mixed $value, ?IValueBinder $binder = null)
Set a cell value.
Cell coordinate
Value to set in the cell
Optional custom value binder
setCellValueExplicit(CellAddress|string|array $coordinate, mixed $value, string $dataType)
Set a cell value with explicit data type.
Cell coordinate
Value to set
Explicit data type (see DataType::TYPE_*)
cellExists(CellAddress|string|array $coordinate)
Check if a cell exists.
Cell coordinate to check
True if cell exists
Title and State
getTitle()
Get the worksheet title.
The worksheet title
setTitle(string $title, bool $updateFormulaCellReferences = true, bool $validate = true)
Set the worksheet title.
New title (max 31 characters, cannot contain: * : / \ ? [ ])
Update formula references to this sheet
Validate the title
getSheetState()
Get the sheet visibility state.
One of: ‘visible’, ‘hidden’, or ‘veryHidden’
setSheetState(string $value)
Set the sheet visibility state.
‘visible’, ‘hidden’, or ‘veryHidden’
Dimensions and Ranges
getHighestRow(?string $column = null)
Get the highest row number.
Optional column letter to check specific column
Highest row number
getHighestColumn($row = null)
Get the highest column letter.
Optional row number to check specific row
Highest column letter
getHighestDataRow(?string $column = null)
Get the highest row that contains data.
Optional column letter
Highest row with data
getHighestDataColumn($row = null)
Get the highest column that contains data.
Optional row number
Highest column with data
calculateWorksheetDimension()
Calculate the dimension of the worksheet.
Range string (e.g., ‘A1:D10’)
Row and Column Dimensions
getRowDimension(int $row)
Get row dimension at a specific row.
Row number
Row dimension object
getColumnDimension(string $column)
Get column dimension at a specific column.
Column letter (e.g., ‘A’)
Column dimension object
getColumnDimensionByColumn(int $columnIndex)
Get column dimension by numeric index.
Column index (1-based: 1=A, 2=B, etc.)
Column dimension object
Styling
getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate)
Get style for a cell or range.
Cell address or range (e.g., ‘A1’ or ‘A1:D10’)
Style object
duplicateStyle(Style $style, string $range)
Duplicate a style to a range of cells.
The style to duplicate
Range to apply style to (e.g., ‘A1:D10’)
Page Setup
getPageSetup()
Get page setup settings.
Page setup object
getPageMargins()
Get page margins.
Page margins object
getHeaderFooter()
Get header/footer settings.
Header/footer object
Protection
getProtection()
Get worksheet protection settings.
Protection object
Charts and Drawings
addChart(Chart $chart)
Add a chart to the worksheet.
Chart object to add
The added chart
getChartCount()
Get the number of charts.
Number of charts
getChartByName(string $chartName)
Get a chart by name.
Name of the chart
The chart, or false if not found
Complete Example
Related Classes
- Spreadsheet - Parent workbook container
- Cell - Individual cell within the worksheet
RowDimension- Row height and visibility settingsColumnDimension- Column width and visibility settingsPageSetup- Print and page layout settingsProtection- Worksheet protection settings

