Programmatically trigger GC.Spread.Sheets.Events.SelectionChanged

Posted by: frederik.beaudry on 21 June 2022, 12:38 am EST

  • Posted 21 June 2022, 12:38 am EST

    Hello, I am trying to programmatically select a cell and have it trigger the SelectionChanged event but can't seem to find a way to do so.

    I'm using

    workbook.getSheetFromName("Sheet1").setSelection(2,2,1,1)

    which selects the cell correctly, but no event is triggered.

    Is there a way to accomplish this?

    Thanks
  • Replied 21 June 2022, 4:53 pm EST

    Hi Frederik,

    The SpreadJS only provide events for the user activity. Since the selection by code is not something that could be done by the end-user that is why the SpreadJS doesn't fire the SelectionChanged.

    What you could do is to make custom events and emit them after you set the selection. Please refer to the following sample that I have created for you: https://jscodemine.grapecity.com/share/RfYhSLKN102TC8opW9WBpg/

    Alternatively(not recommended), you can execute the commands like selectionRight, selectionLeft, selectionUp, selectionDown(refer to the following code snippet) which will trigger the SelectionChanged Event. This is not recommended because each command will extend the selection to one row/column and for selecting more than one row/column, the SelectionChanged will be triggered multiple times.

    let cm = spread.commandManager();
    cm.execute({ cmd: "selectionRight", sheetName: "Sheet1" })


    selectionRight command: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.Commands~selectionRight.html

    selectionUp command:
    https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.Commands~selectionUp.html

    Please let us know if you need further assistance with your query. We would be happy to help you.

    Regards
    Ankit
  • Replied 21 June 2022, 11:41 pm EST

    Thanks for the reply.

    I had already looked into the command manager, but there didn't seem to be a command that allowed to select a specific cell.

    This was for test purposes in Selenium.


    var rect = _driver.ExecuteJavaScript<Dictionary<string, object>>("return workbook.getActiveSheet().getCellRect(arguments[0],arguments[1],1,1)", row, col);

    var x = (int) ((long) rect["x"] + (long)rect["width"] / 2);
    var y = (int) ((long) rect["y"] + (long)rect["height"] / 2);

    new Actions(_driver).MoveToElement(_driver.FindElement(By.Id("gridvp_vp")), x, y).Click().Build().Perform();
  • Replied 22 June 2022, 6:49 am EST

    Hi Frederik,

    Currently there is no command for selecting a particular cell.

    For testing in Selenium, you can definitely use the getCellRect method that gives the location and size of cell's rectangle. Once you get the location, you can perform the click action.

    getCellRect method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.Worksheet~getCellRect.html

    Regards

    Ankit
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels