# Selection

> Language fallback: requested `zh-CN`; content is `en-US`.

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

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

- Requested language: `zh-CN`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

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

---

| Packages | `@univerjs/sheets` |
| -------- | ------------------ |

Represents the active selection in the sheet.

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

## Overview

### @univerjs/sheets

| Method                                      | Description                                          |
| ------------------------------------------- | ---------------------------------------------------- |
| [`getActiveRange`](#getactiverange)         | Represents the active selection in the sheet         |
| [`getActiveRangeList`](#getactiverangelist) | Represents the active selection list in the sheet    |
| [`getActiveSheet`](#getactivesheet)         | Returns the active sheet in the spreadsheet          |
| [`getCurrentCell`](#getcurrentcell)         | Represents the current select cell in the sheet      |
| [`getNextDataRange`](#getnextdatarange)     | Get the next primary cell in the specified direction |
| [`updatePrimaryCell`](#updateprimarycell)   | Update the primary cell in the selection             |

## APIs

### Getters & Queries

### `getActiveRange`

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

**Signature**

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

**Returns**

* `FRange` — The active selection.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A10:B11');
fRange.activate();
const fSelection = fWorksheet.getSelection();
console.log(fSelection.getActiveRange().getA1Notation()); // A10:B11
```

Source: 

`@univerjs/sheets`

### `getActiveRangeList`

Represents the active selection list in the sheet.

**Signature**

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

**Returns**

* `FRange[]` — The active selection list.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fSelection = fWorksheet.getSelection();
const activeRangeList = fSelection.getActiveRangeList();
activeRangeList.forEach((range) => {
  console.log(range.getA1Notation());
});
```

Source: 

`@univerjs/sheets`

### `getActiveSheet`

Returns the active sheet in the spreadsheet.

**Signature**

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

**Returns**

* `FWorksheet` — The active sheet in the spreadsheet.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fSelection = fWorksheet.getSelection();
const activeSheet = fSelection.getActiveSheet();
console.log(activeSheet.equalTo(fWorksheet)); // true
```

Source: 

`@univerjs/sheets`

### `getCurrentCell`

Represents the current select cell in the sheet.

**Signature**

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

**Returns**

* `Nullable<ISelectionCell>` — The current select cell info.Pay attention to the type of the return value.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
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
```

Source: 

`@univerjs/sheets`

### `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.

**Signature**

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

**Parameters**

* `direction` `Direction` — *No description*

**Returns**

* `FRange` — The next primary cell in the specified direction.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// 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
```

Source: 

`@univerjs/sheets`

### Setters & Modifiers

### `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.

**Signature**

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

**Parameters**

* `cell` `FRange` — *No description*

**Returns**

* `FSelection` — 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.getActiveSheet();
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
```

Source: 

`@univerjs/sheets`
