Forum Discussion

mesg2anil's avatar
mesg2anil
Regular Contributor
13 years ago

Create multiple records in the application using data from excel sheet rows

Hi, I'm using oleobject for calling excel file, I need some help on how to make my script run for multiple times if I have data in multiple rows in the excel sheet?


Please find the code below for calling the excel and reading the data from excel, assume i've 4 rows data in the excel which I want to use it for inputting into the applicatioin. The below function; I'm calling in my subroutine as shown below


Function ReadBudgetDetails()


  Set Excel = Sys.OleObject("Excel.Application")

  Call Excel.Workbooks.Open("E:\Budget.xlsx")

  Delay 1000


    For i = 1 to 4

  s = ""

    For j = 1 to 15

  s = s + VarToString(Excel.Cells(i, j)) + "#"

    Next

    Next


  ReadBudgetDetails = s

 

'Closes the driver

  Call Excel.ActiveWorkbook.Save 

  Call Excel.Workbooks.Close

     

End Function


Above function is reading the excel file, but it is reading the last row and inputting the last row data into the application, code is below for reference. I want to create 3 records in the application as I have given data in 3 rows in the excel sheet. Could you please tell me what additional code I need to use below to create 3 records in the application using the data from excel sheet as shown in the above function?


Sub createbudget


'This is calling the above function

  Dim str, arrStr

  str = ReadBudgetDetails()

  arrStr=Split(str,"#")


 Set Rightframe = sys.process.page("http//").document.rightframe

 RightFrame.document.all.Item("ddlProj").ClickItem(arrStr(1))

 RightFrame.document.all.Item("ddlSubProj").ClickItem(arrStr(2))


End Sub

5 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Anil,



    Above function is reading the excel file, but it is reading the last row


    Take a closer look at the following part of your code:

    For i = 1 to 4

      s = ""

      For j = 1 to 15

        s = s + VarToString(Excel.Cells(i, j)) + "#"

      Next

    Next


    Here, you reset the s variable to an empty string on each row iteration. That's why, it ends up only with last row data at the end of the loop. (You can see this if you step through the loop under a debugger.)



    Your code should be as follows:

    s = ""



    For i = 1 to 4

      For j = 1 to 15

        s = s + VarToString(Excel.Cells(i, j)) + "#" 

      Next

    Next






    Actually, I would recommend integrating the AUT operations and the data reading loop, so that you don't have to do data join and split. For example, you could use something like this instead:

    Sub createbudget

      Dim Rightframe, Excel, i



      Set Excel = Sys.OleObject("Excel.Application")

      Call Excel.Workbooks.Open("E:\Budget.xlsx")

      Delay 1000



      Set Rightframe = sys.process.page("http//").document.rightframe



      For i = 1 to 4

        RightFrame.document.all.Item("ddlProj").ClickItem( VarToString(Excel.Cells(i, 1)) )

        RightFrame.document.all.Item("ddlSubProj").ClickItem( VarToString(Excel.Cells(i, 2)) )

      Next



      ' Closes the driver

      Call Excel.ActiveWorkbook.Save

      Call Excel.Workbooks.Close

    End Sub
  • mesg2anil's avatar
    mesg2anil
    Regular Contributor
    Hi Helen,



    Thank you very much for the code you provided.



    I need some more suggestion to the code you provided below...

    1) Instead of using the row loop directly in the subroutine, I want to make this loop run in a separate subroutine and call the subroutine to input the data into the application.

    2) Is it possible to use loop for column also in the below code? For column value; instead of giving excel cell reference as 1, 2 etc, can we give J as reference, the way it is done for row, taking 'i'?

    3) OR can we use the string to store row and column values and call the string in the subroutine to input the data, which I use to do in my code provided in my first mail at the top?



    Could you please help me in this regard?



    VBScript


    Sub createbudget

      Dim Rightframe, Excel, i


      Set Excel = Sys.OleObject("Excel.Application")

      Call Excel.Workbooks.Open("E:\Budget.xlsx")

      Delay 1000


      Set Rightframe = sys.process.page("http//").document.rightframe


      For i = 1 to 4

        RightFrame.document.all.Item("ddlProj").ClickItem( VarToString(Excel.Cells(i, 1)) )

        RightFrame.document.all.Item("ddlSubProj").ClickItem( VarToString(Excel.Cells(i, 2)) )

      Next


      ' Closes the driver

      Call Excel.ActiveWorkbook.Save

      Call Excel.Workbooks.Close

    End Sub

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Anil,



    1) Instead of using the row loop directly in the subroutine, I want to make this loop run in a separate subroutine and call the subroutine to input the data into the application.


    I'm not sure I understand your entire scenario. Do you mean something like in the code below? If not, could you clarify what you need?

    ' Main routine with row loop

    Sub createbudget

      Dim Excel, i



      Set Excel = Sys.OleObject("Excel.Application")

      Call Excel.Workbooks.Open("E:\Budget.xlsx")

      Delay 1000



      For i = 1 to 4

        Call InputData( Excel.Rows(i) ) ' <--- Data input subroutine

      Next



      Call Excel.ActiveWorkbook.Save

      Call Excel.Workbooks.Close

    End Sub



    Sub InputData(ExcelRow)

      Dim Rightframe

      Set Rightframe = sys.process.page("http//").document.rightframe

      RightFrame.document.all.Item("ddlProj").ClickItem( VarToStr(ExcelRow.Cells(1)) )

      RightFrame.document.all.Item("ddlSubProj").ClickItem( VarToStr(ExcelRow.Cells(1)) )

    End Sub






    2) Is it possible to use loop for column also in the below code? For column value; instead of giving excel cell reference as 1, 2 etc, can we give J as reference, the way it is done for row, taking 'i'?


    As you are feeding column values into different objects -- Item("ddlProj") and Item("ddlSubProj") -- then to make a column loop, you'll need to somehow associate these objects with column indexes 1 and 2. For example, you could store the object names in an array so that array element indexes match column indexes. In this case, you'll be able to do something like this:



    Sub InputData(ExcelRow)

      Dim Rightframe, ItemNames, Item, j

      ItemNames = Array("ddlProj", "ddlSubProj")



      Set Rightframe = sys.process.page("http//").document.rightframe

      For j = 0 To 2

        Set Item = Rightframe.document.all.Item( ItemNames(j) )

        Call Item.ClickItem( VarToStr(ExcelRow.Cells(j+1)) ) ' j+1, because array elements are numbered from 0 but Excel columns from 1

      Next

    End Sub


    (On the other hand, this adds more complexity and reduces code readability.)





    3) OR can we use the string to store row and column values and call the string in the subroutine to input the data, which I use to do in my code provided in my first mail at the top?


    Of course, you can stick with your original code. :) I just pointed out that using join and split may not be the most effective way to pass data between subroutines, but it's really up to you.



    Anyway, I hope you'll find the above useful. Good luck with your automation!
  • mesg2anil's avatar
    mesg2anil
    Regular Contributor
    Hi Helen,



    You are super... Thank you very much for all the valuable suggestion!! :)