Attach excel data to object's attribute user text

Hello folks,
does anyone know, if it’s possible to read data from an excel-file and attach it to geometry?
My attempt was to import an shp-file via Urbano and create a bunch of surfaces. Since the shp-file has an ID stored with each surface, I wanted to look through an excel-file and find the row, that contains this ID and save the information of the table header and the according row for each element as key/value-pair in Rhino. I tried using the modify object attribute component, but it doesn’t seem to work on Grasshopper objects. Do I have to bake the surfaces at some point to be able to apply the excel information? I didn’t bake them yet, because I fear that the baked surfaces loose their shp-metadata once they are fixed within Rhino.
Or is there any other way to somehow connect and show data from an excel-file to imported geometry?

Best regards,
~ Carina

Yes it is possible.

  1. use microsoft.interop.excel to read the excel file (py).
  2. search for a specific cell containing a specific value by looping the rows on a fixxed column
  3. loop all the columns until you hit an empty column and store the values in a list
  4. loop the list and create a new user text for each value in the list
  5. assign each value to the object

Alternatively, if you do not want to code your component, pretty sure you can reach your goal by using Elefront or Lunchbox directly in grasshopper.


This is a working example that does the reverse in C#, should be fairly simply to modify it to your use case in case you decide you want to make a quick component

                var excelApp = new Excel.Application();
                var excelWorkbook = excelApp.Workbooks.Add();
                var excelWorksheet = excelWorkbook.Sheets[1];
                OpenFileDialog excelFilePath = new OpenFileDialog();
                excelFilePath.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; // filter for Excel files only
                excelFilePath.Title = "Select Excel file";
                excelFilePath.RestoreDirectory = true; // show file dialog and wait for user input
                // Set the headers for the Excel file
                excelApp.Cells[1, 1] = "IDN";
                excelApp.Cells[1, 2] = "TYPE";
                excelApp.Cells[1, 3] = "MAT";
                excelApp.Cells[1, 4] = "QTY";
                excelApp.Cells[1, 5] = "SIDE";
                excelApp.Cells[1, 6] = "MASS [kg]";
                excelApp.Cells[1, 7] = "DESCRIPTION";

                // Loop through each object and export its user texts if they exist
                int rowIndex = 2;
                foreach (var rhinoObject in rhinoObjects)
                    // Check if the object has the desired user texts
                    var userTexts = rhinoObject.Attributes.GetUserStrings();
                    // Get the values of the user texts
                    var idn = userTexts["IDN"];
                    var type = userTexts["TYPE"];
                    var mat = userTexts["MAT"];
                    var qty = userTexts["QTY"];
                    var side = (userTexts["END1"] == "Y" || userTexts["END2"] == "Y") ? "BOTH" : "SINGLE";
                    var mass= userTexts["wt"];
                    var description = userTexts["description"];
                    excelApp.Cells[rowIndex, 1] = idn;
                    excelApp.Cells[rowIndex, 2] = type;
                    excelApp.Cells[rowIndex, 3] = mat;
                    excelApp.Cells[rowIndex, 4] = qty;
                    excelApp.Cells[rowIndex, 5] = side;
                    excelApp.Cells[rowIndex, 6] = mass;
                    excelApp.Cells[rowIndex, 7] = description;
1 Like

Wow! Thanks for the quick reply! That looks pretty advanced. I’ll have to figure out how to use microsoft.interop.excel. This only works, when geometry is baked first to Rhino, am I correct?

You could bake geometry and the attributes at the same time with Elefront, but you’d need a way to tell Grasshopper which attributes go to which surface.

Maybe you could post an example file of what your desired result is ?

TTToolbox 1.9 works well to read/write data to Excel.

1 Like

Ah thanks @magicteddy ! I was so confused, since I couldn’t find the readExcel component I was looking for in my current TTToolbox version. But installing TT 1.9 does the trick.
I still have the same problem with Elefront though. I’m not sure which version is the one to get. Some of my Elefront components are labeled as old and they don’t seem to work properly sometimes.

Nontheless - Thanks to your advice, I figured out a way to bake the surfaces with the shp-information already stored in the original file. So right now I’ve got surfaces with already some information as attribute user text. I figured I could try @farouk.serragedine’s approach to reference id’s within an excel-file and add them to each object.
My goal is for each object to look in the id column for the object’s id and then add the other attributes in the row with their headers as key/value pair.

I’m using v4.2.2. from Package Manager.
If you have some sort of ID on the surfaces, you should be able to find it in the appropriate Excel column and get all other info attached to the geometry.

You’ll need Modify Rhino Objects and Define Object Attributes.

Here is a full example.

Step 1 : bake some surfaces with an ID (simple integer) and a BakeName.

Step 2 : retrieve those elements with this BakeName, retrieve their ID and search for it in the Excel sheet. Then modify attributes Tag1 and Tag2.

The Excel file is formatted as follows :

image (12.1 KB)

1 Like

Thank you! While I think, that’s a pretty neat solution, I’m afraid I have too many columns in my actual project, to connect them all with the Key/Value Search component. And I’m quite certain, it will get updated during the project process. But I can definitely start from there and I’ve got one solution that works for now.
So thanks again ^^

This will take any amount of columns, with the ID as the first one :smiley: (13.3 KB)

1 Like

Woohoo, it works! That’s amazing! Thanks a lot :smiley:
Plus I got to know the split tree component - that’s really handy