# GC.Spread.Commands.CommandManager

## Content

# Class: CommandManager

[Spread](../modules/GC.Spread).[Commands](../modules/GC.Spread.Commands).CommandManager

## Table of contents

### Constructors

- [constructor](GC.Spread.Commands.CommandManager#constructor)

### Methods

- [execute](GC.Spread.Commands.CommandManager#execute)
- [register](GC.Spread.Commands.CommandManager#register)
- [setShortcutKey](GC.Spread.Commands.CommandManager#setshortcutkey)

## Constructors

### <a id="constructor" name="constructor"></a> constructor

• **new CommandManager**(`context`)

Represents a command manager.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `context` | `Object` | The execution context for all commands in the command manager. |

## Methods

### <a id="execute" name="execute"></a> execute

▸ **execute**(`commandOptions`): `boolean`

Executes a command and adds the command to UndoManager.

**`example`**
```
//For example, the following code executes the autoFitColumn command.
var spread = GC.Spread.Sheets.findControl(document.getElementById("ss"));
spread.commandManager().execute({cmd: "autoFitColumn", sheetName: "Sheet1", columns: [{col: 1}], rowHeader: false, autoFitType: GC.Spread.Sheets.AutoFitType.cell});
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `commandOptions` | `Object` | The options for the command. |

#### Returns

`boolean`

The execute command result.

___

### <a id="register" name="register"></a> register

▸ **register**(`name`, `command`, `key?`, `ctrl?`, `shift?`, `alt?`, `meta?`): `void`

Registers a command with the command manager.

**`example`**
```
//For example, the following code registers the changeBackColor command and then executes the command.
var command = {
  canUndo: true,
  execute: function (context, options, isUndo) {
    var Commands = GC.Spread.Sheets.Commands;
    options.cmd = "changeBackColor";
    if (isUndo) {
      Commands.undoTransaction(context, options);
      return true;
    } else {
      Commands.startTransaction(context, options);
      var sheet = context.getSheetFromName(options.sheetName);
      var cell = sheet.getCell(options.row, options.col);
      cell.backColor(options.backColor);
      Commands.endTransaction(context, options);
      return true;
    }
  }
};
var spread = GC.Spread.Sheets.findControl(document.getElementById("ss"));
var commandManager = spread.commandManager();
commandManager.register("changeBackColor", command);
commandManager.execute({cmd: "changeBackColor", sheetName: spread.getSheet(0).name(), row: 1, col: 2, backColor: "red"});
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `string` | The name of the command. |
| `command` | `Object` | The object that defines the command. |
| `key?` | `number` | The key code. |
| `ctrl?` | `boolean` | `true` if the command uses the Ctrl key; otherwise, `false`. |
| `shift?` | `boolean` | `true` if the command uses the Shift key; otherwise, `false`. |
| `alt?` | `boolean` | `true` if the command uses the Alt key; otherwise, `false`. |
| `meta?` | `boolean` | `true` if the command uses the Command key on the Macintosh or the Windows key on Microsoft Windows; otherwise, `false`. |

#### Returns

`void`

___

### <a id="setshortcutkey" name="setshortcutkey"></a> setShortcutKey

▸ **setShortcutKey**(`commandName`, `key?`, `ctrl?`, `shift?`, `alt?`, `meta?`): `void`

Binds a shortcut key to a command.

**`example`**
```
//This example changes the behavior of default keys.
var activeSheet = spread.getActiveSheet();
//Change the default Up arrow key action to "Page Up" for the active cell.
spread.commandManager().setShortcutKey('navigationPageUp', GC.Spread.Commands.Key.up, false, false, false, false);
//Change the default Down arrow key action to "Page Down" for the active cell.
spread.commandManager().setShortcutKey('navigationPageDown', GC.Spread.Commands.Key.down, false, false, false, false);
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `commandName` | `string` | The command name, setting commandName to undefined removes the bound command of the shortcut key. |
| `key?` | `number` | The key code, setting the key code to undefined removes the shortcut key of the command. |
| `ctrl?` | `boolean` | `true` if the command uses the Ctrl key; otherwise, `false`. |
| `shift?` | `boolean` | `true` if the command uses the Shift key; otherwise, `false`. |
| `alt?` | `boolean` | `true` if the command uses the Alt key; otherwise, `false`. |
| `meta?` | `boolean` | `true` if the command uses the Command key on the Macintosh or the Windows key on Microsoft Windows; otherwise, `false`. |

#### Returns

`void`
