Setting Multi-Value Parameter from Excel Named Range Values

By Xiaodong Liang

Question:
I want to set multi values from Excel named range. I tested and got the failure: "Array was not a one-dimensional array." 

GoExcel.Open("C:\Temp\Book1.xlsx", "Sheet1")

MultiValue.List("myP") = GoExcel.NamedRangeValue("MyRange") 

Is there anything missed?

 

Solution:

In Excel, the named range is a X*Y dimensions. X is the rows, Y is the column. While the parameter of multi-values in Inventor is one dimension. Even you defined the named range with only one column, it is not a correct usage to set it to the multi-values directly. You need to extract the values of the column we are interested in.  e.g. the code below gets the values of the first column.

GoExcel.Open("C:\Temp\Book1.xlsx", "Sheet1")

 

‘ this is an array X*Y dimension

rangeArray = GoExcel.NamedRangeValue("MyRange")

 

‘create a list to get the values of the columns

Dim oList As ArrayList

oList = New ArrayList

‘rangeArray.GetLength(0)  means how many rows

‘rangeArray.GetLength(1)  means how many colmuns

Dim oRowsCount

oRowsCount = rangeArray.GetLength(0)

Dim oColCount

oColCount = rangeArray.GetLength(1)

 

 

Dim oRowIndex As Integer

Dim oColIndex As Integer

For oRowIndex = 1 To oRowsCount  

  ‘ add value of each row one by one

  For oColIndex = 1 To oColCount 

    oList.Add(rangeArray(oRowIndex,oColIndex))

  Next 

Next

 

‘set it to the list parameter

MultiValue.List("MyP") = oList

GoExcel.Close

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal” align=”justify”> 


Comments

4 responses to “Setting Multi-Value Parameter from Excel Named Range Values”

  1. Jef eerdekens Avatar
    Jef eerdekens

    Hi,
    Thanks for this rule. It works great! But..
    How should i go about this when the range is in the other direction?
    my range is:
    $B$26:$D$26
    Thanks

  2. This is working flawlessly when there is more than one entry in the defined range. Suppose however that “MyRange” is only one cell? I understand I can define a variable by looking at one cell, but I am using a “live” list that may change in length from 1 entry to any length. When “MyRange” is only one cell, this error occurs:
    Public member ‘GetLength’ on type ‘String’ not found.
    As soon as I add a second cell into “MyRange” this error does not occur. Any suggestions?

  3. Hi Xiaodong Liang,
    I have worked with your code since Inventor 2018 and it worked very well the last years.
    Unfortunately since I updated to 2021 I get an error when I run the iLogic Rule
    System.NullReferenceException: Object reference not set to an instance of an object.
    at iLogic.GoExcel.get_NamedRangeValue(String name)
    at ThisRule.Main()
    at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
    at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
    It looks like the rule is not finding the Named Range in Excel anymore.
    Can you help me with this? I’d really appreciate it!
    Regards
    Sebastian

  4. Hi Sebastian,
    Please try below iLogic code to fill range values into parameter(“Myp”). In below code, iterating through rangeArray is changed and it is working.
    GoExcel.Open(“C:\Temp\Sample.xlsx”, “Sheet1”)
    ‘ this is an array X*Y dimension
    rangeArray = GoExcel.NamedRangeValue(“Range1”)
    ‘create a list to get the values of the columns
    Dim oList As ArrayList
    oList = New ArrayList
    Dim oRowIndex As Integer
    Dim oColIndex As Integer
    For oRowIndex = rangeArray.GetLowerBound(0) To rangeArray.GetUpperBound(0)
    ‘ add value of each row one by one
    For oColIndex = rangeArray.GetLowerBound(1) To rangeArray.GetUpperBound(1)
    oList.Add(rangeArray(oRowIndex,oColIndex))
    Next
    Next
    ‘set it to the list parameter
    MultiValue.List(“MyP”) = oList
    GoExcel.Close
    Thanks and regards,
    Chandra shekar G

Leave a Reply to AndyCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading