ksobon / Bumblebee

Excel interop for Dynamo
36 stars 14 forks source link

Read data - Named Range support? #8

Closed sanzoghenzo closed 8 years ago

sanzoghenzo commented 8 years ago

I would like to use named ranges to read data from my excel files. I currently use named tables in VBA to read the contents of those tables, using the Woorksheet.Range(rangename) property. I assume this is also possible with Excel interop, but I see that the ReadData only accepts start cell and end cell to build the range. I will try to create a new node by myself using your node as a base (to not mess up with your work), but it would be wonderful if you can add this option to the node!

ksobon commented 8 years ago

go ahead and add that in. if you submit it here I can review the code and add it to the overall package. Thanks!

ksobon commented 8 years ago

I have looked at this issue and it seems to be really simple. If you input a name "RangeName" instead of a Range notation "A1:B1" then it can be used like that:

def StyleData(ws, gs, cellRange):
    # get range
    if ":" in cellRange:
        origin = ws.Cells(bb.xlRange(cellRange)[1], bb.xlRange(cellRange)[0])
        extent = ws.Cells(bb.xlRange(cellRange)[3], bb.xlRange(cellRange)[2])
        rng = ws.Range[origin, extent]
    else:
        # this is a named cell range
        rng = ws.Range(cellRange)
    # format cell fill style
    if gs.fillStyle != None:
        fs = gs.fillStyle
        if fs.patternType != None:
            rng.Interior.Pattern = fs.PatternType()
        if fs.backgroundColor != None:
            rng.Interior.Color = fs.BackgroundColor()
        if fs.patternColor != None:
            rng.Interior.PatternColor = fs.PatternColor()

I will add that to Read Excel node and post to Package Manager with next release.

Sample:

image

ksobon commented 8 years ago

Here's an example of this functionality: image

Notice it accepts three types of range input. It can be a list of range names, a single range name as string and finally a A1 formatted range string. I will add that to bumblebee as a new node. I am not sure yet if I want to break the Origin Extent way of working and change that to ranges.