Forum Discussion

ganapati_bhat's avatar
ganapati_bhat
Occasional Contributor
10 years ago

Excel data read not happening

Hi,



I need the help to read the data from excel sheet using javascript.



I am able to read the data from the 1st sheet. But not able to read from any other sheet. Please share me the code to select the sheet and retriving the data from it.



I have written the code like :

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

 var work=Excel.Workbooks.Open(Filepath);


  work.Sheets("PersonalInformationData"); //  failing in this step.



Please help

4 Replies

  • ganapati_bhat's avatar
    ganapati_bhat
    Occasional Contributor
    Hi,



    I dont want to use DDT drivers. I am using simple jscript and calling that script in my test cases.



    Please provide the information regarding normal jscript read from different sheets.
  • marin's avatar
    marin
    Frequent Contributor
    Hello ganapati,



    I use DDT approach myself and find it quite handy.

    You might want to supply more details about the error you're getting -> this would increase the chance of someone answering.



    Marin
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi ganapati, your code is failing because Sheets is a property of the Excel object, not the Excel.Workbooks object.

     


    The following code will:


     


    1. Open an Excel Workbook.


    2. Select the "PersonalInformationData" Sheet.


    3. Select the Cell "A1" and Log the value contained in that Cell.


     


    function testExcel() {


      var Filepath = "E:\\temp\\test.xlsx";


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


      Excel.Workbooks.Open(Filepath);


      Excel.Sheets( "PersonalInformationData" ).Select();


      Excel.Range( "A1" ).Select();


      Log.Message( Excel.ActiveCell.Value2 );


    }


     


    Hope this helps.


     


    Regards,


    Phil Baird