# FSelection

> Language fallback: requested `ko-KR`; content is `en-US`.

- Human documentation: [https://docs.univer.ai/ko-KR/reference/facade/selection](https://docs.univer.ai/ko-KR/reference/facade/selection)

- Agent Markdown: [https://docs.univer.ai/ko-KR/reference/facade/selection.md](https://docs.univer.ai/ko-KR/reference/facade/selection.md)

- Requested language: `ko-KR`

- Content language: `en-US`

- Documentation version: `1.0.4`

- Source: [facade/selection.mdx](https://github.com/dream-num/documentation/blob/dev/content/reference/facade/selection.mdx)

---

Represents the active selection in the sheet.

> This class should not be instantiated directly. Use factory methods on `univerAPI` instead.

## Access

Access through:

* [`FWorksheet.getSelection()`](https://docs.univer.ai/ko-KR/reference/facade/worksheet.md#getselection)

## Example

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fSelection = fWorksheet.getSelection()
const activeRange = fSelection.getActiveRange()
console.log(activeRange)
```

## Setup

Register [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) or a preset that includes it. In plugin mode, import `@univerjs/sheets/facade`. Additional methods below require their listed plugin packages. See [Facade setup](https://docs.univer.ai/ko-KR/guides/sheets/getting-started/facade.md).

## `@univerjs/sheets`

### `FSelection.getActiveRange`

Represents the active selection in the sheet. Which means the selection contains the active cell.

```typescript
getActiveRange(): FRange | null
```

**Returns**

The active selection.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fRange = fWorksheet.getRange('A10:B11')
fRange.activate()
const fSelection = fWorksheet.getSelection()
console.log(fSelection.getActiveRange().getA1Notation()) // A10:B11
```

**Types:** [`FRange`](https://docs.univer.ai/ko-KR/reference/facade/range.md)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)

### `FSelection.getActiveRangeList`

Represents the active selection list in the sheet.

```typescript
getActiveRangeList(): FRange[]
```

**Returns**

The active selection list.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fSelection = fWorksheet.getSelection()
const activeRangeList = fSelection.getActiveRangeList()
activeRangeList.forEach((range) => {
  console.log(range.getA1Notation())
})
```

**Types:** [`FRange`](https://docs.univer.ai/ko-KR/reference/facade/range.md)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)

### `FSelection.getActiveSheet`

Returns the active sheet in the spreadsheet.

```typescript
getActiveSheet(): FWorksheet
```

**Returns**

The active sheet in the spreadsheet.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fSelection = fWorksheet.getSelection()
const activeSheet = fSelection.getActiveSheet()
console.log(activeSheet.equalTo(fWorksheet))
```

**Types:** [`FWorksheet`](https://docs.univer.ai/ko-KR/reference/facade/worksheet.md)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)

### `FSelection.getCurrentCell`

Represents the current select cell in the sheet.

```typescript
getCurrentCell(): Nullable<ISelectionCell>
```

**Returns**

The primary cell of the current selection, or `null` when none exists.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fRange = fWorksheet.getRange('A10:B11')
fRange.activate()
const fSelection = fWorksheet.getSelection()
const currentCell = fSelection.getCurrentCell()
const { actualRow, actualColumn } = currentCell
console.log(currentCell)
console.log(`actualRow: ${actualRow}, actualColumn: ${actualColumn}`) // actualRow: 9, actualColumn: 0
```

**Types:** [`ISelectionCell`](https://unpkg.com/@univerjs/core/lib/types/sheets/typedef.d.ts) · [`Nullable`](https://unpkg.com/@univerjs/core/lib/types/shared/types.d.ts)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)

### `FSelection.getNextDataRange`

Get the next primary cell in the specified direction. If the primary cell not exists in selections, return null.
The next primary cell in the specified direction is the next cell only within the current selection range.
For example, if the current selection is A1:B2, and the primary cell is B1, the next cell in the right direction is A2 instead of C1.

```typescript
getNextDataRange(direction: Direction): FRange | null
```

**Parameters**

* `direction` — Required. The direction to move the primary cell.The enum value is maybe one of the following: UP(0),RIGHT(1), DOWN(2), LEFT(3).

**Returns**

The next primary cell in the specified direction.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
// make sure the active cell is A1 and selection is A1:B2
const fRange = fWorksheet.getRange('A1:B2')
fRange.activate()

// get the next cell in the right direction, and update the primary cell to the next cell, now the active cell is B1
let fSelection = fWorksheet.getSelection()
const nextCell = fSelection.getNextDataRange(univerAPI.Enum.Direction.RIGHT)
console.log(nextCell?.getA1Notation()) // B1
fSelection = fSelection.updatePrimaryCell(nextCell)

// get the next cell in the right direction, the next cell is A2
const nextCell2 = fSelection.getNextDataRange(univerAPI.Enum.Direction.RIGHT)
console.log(nextCell2?.getA1Notation()) // A2
```

**Types:** [`FRange`](https://docs.univer.ai/ko-KR/reference/facade/range.md) · [`Direction`](https://unpkg.com/@univerjs/core/lib/types/types/enum/direction.d.ts)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)

### `FSelection.updatePrimaryCell`

Update the primary cell in the selection. if the primary cell not exists in selections, add it to the selections and clear the old selections.

```typescript
updatePrimaryCell(cell: FRange): FSelection
```

**Parameters**

* `cell` — Required. The new primary cell to update.

**Returns**

The new selection after updating the primary cell.Because the selection is immutable, the return value is a new selection.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getSheetByName('Sheet1')
if (!fWorksheet) throw new Error('fWorksheet is not available')
const fRange = fWorksheet.getRange('A10:B11')
fRange.activate()
const cell = fWorksheet.getRange('B11')

let fSelection = fWorksheet.getSelection()
fSelection = fSelection.updatePrimaryCell(cell)

const currentCell = fSelection.getCurrentCell()
const { actualRow, actualColumn } = currentCell
console.log(currentCell)
console.log(`actualRow: ${actualRow}, actualColumn: ${actualColumn}`) // actualRow: 10, actualColumn: 1
```

**Types:** [`FSelection`](https://docs.univer.ai/ko-KR/reference/facade/selection.md) · [`FRange`](https://docs.univer.ai/ko-KR/reference/facade/range.md)

**Package:** [`@univerjs/sheets`](https://docs.univer.ai/ko-KR/reference/packages/plugins/univerjs/sheets.md) · [Type definitions](https://unpkg.com/@univerjs/sheets/lib/types/facade/f-selection.d.ts)
