- Cell selection
- Row selection
- Column selection
- Get selected cell values
- Remove Selection
- Limitations
- Note
Contact Support
Selection in Angular Spreadsheet component
28 Oct 202417 minutes to read
Selection provides interactive support to highlight the cell, row, or column that you select. Selection can be done through Mouse, Touch, or Keyboard interaction. To enable selection, set mode as Single
|
Multiple in selectionSettings . If you set mode to None , it disables the UI selection. |
- The default value for
mode
inselectionSettings
isMultiple
.
You have the following options in Selection,
- Cell selection
- Row selection
- Column selection
Cell selection
Cell selection is used to select a single or multiple cells. It can be performed using the selectRange
method.
User Interface:
- Click on a cell to select it (or) use the
arrow
keys to navigate to it and select it. - To select a range, select a cell, then use the left mouse button to select and drag over to other cells (or) use the
Shift + arrow
keys to select the range. - To select non-adjacent cells and cell ranges, hold
Ctrl
and select the cells.
You can quickly locate and select specific cells or ranges by entering their names or cell references in the Name box, which is located to the left of the formula bar, and also select named or unnamed cells or ranges by using the Go To (Ctrl+G
) command.
Row selection
Row selection is used to select a single or multiple rows.
User Interface:
You can perform row selection in any of the following ways,
- By clicking the row header.
- To select multiple rows, select a row header with the left mouse button and drag over to other row headers (or) use the
Shift + arrow
keys to select multiple rows. - To select non-adjacent rows, hold
Ctrl
and select the row header. - You can also use the
selectRange
method for row selection.
The following sample shows the row selection in the spreadsheet, here selecting the 5th row using the selectRange
method.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent, getRangeAddress } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1 } from './datasource';
enableRipple(true);
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<ejs-spreadsheet #spreadsheet (created)="created()" (cellEdit)="cellEdit($event)" [selectionSettings]="{ mode: 'Multiple' }">
<e-sheets>
<e-sheet name="Price Details">
<e-ranges>
<e-range [dataSource]="priceData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=130></e-column>
<e-column [width]=92></e-column>
<e-column [width]=96></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent | undefined;
priceData: object[] = dataSource1;
created() {
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
var colCount = this.spreadsheetObj!.getActiveSheet().colCount;
this.spreadsheetObj!.selectRange(getRangeAddress([4, 0, 4, colCount as any]));
}
cellEdit(args : any) {
args.cancel = true;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Column selection
Column selection is used to select a single or multiple columns.
User Interface:
You can perform column selection in any of the following ways,
- By clicking the column header.
- To select multiple columns, select a column header with the left mouse button and drag over to other column headers (or) use the
Shift + arrow
keys to select the multiple columns. - To select non-adjacent columns, hold
Ctrl
and select the column header. - You can also use the
selectRange
method for row selection.
The following sample shows the column selection in the spreadsheet, here selecting the 3rd column using the selectRange
method.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent, getRangeAddress } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1 } from './datasource';
enableRipple(true);
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<ejs-spreadsheet #spreadsheet (created)="created()" [selectionSettings]="{ mode: 'Multiple' }">
<e-sheets>
<e-sheet name="Price Details">
<e-ranges>
<e-range [dataSource]="priceData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=130></e-column>
<e-column [width]=92></e-column>
<e-column [width]=96></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent | undefined;
priceData: object[] = dataSource1;
created() {
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
var rowCount = this.spreadsheetObj!.getActiveSheet().rowCount;
this.spreadsheetObj!.selectRange(getRangeAddress([0, 2, rowCount as any, 2]));
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Get selected cell values
You can select single or multiple cells, rows, or columns using mouse and keyboard interactions. You can also programmatically perform selections using the selectRange method. This selection behavior is controlled by the selectionSettings property. Finally, you can retrieve the selected cell values as a collection using the getData method.
import { Component, OnInit, ViewChild } from '@angular/core';
import { defaultData } from './datasource';
import { SpreadsheetComponent, SheetModel, getRangeIndexes, getSwapRange, getRangeAddress, CellModel } from '@syncfusion/ej2-angular-spreadsheet';
import { isNullOrUndefined } from '@syncfusion/ej2-base';
@Component({
selector: 'app-container',
template: `<button class="e-btn custom-btn" (click)="onClick()">Get Selected Cell Values</button>
<ejs-spreadsheet #spreadsheet (created)='created()'>
<e-sheets>
<e-sheet>
<e-ranges>
<e-range [dataSource]='dataSource'>
</e-range>
</e-ranges>
<e-columns>
<e-column [width]=150></e-column>
<e-column [width]=120></e-column>
<e-column [width]=100></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent implements OnInit {
public dataSource?: object[];
@ViewChild('spreadsheet')
public spreadsheetObj!: SpreadsheetComponent;
ngOnInit(): void {
this.dataSource = defaultData;
}
created() {
// Applies cell formatting to specified range of the active sheet.
this.spreadsheetObj.cellFormat({ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' }, 'A1:F1');
};
onClick(): void {
let sheet: SheetModel = this.spreadsheetObj.getActiveSheet();
let selectedRange: string | undefined = sheet.selectedRange;
let index: number[] = getRangeIndexes(selectedRange as string);
let cellRange: number[] = getSwapRange(index);
let swappedRange: string = getRangeAddress(cellRange);
let valueObject: string[] = [];
let range: string = sheet.name + '!' + swappedRange;
// Get the collection of selected cell values by using the getData() method.
this.spreadsheetObj.getData(range).then((cells: Map<string, CellModel>) => {
cells.forEach((cell: CellModel) => {
valueObject.push(isNullOrUndefined((cell.value) as string) ? '' : (cell.value) as string);
});
console.log("Collection of selected cell values:", valueObject);
});
}
}
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Remove Selection
The following sample shows, how to remove the selection in the spreadsheet. Here changing the mode
as None
in selectionSettings
to disable’s the UI selection.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1 } from './datasource';
enableRipple(true);
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<ejs-spreadsheet #spreadsheet (created)="created()" [selectionSettings]="{ mode: 'None' }">
<e-sheets>
<e-sheet name="Price Details">
<e-ranges>
<e-range [dataSource]="priceData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=130></e-column>
<e-column [width]=92></e-column>
<e-column [width]=96></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent | undefined;
priceData: object[] = dataSource1;
created() {
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Limitations
- We have a limitation while performing the Select All(
ctrl + A
). You can do this only by clicking the Select All button at the top left corner.
Note
You can refer to our Angular Spreadsheet feature tour page for its groundbreaking feature representations. You can also explore our Angular Spreadsheet example to knows how to present and manipulate data.