For the lazy programmer: Using Excel as a code generator

As a 'lazy' programmer, I tend to find creative ways to generate code. One of today's challenges required me to create Enums (each with a description) from code & description pairs of unit measures found inside an Excel spreadsheet containing millions of records (okay I may be exaggerating).

Immediately, my brain started to think about possible solutions like T4 generation or find & replace. Luckily, I remembered I have Excel on my machine. This made things so much easier.

In the spreadsheet given to me (after removing all the colour formatting and other noise), columns A & B contain the unit code and description respectively.

Now, applying a simple Excel formula in column C could do the trick:

="[Description(""" & B1 & """)]" & A1 & ","

After copying the formula down to all the needed rows, the result is quite pleasing:

Back in Visual Studio, I've created an empty Enum called "Types":

The only thing left to do is to copy the values of column C into the body and apply document formatting (press Ctrl + D, E):

Remember to resolve references!

And that's all it takes. A little Excel goes a long way.

Till next time!

comments powered by Disqus