# Smart Edit

Canonical page: https://www.adaptabletools.com/docs/handbook-editing-smart-edit

- Smart Edit applies a single mathematical edit to numerous, numeric cells
- The Operation can be either:
  - one of the 4 System Smart Edit Operations provided by Adaptable
  - a Custom Smart Edit Operation supplied by a developer at design time

Smart Editing enables multiple, contiguous **numeric** cells to be updated with a single mathematical operation.

The System Smart Edit Operations - supplied by AdapTable - are:

- Addition
- Subtraction
- Multiplication
- Division

For example a Smart Edit value of 10 with an operation of Multiply, will multiply all selected cells by 10.

## Applying Smart Edit

Smart Edits can be applied in 3 places in AdapTable:

- Smart Edit [Toolbar](https://www.adaptabletools.com/docs/ui-dashboard-tabs-toolbars/index.md)
- Smart Edit [Tool Panel](https://www.adaptabletools.com/docs/ui-tool-panel/index.md)
- Smart Edit page in the [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md)

The Smart Edit Settings Panel page can also be opened by selecting `Apply Smart Edit` in the [Context Menu](https://www.adaptabletools.com/docs/ui-context-menu/index.md)

### Applying a Smart Edit

All Smart Edit controls operate in a very similar fashion:

Smart Edit requires that all selected cells are from a single column, and that column must be numeric and editable.

The available Smart Edit Operations are: 
- Addition
- Subtraction
- Multiplication
- Division
Multiplication is selected by AdapTable as the default as it the most commonly applied operation.
Additionally developers can provide their own Smart Edit operations via custom functions.

Enter a value into the [Numeric Cell Editor](https://www.adaptabletools.com/docs/handbook-cell-editors-numeric/index.md) in the Smart Edit control.
This is the value which will be applied to the cell.

Adaptable displays a *new values* table showing what the current selected cell values would be if the Smart Edit is applied.
This indicates if any of the proposed edits will break validation.

In the Toolbar and Tool Panel, click the *Preview Results* button to open the Smart Edit page in the [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md), where this table is displayed

Click the button to apply the Smart Edit.

AdapTable re-selects the cells so you can repeat the operation by clicking the button again

**Example: Smart Edit**

Editing numeric Columns using Smart Edit

- Select editable numeric cells in a **single column**, then use Smart Edit on the toolbar to apply an operation (the demo uses multiply by 10)

- Click the Green tick in the Smart Edit toolbar to multiply the selected values by 10

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Smart Edit',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['SmartEdit'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    adaptableApi.columnApi.selectColumn('github_stars');
    adaptableApi.smartEditApi.setSmartEditValue(10);
  }, 200);
};
```

## Custom Smart Edit Operations

Developers can provide Custom Smart Edit Operations in addition to those shipped by AdapTable.

This is done via the `smartEditCustomOperations` property in the `smartEditOptions` section of [`Edit Options`](https://www.adaptabletools.com/docs/reference/editoptions.md).

### `smartEditCustomOperations`

Custom Operations to use in Smart Edit Module

  [`SmartEditCustomOperation[]`](https://www.adaptabletools.com/docs/reference/smarteditcustomoperation.md)

Custom Smart Edit Operations to be provided by Developers in addition to those shipped by AdapTable.

Each Custom Operation has 2 properties:

- `name` - how the Operation is referenced in the Smart Edit dropdown
- `operation` - function invoked by AdapTable when Smart Edit is run, and which returns the new numeric cell value

The operation function receives [`SmartEditOperationContext`](https://www.adaptabletools.com/docs/reference/smarteditoperationcontext.md) defined as follows:

| Property | Type | Description |
| --- | --- | --- |
| [currentCell](https://www.adaptabletools.com/docs/reference/smarteditoperationcontext.md#currentcell) | [`GridCell`](https://www.adaptabletools.com/docs/reference/gridcell.md)`<TData>` | Current selected grid cell - contains column, row and cell value information |
| [smartEditValue](https://www.adaptabletools.com/docs/reference/smarteditoperationcontext.md#smarteditvalue) | `number` | Smart Edit value |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/smarteditoperationcontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

```ts
editOptions: {
  smartEditOptions: {
    smartEditCustomOperations: [
      {
        name: 'Power',
        operation: (context: SmartEditOperationContext) => {
          return Math.pow(context.currentCell.rawValue, context.smartEditValue);
        },
      },
    ],
  },
},
```

**Example: Smart Edit Custom Operations**

Adding bespoke Smart Edit Operations

- This example includes 2 Custom Smart Edit Operations:
  - `Power` - which pows the selected cell by the Smart Edit Value
  - `bps` - which adds the Smart Edit Value as Basis Points 
- Note: we have used methods in [Smart Edit API](https://www.adaptabletools.com/docs/technical-reference-editing/index.md) to set the Smart Edit Operation to `Power`

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableOptions,
  SmartEditOperationContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Smart Edit Custom Operation',
  editOptions: {
    smartEditOptions: {
    customOperations: [
      {
        name: 'Power',
        operation: (context: SmartEditOperationContext) => {
          return Math.pow(context.currentCell.rawValue, context.smartEditValue);
        },
      },
      {
        name: 'bps',
        operation: (context: SmartEditOperationContext) => {
          const initialValue = context.currentCell.rawValue;
          const bpChange = context.smartEditValue / 100 / 100;
          return initialValue + initialValue * bpChange;
          },
        },
      ],
    },
  },
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['SmartEdit'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'week_issue_change',
            'github_stars',
            'topics',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'description',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    adaptableApi.gridApi.selectCellRange({
      columnIds: ['week_issue_change'],
      rowIndexStart: 0,
      rowIndexEnd: 3,
    });
    adaptableApi.smartEditApi.setCustomSmartEditOperation('Power');
    adaptableApi.smartEditApi.setSmartEditValue(2);
  }, 200);
};
```

### Smart Edit and Data Validation

AdapTable will prevent you from applying a Smart Edit which breaks a [Data Validation Rule](https://www.adaptabletools.com/docs/handbook-validating/index.md)

The _new values_ table will indicate - and provide details - if a Smart Edit will break a Validation Rule.

In addition AdapTable does the following:

| Validation       | _Apply Smart Edit_ Button Behaviour                                                      |
| ---------------- | ---------------------------------------------------------------------------------------- |
| All Valid        | Has a success colour (default is green)                                                  |
| Some are Invalid | Has a warning colour (default is amber) and **only** valid edits will be applied         |
| All are Invalid  | Has an error colour (default is red) and is disabled and the operation is not accessible |

### FAQ

**Can I use Smart Edit to bypass Prevent Edit Alerts?**
No, Smart Edit is aware of Data Validation rules and will warn or prevent you as applicable if one is breached.

**Can I run a Smart Edit across multiple columns if they are all numeric?**
No, Smart Edit can only be used on a single column at at time, to ensure that Data Validation rules are adhered to.

**My column contains strings in some cells and numbers in others. Can I use Smart Edit on the numeric cells?**
No, Smart Edit only works where the entire Column is identified as Numeric.

**Can I create custom Smart Edit rules (e.g. to move by bps or other amounts)?**
Yes — provide operations in `smartEditCustomOperations` on [Edit Options](https://www.adaptabletools.com/docs/technical-reference-editing/index.md) (see **Custom Smart Edit Operations** above).

**I don't want to use a formula to update the cell, I just want to replace the current value with a new one?**
You can use [Bulk Update](https://www.adaptabletools.com/docs/handbook-editing-bulk-update/index.md) to replace the cell value with a new one entirely.
