Selection in Spreadsheet Control

30 Dec 202418 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 or Multiple in selectionSettings. If you set mode to None, it disables the UI selection.

NOTE

The default value for mode in selectionSettings is Multiple.

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.

@Html.EJS().Spreadsheet("spreadsheet").Created("createHandler").SelectionSettings(selectionSettings =>
{
   selectionSettings.Mode(SelectionMode.Multiple);
}).Sheets(sheet =>
    {
        sheet.Ranges(ranges =>
        {
            ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).Add();

}).Columns(column =>
{
    column.Width(130).Add();
    column.Width(92).Add();
    column.Width(96).Add();
}).Add();
}).Render()

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
        var colCount = this.getActiveSheet().colCount;
        this.selectRange(ej.spreadsheet.getRangeAddress([4, 0, 4, colCount]));
    }
</script>
public ActionResult Index()
{
    List<object> data = new List<object>()
    {
        new { ExpenseType= "Housing", ProjectedCost= "7000", ActualCost= "7500", Difference= "-500" },
        new { ExpenseType= "Transportation", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Insurance", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Food", ProjectedCost= "2000", ActualCost= "1800", Difference= "200" },
        new { ExpenseType= "Pets", ProjectedCost= "300", ActualCost= "200", Difference= "100" },
        new { ExpenseType= "Personel Care", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Loan", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Tax", ProjectedCost= "200", ActualCost= "200", Difference= "0" },
        new { ExpenseType= "Savings", ProjectedCost= "1000", ActualCost= "900", Difference= "100" },
        new { ExpenseType= "Total", ProjectedCost= "13500", ActualCost= "13600", Difference= "-100" },
    };
    ViewBag.DefaultData = data;
    return View();
}

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.

@Html.EJS().Spreadsheet("spreadsheet").Created("createHandler").SelectionSettings(selectionSettings =>
{
   selectionSettings.Mode(SelectionMode.Multiple);
}).Sheets(sheet =>
    {
        sheet.Ranges(ranges =>
        {
            ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).Add();

}).Columns(column =>
{
    column.Width(130).Add();
    column.Width(92).Add();
    column.Width(96).Add();
}).Add();
}).Render()

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
        var rowCount = this.getActiveSheet().rowCount;
        this.selectRange(ej.spreadsheet.getRangeAddress([0, 2, rowCount, 2]));
    }
</script>
public ActionResult Index()
{
    List<object> data = new List<object>()
    {
        new { ExpenseType= "Housing", ProjectedCost= "7000", ActualCost= "7500", Difference= "-500" },
        new { ExpenseType= "Transportation", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Insurance", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Food", ProjectedCost= "2000", ActualCost= "1800", Difference= "200" },
        new { ExpenseType= "Pets", ProjectedCost= "300", ActualCost= "200", Difference= "100" },
        new { ExpenseType= "Personel Care", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Loan", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Tax", ProjectedCost= "200", ActualCost= "200", Difference= "0" },
        new { ExpenseType= "Savings", ProjectedCost= "1000", ActualCost= "900", Difference= "100" },
        new { ExpenseType= "Total", ProjectedCost= "13500", ActualCost= "13600", Difference= "-100" },
    };
    ViewBag.DefaultData = data;
    return View();
}

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.

Below is a code example demonstrating how to retrieve the selected cell values as a collection programmatically:

@Html.EJS().Button("getSelectedCellValues").Content("Get Selected Cell Values").Render()
@Html.EJS().Spreadsheet("spreadsheet").Sheets((sheet) =>
{
    sheet.Ranges((ranges) =>
    {
        ranges.DataSource(@ViewBag.DefaultData).Add();
    }).Add();
}).Render()

<script>

    document.getElementById("getSelectedCellValues").addEventListener('click', function () {
        var spreadsheet = document.getElementById("spreadsheet").ej2_instances[0];
        var sheet = spreadsheet.getActiveSheet();
        var selectedRange = sheet.selectedRange;
        var index = ej.spreadsheet.getRangeIndexes(selectedRange);
        var cellRange = ej.spreadsheet.getSwapRange(index);
        var swappedRange = ej.spreadsheet.getRangeAddress(cellRange);
        var valueObject = [];
        var range = sheet.name + '!' + swappedRange;
        // Get the collection of selected cell values by using the getData() method.
        spreadsheet.getData(range).then((cells) => {
            cells.forEach((cell) => {
                valueObject.push(ej.base.isNullOrUndefined(cell.value) ? '' : cell.value);
            });
            console.log("Collection of selected cell values:", valueObject);
        });
    });

</script>
public ActionResult Index()
{
    List<object> data = new List<object>()
    {
        new { CustomerName= "Romona Heaslip",  Model= "Taurus",  Color= "Aquamarine",  PaymentMode= "Debit Card",  DeliveryDate= "07/11/2015",  Amount= "8529.22" },
        new { CustomerName= "Clare Batterton",  Model= "Sparrow",  Color= "Pink",  PaymentMode= "Cash On Delivery",  DeliveryDate= "7/13/2016",  Amount= "17866.19" },
        new { CustomerName= "Eamon Traise",  Model= "Grand Cherokee",  Color= "Blue",  PaymentMode= "Net Banking",  DeliveryDate= "09/04/2015",  Amount= "13853.09" },
        new { CustomerName= "Julius Gorner",  Model= "GTO",  Color= "Aquamarine",  PaymentMode= "Credit Card",  DeliveryDate= "12/15/2017",  Amount= "2338.74" },
        new { CustomerName= "Jenna Schoolfield",  Model= "LX",  Color= "Yellow",  PaymentMode= "Credit Card",  DeliveryDate= "10/08/2014",  Amount= "9578.45" },
        new { CustomerName= "Marylynne Harring",  Model= "Catera",  Color= "Pink",  PaymentMode= "Cash On Delivery",  DeliveryDate= "7/01/2017",  Amount= "19141.62" },
        new { CustomerName= "Vilhelmina Leipelt",  Model= "7 Series",  Color= "Goldenrod",  PaymentMode= "Credit Card",  DeliveryDate= "12/20/2015",  Amount= "6543.30" },
        new { CustomerName= "Barby Heisler",  Model= "Corvette",  Color= "Red",  PaymentMode= "Credit Card",  DeliveryDate= "11/24/2014",  Amount= "13035.06" },
        new { CustomerName= "Karyn Boik",  Model= "Regal",  Color= "Pink",  PaymentMode= "Debit Card",  DeliveryDate= "05/12/2014",  Amount= "18488.80" },
        new { CustomerName= "Jeanette Pamplin",  Model= "S4",  Color= "Fuscia",  PaymentMode= "Net Banking",  DeliveryDate= "12/30/2014",  Amount= "12317.04" },
        new { CustomerName= "Cristi Espinos",  Model= "TL",  Color= "Aquamarine",  PaymentMode= "Credit Card",  DeliveryDate= "12/18/2013",  Amount= "6230.13" },
        new { CustomerName= "Issy Humm",  Model= "Club Wagon",  Color= "Pink",  PaymentMode= "Cash On Delivery",  DeliveryDate= "02/02/2015",  Amount= "9709.49" },
        new { CustomerName= "Tuesday Fautly",  Model= "V8 Vantage",  Color= "Crimson",  PaymentMode= "Debit Card",  DeliveryDate= "11/19/2014",  Amount= "9766.10" },
        new { CustomerName= "Rosemaria Thomann",  Model= "Caravan",  Color= "Violet",  PaymentMode= "Net Banking",  DeliveryDate= "02/08/2014",  Amount= "7685.49" },
    };
    ViewBag.DefaultData = data;
    return View();
}

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.

@Html.EJS().Spreadsheet("spreadsheet").Created("createHandler").CellEdit("cellEdit").SelectionSettings(selectionSettings =>
{
   selectionSettings.Mode(SelectionMode.None);
}).Sheets(sheet =>
    {
        sheet.Ranges(ranges =>
        {
            ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).Add();

}).Columns(column =>
{
    column.Width(130).Add();
    column.Width(92).Add();
    column.Width(96).Add();
}).Add();
}).Render()

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
    }
    function cellEdit (args){
        args.cancel = true;
    }
</script>
public ActionResult Index()
{
    List<object> data = new List<object>()
    {
        new { ExpenseType= "Housing", ProjectedCost= "7000", ActualCost= "7500", Difference= "-500" },
        new { ExpenseType= "Transportation", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Insurance", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Food", ProjectedCost= "2000", ActualCost= "1800", Difference= "200" },
        new { ExpenseType= "Pets", ProjectedCost= "300", ActualCost= "200", Difference= "100" },
        new { ExpenseType= "Personel Care", ProjectedCost= "500", ActualCost= "500", Difference= "0" },
        new { ExpenseType= "Loan", ProjectedCost= "1000", ActualCost= "1000", Difference= "0" },
        new { ExpenseType= "Tax", ProjectedCost= "200", ActualCost= "200", Difference= "0" },
        new { ExpenseType= "Savings", ProjectedCost= "1000", ActualCost= "900", Difference= "100" },
        new { ExpenseType= "Total", ProjectedCost= "13500", ActualCost= "13600", Difference= "-100" },
    };
    ViewBag.DefaultData = data;
    return View();
}

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.