Forum Discussion

Eliton's avatar
Eliton
Occasional Contributor
14 years ago

Position of the cursor in the table (XLS)

Hi,

Exists some method that returns the position of the cursor in the table (XLS)?

5 Replies

  • Hi,


    You can work with Excel data from tests via its COM server -- Excel.Application. For more information, see Working With Excel Files via COM in the online documentation.


    To get the current position of the cursor, you can use the activecell.address property. For example:



    function ReadDataFromExcel()

    {

      var Excel = Sys.OleObject("Excel.Application");

      Excel.Workbooks.Open("C:\\MyFile.xls");

      Log.Message (Excel.activecell.address)

    }

  • Eliton's avatar
    Eliton
    Occasional Contributor
    What do you mean with "Excel.Application"?

    Look at:

       var ex = Sys.OleObject("Excel.Application");

       ex.WorkBooks.Open("c:\a.xls","Sheet1"); //Here the error occurs, error: the Open method of the class WorkBooks failed!

       Log.Message(ex.activecell.address)


  • Hi,




    What do you mean with "Excel.Application"?


    Excel.Application is a run-time object that automates Microsoft Excel via its COM server. See http://msdn.microsoft.com/en-us/library/ms277887.aspx.

    You can retrieve a reference to this object with the Sys.OleObject property and use the members of this object to work with Excel files from TestComplete.




    Look at:

       var ex = Sys.OleObject("Excel.Application");

       ex.WorkBooks.Open("c:\a.xls","Sheet1"); //Here the error occurs, error: the Open method of the class WorkBooks failed!

       Log.Message(ex.activecell.address)


    The error occurs, because you cannot use the WorkBooks.Open method to open a certain sheet.

    To get access to the needed sheet, you can use the Worksheets property. For example:




    function Test()

    {

       var ex = Sys.OleObject("Excel.Application");

       ex.WorkBooks.Open("c:\a.xls");

       ex.Worksheets("Sheet1")

       Log.Message(ex.activecell.address)

    }

  • Eliton's avatar
    Eliton
    Occasional Contributor
    It worked well:



    function goLineXLS() {

       var ex = Sys.OleObject("Excel.Application");

       ex.WorkBooks.Open("C:\\a.xls");

       Log.Message(ex.ActiveSheet.Name)

       Log.Message(ex.activecell.address)

    }



    but,

    How do ex.Next (next line) ?
  • Hi,


    To go to the next cell, you can use the Range.Offset property. Offset(1,0) moves the position to the next cell vertically. Offset(0,1) moves the position to the next cell horizontally. For example:




    function Test()

    {

      var ex = Sys.OleObject("Excel.Application");

       ex.WorkBooks.Open("c:\a.xls");

       ex.Worksheets("Sheet1");

       var curCell = ex.activecell;

       curCell.Offset(1,0).Activate; //Activates the next cell vertically

    }