I am curious how to print my data from a dictionary to an Excel file.
The dictionary (myDict) contains a key (string) and a List(of string).
The List(of string) is called dictList and contains two items.
I initialize both at the beginning to be able to work with them.
Then I read the range of my config xlsx file to add the key-value pairs to the dictionary and they correctly appear in the dictionary.
Example: “180012”: {“Eastern Airlines”, “Aviation”}
Now, I would like to print the content to Excel by using Build Data Table activity with output outDT and later use Write Range (with outDT as input), all in a For Each “key” in “myDict” activity.
I am struggling how to achieve this given that the dictionary has a list as its values and i need to print them in seperate cells. Output should look like in Excel: A1: key, B1: value(0), C1: value(1). Thanks!
Thanks for both for the quick reply! I think I’m on the right track but something else it missing.
During the add data row, the input (ArrayRow), {item.key, item.value} seems to be not working. When I type “item.” … the “key” wont appear at all so I am guessing that this has something to do with the data typy settings? Please see screenshot below.
So, I changed the For Each loop argument type to KeyValuePair(String, List(Of String)) and it seems to work!
For testing purposes, I used a Message Box with “item.Key + " - " + item.Value(i) + " + " + item.Value(i+1)”
However, it is a bit weird because initially I wanted to use item.value(0) and item.value(1) to dispaly both values of the dictionary values list. This turned out to be a fail because it seems that it adds the whole list to the dictionary so I suppose i need to clear the list after adding it to the dictionary. I will come back
Guys, thanks for all the valuable input! Every solution is a bit different so this one is like the mix of all the other topics your guys linked.
Adding a list to a dictionary and writing it to Excel
Creating the dictionary
Initialize dictionary of type IDictionary(String, List(Of String))
Use Assign activity and New Dictionary(Of String, List(Of String))()
A List(Of String) variable had to be initialized
The List(Of String) was added to the Value field of the Add to dictionary activity in a For each loop.
Note: You need to re-initialize the list after adding it to the dictionay else it will keep adding all the elements to it!
Writing dictionary to Excel
Build Data Table activity to set up the DT you want to use
Use a For each loop with Type Argument of KeyValuePair(String, List(Of String)) - this is crucial!
Inside the body of the loop, palce an Add data row activity
In the Input Pane, type {item.Key, item.Value(0), item.Value(1)} in ArrayRow (as we are working with a list!). Note: If “key” will not pop up after you type “item.” then you are using incorrect TypeArgument.
Add an Excel Application Scope and a Write Range to write the Data Table you specified in the Build Data Table activity!