# Importing Data into AdapTable

Canonical page: https://www.adaptabletools.com/docs/handbook-importing

- Data Import allows run-time users to update data in AdapTable in a single workflow
- The imported data can update existing rows, provide new rows or populate the entire grid
- The Data Import wizard facilitates the import process via a series of steps, including Column matching
- Validation is also available to ensure that the data being imported meets custom business rules

AdapTable enables data to be imported into AG Grid at run-time.

This data can be imported in 3 forms:

- JSON file
- CSV file
- Text (i.e. copy and paste)

For paste, AdapTable parses the text with its default behaviour; you can supply a custom `textHandler` in [Data Import Options](https://www.adaptabletools.com/docs/technical-reference-importing/index.md) — see [Configuring Data Import](https://www.adaptabletools.com/docs/handbook-importing-configuring/index.md).

The data can consist of either updates to existing rows in the Grid, or details of new rows. Rows are matched to the grid using the [primary key](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md) column (`primaryKey` in AdapTable Options).

The AdapTable-provided handlers for CSV and JSON files can be replaced, and / or extended, by [custom file handlers](https://www.adaptabletools.com/docs/handbook-importing-configuring/index.md)

## Topics in this Section

| Topic                                                                                       | Description                                                                            |
| ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| [Configuring Data Import](https://www.adaptabletools.com/docs/handbook-importing-configuring/index.md)        | Custom file handling<br/>paste (`textHandler`)<br/>custom apply (`handleImportedData`) |
| [Data Import Technical Reference](https://www.adaptabletools.com/docs/technical-reference-importing/index.md) | Data Import Options<br/>Data Import API<br/> `DataImported` event                      |

## Updating Data

The most common use case is when the data being imported overrides that which is already in the Grid.

**Example: Data Import - Update Rows**

Updating Data using Data Import

- This example shows how to use Data Import to **update existing** rows in AdapTable
- Click to download either <DownloadFile href="/data/updateframeworks.json">updateframeworks.json</DownloadFile> or <DownloadFile href="/data/updateframeworks.csv">updateframeworks.csv</DownloadFile> — both contain (the same) changes to the first 2 rows (react and angular)
- Then upload that file in the Data Import wizard
- We have added [Flashing Cells](https://www.adaptabletools.com/docs/handbook-flashing-cell/index.md) to the grid (and increased duration to 3 seconds) so you can see the changes to the first 2 rows

- Click the Import Data button in the Dashboard (or use a menu option) to open the Wizard
- In the first step load one of the 2 files and click "Import"

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  [template]
  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: 'Data Import - Update Rows',

  initialState: {
    Dashboard: {
      ModuleButtons: ['DataImport'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export'],
        },
      ],
    },
    FlashingCell: {
      FlashingCellDefinitions: [
        {
          Name: 'Flashing_Cell_Any_Change',
          Scope: {
            All: true,
          },
          Rule: 'ANY_CHANGE()',
          FlashDuration: 3000,
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'license',
            'github_watchers',
            'has_projects',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Adding Whole Rows

The imported data can also consist of new rows which AdapTable will add to the grid.

It is possible to import data which contains both new rows and updates to existing rows

**Example: Data Import - Add Rows**

Adding New Rows using Data Import

- This example shows how to use Data Import to **add new** rows into AdapTable
- Click to download either <DownloadFile href="/data/addframeworks.json">addframeworks.json</DownloadFile> or <DownloadFile href="/data/addframeworks.csv">addframeworks.csv</DownloadFile> — both contain (the same) 2 rows to add
- Then upload that file in the Data Import wizard
- We have added a `ROW_ADDED` [Row Change Alert](https://www.adaptabletools.com/docs/handbook-alerting-row-change/index.md) with a highlight row behaviour to show the rows that were added

- Click the Import Data button in the Dashboard (or use a menu option) to open the Wizard
- In the first step load one of the 2 files and click "Import"

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  [template]
  rowData.ts
  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: 'Data Import - Add Rows',

  initialState: {
    Dashboard: {
      ModuleButtons: ['DataImport'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export'],
        },
      ],
    },
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-row-added',
          Rule: 'ROW_ADDED{}',
          MessageType: 'Success',
          Scope: {
            All: true,
          },
          AlertProperties: {
            DisplayNotification: false,
            HighlightRow: {
              BackColor: 'var(--ab-color-palette-28)',
              ForeColor: 'var(--ab-color-palette-14)',
            },
          },
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'license',
            'github_watchers',
            'has_projects',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/rowData.ts`**

```ts
export interface WebFramework {
  id: number;
  name: string;
  full_name: string;
  html_url: string;
  description: string;
  created_at: string;
  updated_at: string;
  pushed_at: string;
  homepage: string;
  github_stars: number;
  language: string;
  forks_count: number;
  open_issues_count: number;
  license: string;
  topics: string[];
  github_watchers: number;
  has_projects: boolean;
  has_wiki: boolean;
  has_pages: boolean;
  closed_issues_count: number;
  open_pr_count: number;
  closed_pr_count: number;
  week_issue_change: number;
}

export const rowData: WebFramework[] = [
  {
    id: 10270250,
    name: 'react',
    full_name: 'facebook/react',
    html_url: 'https://github.com/facebook/react',
    description:
      'A declarative, efficient, and flexible JavaScript library for building user interfaces.',
    created_at: new Date('2013-05-24T16:15:54').toDateString(),
    updated_at: new Date('2021-12-20T09:03:49').toDateString(),
    pushed_at: new Date('2021-12-19T14:34:59').toDateString(),
    homepage: 'https://reactjs.org',
    github_stars: 179429,
    language: 'JavaScript',
    forks_count: 36403,
    open_issues_count: 920,
    license: 'MIT License',
    topics: ['declarative', 'frontend', 'javascript', 'library', 'react', 'ui'],
    github_watchers: 6671,
    has_projects: true,
    has_wiki: true,
    has_pages: true,
    closed_issues_count: 10326,
    open_pr_count: 249,
    closed_pr_count: 11408,
    week_issue_change: 5,
  },
  {
    id: 24195339,
    name: 'angular',
    full_name: 'angular/angular',
    html_url: 'https://github.com/angular/angular',
    description: 'The modern web developer’s platform',
    created_at: new Date('2014-09-18T16:12:01').toDateString(),
    updated_at: new Date('2021-12-20T08:37:07').toDateString(),
    pushed_at: new Date('2021-12-19T21:54:01').toDateString(),
    homepage: 'https://angular.io',
    github_stars: 78348,
    language: 'TypeScript',
    forks_count: 20569,
    open_issues_count: 1889,
    license: 'MIT License',
    topics: [
      'angular',
      'javascript',
      'pwa',
      'typescript',
      'web',
      'web-framework',
      'web-performance',
    ],
    github_watchers: 3119,
    has_projects: true,
    has_wiki: false,
    has_pages: false,
    closed_issues_count: 21706,
    open_pr_count: 159,
    closed_pr_count: 20052,
    week_issue_change: 12,
  },
  {
    id: 11730342,
    name: 'vue',
    full_name: 'vuejs/vue',
    html_url: 'https://github.com/vuejs/vue',
    description:
      '🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.',
    created_at: new Date('2013-07-29T03:24:51').toDateString(),
    updated_at: new Date('2021-12-20T08:47:06').toDateString(),
    pushed_at: new Date('2021-12-20T08:10:16').toDateString(),
    homepage: 'http://vuejs.org',
    github_stars: 191435,
    language: 'JavaScript',
    forks_count: 30945,
    open_issues_count: 546,
    license: 'MIT License',
    topics: ['framework', 'frontend', 'javascript', 'vue'],
    github_watchers: 6187,
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 9318,
    open_pr_count: 224,
    closed_pr_count: 1878,
    week_issue_change: -9,
  },
  {
    id: 74293321,
    name: 'svelte',
    full_name: 'sveltejs/svelte',
    html_url: 'https://github.com/sveltejs/svelte',
    description: 'Cybernetically enhanced web apps',
    created_at: new Date('2016-11-20T18:13:05').toDateString(),
    updated_at: new Date('2021-12-20T08:56:37').toDateString(),
    pushed_at: new Date('2021-12-19T14:53:30').toDateString(),
    homepage: 'https://svelte.dev',
    github_stars: 53935,
    language: 'TypeScript',
    forks_count: 2584,
    open_issues_count: 604,
    license: 'MIT License',
    topics: ['compiler', 'template', 'ui'],
    github_watchers: 874,
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 3549,
    open_pr_count: 104,
    closed_pr_count: 2738,
    week_issue_change: 14,
  },
  {
    id: 224663696,
    name: 'alpine',
    full_name: 'alpinejs/alpine',
    html_url: 'https://github.com/alpinejs/alpine',
    description:
      'A rugged, minimal framework for composing JavaScript behavior in your markup. ',
    created_at: new Date('2019-11-28T13:51:55').toDateString(),
    updated_at: new Date('2021-12-20T08:12:43').toDateString(),
    pushed_at: new Date('2021-12-14T16:02:26').toDateString(),
    homepage: 'https://alpinejs.dev',
    github_stars: 19338,
    language: 'HTML',
    forks_count: 837,
    open_issues_count: 19,
    license: 'MIT License',
    topics: [],
    github_watchers: 202,
    has_projects: false,
    has_wiki: false,
    has_pages: false,
    closed_issues_count: 544,
    open_pr_count: 13,
    closed_pr_count: 546,
    week_issue_change: 21,
  },
  {
    id: 95797174,
    name: 'lit',
    full_name: 'lit/lit',
    html_url: 'https://github.com/lit/lit',
    description:
      'Lit is a simple library for building fast, lightweight web components.',
    created_at: new Date('2017-06-29T16:27:16').toDateString(),
    updated_at: new Date('2021-12-20T07:20:35').toDateString(),
    pushed_at: new Date('2021-12-17T03:45:26').toDateString(),
    homepage: 'https://lit.dev',
    github_stars: 9752,
    language: 'JavaScript',
    forks_count: 528,
    open_issues_count: 207,
    license: 'BSD 3-Clause',
    topics: ['html-templates', 'lit', 'lit-element', 'lit-html'],
    github_watchers: 204,
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 895,
    open_pr_count: 53,
    closed_pr_count: 1138,
    week_issue_change: -45,
  },
  {
    id: 76694515,
    name: 'stimulus',
    full_name: 'hotwired/stimulus',
    html_url: 'https://github.com/hotwired/stimulus',
    description: 'A modest JavaScript framework for the HTML you already have',
    created_at: new Date('2016-12-17T00:19:29').toDateString(),
    updated_at: new Date('2021-12-20T07:20:01').toDateString(),
    pushed_at: new Date('2021-12-17T20:47:34').toDateString(),
    homepage: 'https://stimulus.hotwired.dev/',
    github_stars: 11049,
    language: 'TypeScript',
    forks_count: 316,
    open_issues_count: 17,
    license: 'MIT License',
    topics: [],
    github_watchers: 221,
    has_projects: false,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 282,
    open_pr_count: 5,
    closed_pr_count: 191,
    week_issue_change: -11,
  },
  {
    id: 1801829,
    name: 'ember.js',
    full_name: 'emberjs/ember.js',
    html_url: 'https://github.com/emberjs/ember.js',
    description:
      'Ember.js - A JavaScript framework for creating ambitious web applications',
    created_at: new Date('2011-05-25T23:39:40').toDateString(),
    updated_at: new Date('2021-12-19T07:57:31').toDateString(),
    pushed_at: new Date('2021-12-20T07:15:54').toDateString(),
    homepage: 'https://emberjs.com',
    github_stars: 22093,
    language: 'JavaScript',
    forks_count: 4253,
    open_issues_count: 427,
    license: 'MIT License',
    topics: ['ember', 'hacktoberfest', 'javascript', 'javascript-framework'],
    github_watchers: 908,
    has_projects: true,
    has_wiki: false,
    has_pages: false,
    closed_issues_count: 6187,
    open_pr_count: 94,
    closed_pr_count: 9574,
    week_issue_change: -22,
  },
  {
    id: 82095231,
    name: 'stencil',
    full_name: 'ionic-team/stencil',
    html_url: 'https://github.com/ionic-team/stencil',
    description:
      'A toolchain for building scalable, enterprise-ready component systems on top of TypeScript.',
    created_at: new Date('2017-02-15T18:57:07').toDateString(),
    updated_at: new Date('2021-12-20T05:46:50').toDateString(),
    pushed_at: new Date('2021-12-16T21:15:17').toDateString(),
    homepage: 'https://stenciljs.com',
    github_stars: 9884,
    language: 'TypeScript',
    forks_count: 641,
    open_issues_count: 499,
    license: 'Other',
    topics: [
      'custom-elements',
      'ionic',
      'progressive-web-app',
      'pwa',
      'ssg',
      'ssr',
      'static-site-generator',
      'stencil',
      'stenciljs',
      'typescript',
      'webcomponents',
    ],
    github_watchers: 203,
    has_projects: true,
    has_wiki: false,
    has_pages: false,
    closed_issues_count: 1929,
    open_pr_count: 46,
    closed_pr_count: 743,
    week_issue_change: 12,
  },
  {
    id: 130884470,
    name: 'solid',
    full_name: 'solidjs/solid',
    html_url: 'https://github.com/solidjs/solid',
    description:
      'A declarative, efficient, and flexible JavaScript library for building user interfaces.',
    created_at: new Date('2018-04-24T16:36:27').toDateString(),
    updated_at: new Date('2021-12-20T08:29:47').toDateString(),
    pushed_at: new Date('2021-12-19T21:06:01').toDateString(),
    homepage: 'https://solidjs.com',
    github_stars: 13119,
    language: 'TypeScript',
    forks_count: 330,
    open_issues_count: 21,
    license: 'MIT License',
    topics: [
      'declarative',
      'fine-grained',
      'javascript',
      'jsx',
      'performance',
      'proxies',
      'reactive',
      'solid',
    ],
    github_watchers: 161,
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 366,
    open_pr_count: 3,
    closed_pr_count: 164,
    week_issue_change: 23,
  },
  {
    id: 17814354,
    name: 'mithril.js',
    full_name: 'MithrilJS/mithril.js',
    html_url: 'https://github.com/MithrilJS/mithril.js',
    description: 'A JavaScript Framework for Building Brilliant Applications',
    created_at: new Date('2014-03-17T01:59:39').toDateString(),
    updated_at: new Date('2021-12-20T03:17:55').toDateString(),
    pushed_at: new Date('2021-11-24T10:16:47').toDateString(),
    homepage: 'https://mithril.js.org',
    github_stars: 13064,
    language: 'JavaScript',
    forks_count: 940,
    open_issues_count: 173,
    license: 'MIT License',
    topics: [
      'framework',
      'javascript',
      'mithril',
      'router',
      'vdom',
      'virtual-dom',
      'xhr',
    ],
    github_watchers: 324,
    has_projects: true,
    has_wiki: true,
    has_pages: true,
    closed_issues_count: 1368,
    open_pr_count: 28,
    closed_pr_count: 1144,
    week_issue_change: 57,
  },
  {
    id: 167174,
    name: 'jquery',
    full_name: 'jquery/jquery',
    html_url: 'https://github.com/jquery/jquery',
    description: 'jQuery JavaScript Library',
    created_at: new Date('2009-04-03T15:20:14').toDateString(),
    updated_at: new Date('2021-12-20T06:13:48').toDateString(),
    pushed_at: new Date('2021-12-17T16:50:04').toDateString(),
    homepage: 'https://jquery.com',
    github_stars: 55666,
    language: 'JavaScript',
    forks_count: 20187,
    open_issues_count: 84,
    license: 'MIT License',
    topics: ['jquery'],
    github_watchers: 3306,
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 2083,
    open_pr_count: 13,
    closed_pr_count: 2692,
    week_issue_change: 15,
  },
];
```

## Importing All Data

Data Import can also be used to populate an empty Grid with a full set of data.

**Example: Data Import - Adding all data**

Adding All Data using Data Import

- This example shows how to use Data Import to **load a full** set of rows into AdapTable — the grid opens with no data provided
- Click to download either <DownloadFile href="/data/allframeworks.json">allframeworks.json</DownloadFile> or <DownloadFile href="/data/allframeworks.csv">allframeworks.csv</DownloadFile> — both contain the same data to add; upload the file in the Data Import wizard
- Note: the Grid is fully populated including 3 [Calculated Columns](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md) which are evaluated using the imported data

- Click the Import Data button in the Dashboard (or use a menu option) to open the Wizard
- In the first step load one of the 2 files and click "Import"

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts  [template]
  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: 'Data Import - Import All Data',
  initialState: {
    Dashboard: {
      ModuleButtons: ['DataImport'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export'],
        },
      ],
    },

    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'license',
            'github_watchers',
            'has_projects',
            'open_issues',
            'closed_issues',
            'open_prs',
            'closed_prs',
            'subscribersRatio',
            'open-total-issue-ratio',
            'open-total-pr-ratio',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          FriendlyName: 'Subscribers Ratio',
          ColumnId: 'subscribersRatio',
          Expression: '[github_stars] / [github_watchers]',
          CalculatedColumnSettings: {
            Filterable: true,
            Groupable: true,
            DataType: 'number',
          },
        },
        {
          FriendlyName: 'Issues Open/Total Ratio',
          ColumnId: 'open-total-issue-ratio',
          Expression: '[open_issues_count] / ([open_issues_count] +[closed_issues_count])',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          FriendlyName: 'PR Open/Total Ratio',
          ColumnId: 'open-total-pr-ratio',
          Expression: '[open_pr_count] / ([open_pr_count] + [closed_pr_count])',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-subscribersRatio',
          Scope: {
            ColumnIds: [
              'subscribersRatio',
              'open-total-issue-ratio',
              'open-total-pr-ratio',
            ],
          },

          DisplayFormat: {
            Formatter: 'NumberFormatter',
            Options: {
              FractionDigits: 2,
            },
          },
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {ColDef, GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData, WebFramework} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions<WebFramework> = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: [],
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```

**`src/columnDefs.ts`**

```ts
import {ColDef} from 'ag-grid-enterprise';

export const columnDefs: ColDef<any>[] = [
  {
    field: 'name',
    cellDataType: 'text',
    sortable: true,
  },
  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'has_wiki',
    headerName: 'Has Wiki',
    cellDataType: 'boolean',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {
    field: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {field: 'has_projects', headerName: 'Has Projects', cellDataType: 'boolean'},
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
];
```

## Column Matching

The Import Data wizard attempts to match all the columns in the inputted data against those in the Grid's Column definitions.

This is done by checking the column in the header of the input file against an existing ColumnId or FriendlyName.

- The logic is that first AdapTable will try to find a Column where the `columnId` matches that in the input source
- If nothing is found, then it will try to match using a Column `friendlyName` (aka the Header)

If there are no matches AdapTable will display a dropdown with a list of all the current columns so the user can pick the correct one.

**Example: Data Import - Matching Columns**

Data Import with Column Matching

- This example shows how the Data Import will try to match Columns automatically, and allow the user to provide those which cannot be matched
- Click to download either <DownloadFile href="/data/columnmatchingframeworks.json">columnmatchingframeworks.json</DownloadFile> or <DownloadFile href="/data/columnmatchingframeworks.csv">columnmatchingframeworks.csv</DownloadFile>
- Both files contain the same 2 rows to update as in the Updating Rows demo but with some columns in the import file **not** using the columnId:
  - Language and Licence are both Friendly Names / Headers
  - Stars and Watchers don't match either Column Id or Friendly Name
- In all 4 cases the Columns section of the Import Data wizard asks the user to supply the relevant column

- Click the Import Data button in the Dashboard (or use a menu option) to open the Wizard
- In the first step load one of the 2 files and click "Import"
- In the Columns step of the Wizard, supply the names of the columns that could not be matched automatically

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  [template]
  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: 'Data Import - Column Matching',

  initialState: {
    Dashboard: {
      ModuleButtons: ['DataImport'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export'],
        },
      ],
    },
    FlashingCell: {
      FlashingCellDefinitions: [
        {
          Name: 'Flashing_Cell_Any_Change',
          Scope: {
            All: true,
          },
          Rule: 'ANY_CHANGE()',
          FlashDuration: 3000,
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'license',
            'github_watchers',
            'has_projects',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Validating Import Data

The data file can be validated before it is imported to make sure that it fits with business requirements.

This is separate to any [Data Change Alerts](https://www.adaptabletools.com/docs/handbook-alerting-data-change/index.md) which fire **after** the data has been imported into the Grid

Data Validation is done using the `validate` function property in [Data Import Options](https://www.adaptabletools.com/docs/technical-reference-importing/index.md).

### `validate`

Function to validate the Imported Data

  [`DataImportValidationError[]`](https://www.adaptabletools.com/docs/reference/dataimportvalidationerror.md)

Use this function to validate the data being imported into AG Grid via the Data Import wizard.

It allows for bad data to be rejected before it gets added to the Grid.

The function receives a [`DataImportValidateContext`](https://www.adaptabletools.com/docs/reference/dataimportvalidatecontext.md) and returns a [`DataImportValidationError`](https://www.adaptabletools.com/docs/reference/dataimportvalidationerror.md) array.

The [`DataImportValidateContext`](https://www.adaptabletools.com/docs/reference/dataimportvalidatecontext.md) object contains just the row data being imported:

| Property | Type | Description |
| --- | --- | --- |
| [rowData](https://www.adaptabletools.com/docs/reference/dataimportvalidatecontext.md#rowdata) | `T` | Imported Row Data |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/dataimportvalidatecontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

The [`DataImportValidationError`](https://www.adaptabletools.com/docs/reference/dataimportvalidationerror.md) object includes the Column where validation fails and an error message:

| Property | Type | Description |
| --- | --- | --- |
| [columnId](https://www.adaptabletools.com/docs/reference/dataimportvalidationerror.md#columnid) | `string` | Column which contains the Error |
| [error](https://www.adaptabletools.com/docs/reference/dataimportvalidationerror.md#error) | `string` | The validation error text |

```ts {3}
const adaptableOptions: AdaptableOptions = {
  dataImportOptions: {
    validate: (context: DataImportValidateContext) => {
      const rowData = context.rowData;
      return rowData.license !== 'MIT License' && rowData.license !== 'Other'
        ? [
            {
              columnId: 'license',
              error: 'License must be "MIT License" or "Other"',
            },
          ]
        : [];
    },
  },
};
```

**Example: Data Import - Validation**

Validating Data in Data Import

- This example shows how to **validate** the data in Data Import
- Click to download either <DownloadFile href="/data/updateframeworks.json">updateframeworks.json</DownloadFile> or <DownloadFile href="/data/updateframeworks.csv">updateframeworks.csv</DownloadFile> — both contain (the same) changes to the first 2 rows (react and angular)
- But this time we have added a Validation rule that the `License` must be either _MIT License_ or _Other_
- Then upload that file in the Data Import wizard
### Expand to the validate function

```ts
 dataImportOptions: {
  validate: (context: DataImportValidateContext) => {
    const rowData = context.rowData;
    return rowData.license !== 'MIT License' && rowData.license !== 'Other'
      ? [
          {
            columnId: 'license',
            error: 'License must be "MIT License" or "Other"',
          },
        ]
      : [];
  },
},
```

- Click the Import Data button in the Dashboard (or use a menu option) to open the Wizard
- In the first step load one of the 2 files and click "Import"
- Go to the Validation step and note that the License column contains an error in each row
- Change one of the values to "Other" and the other "MIT License" and note that this passes validation and you are able to import

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  [template]
  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,
  DataImportValidateContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Data Import - Validating',
  dataImportOptions: {
    validate: (context: DataImportValidateContext) => {
      const rowData = context.rowData;
      return rowData.license !== 'MIT License' && rowData.license !== 'Other'
        ? [
            {
              columnId: 'license',
              error: 'License must be "MIT License" or "Other"',
            },
          ]
        : [];
    },
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['DataImport'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export'],
        },
      ],
    },
    FlashingCell: {
      FlashingCellDefinitions: [
        {
          Name: 'Flashing_Cell_Any_Change',
          Scope: {
            All: true,
          },
          Rule: 'ANY_CHANGE()',
          FlashDuration: 3000,
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'license',
            'github_watchers',
            'has_projects',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

### FAQ

**Does the imported data need to be on contiguous rows?**
No, you can update any rows in the Grid; there is no need for them to form a "block"

**How does the import know which row to update?**
Matching uses the value in the import for your **primary key** column (see `primaryKey` in [AdapTable Options](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md)). New primary key values create new rows.
