Friday, May 30, 2008

Quick Code Generation With Excel

Example Download: Example Excel Code Generation Excel File

Often times, I have found myself in a place where I want to generate some code from a list of items that I was given or that I had created, but were only a one-time thing and creating an xslt transform, a mini code generation program, or a template in a code generation suite such as CodeSmith Studio or MyGeneration would be overkill.

In the past, I would just write all the code out by hand while copy and pasting from my list. There were a couple problems with this approach, which I'm sure many of you are familiar with: you often make copy-paste errors, and it is a lot more time consuming than it needs to be.

Recently, I have discovered the power of using Excel to help me with these kinds of tasks. All you need to know is a few functions and operators and how to copy and paste, and you can significantly cut down the time that it takes to finish such tasks.

Starting Out

Make sure you have Excel or another spreadsheet application with similar functionality (Many who do not want to pay the premium for the Microsoft Office suite are using Open Office, an open source office suite).

The Basics

The main thing to understand before you start is that anything that has carriage returns can be copied and pasted into excel as cells in a column. Once you have these values separated into cells, you can work with the values quite easily.

I feel that is easiest to learn through examples, so I will demonstrate how to generate code by using the example of making an insert script for a static company table. You get a list of companies to be inserted from your customer. Each record will have an integer id, and the company name. You also need to make an enumeration for each of the companies with no spaces, commas, or periods.

Here's your list (These are the first 40 Fortune 500 companies in alphabetical order):

3M Company
Abbott Laboratories
Advance Auto Parts, Inc.
Advanced Micro Devices, Inc.
Aetna Inc.
Affiliated Computer Services, Inc.
Aflac Incorporated
AGCO Corporation
Agilent Technologies, Inc.
Air Products and Chemicals, Inc.
AK Steel Holding Corporation
Albertson's, Inc.
Alcoa Inc.
Allied Waste Industries, Inc.
ALLTEL Corporation
Altria Group, Inc., Inc.
Ameren Corporation
American Electric Power Company, Inc.
American Express Company
American Family Insurance Group
American Financial Group, Inc.
American International Group, Inc.
American Standard Companies Inc.
AmerisourceBergen Corporation
Amgen Inc.
AMR Corporation
Anadarko Petroleum Corporation
Anheuser-Busch Companies, Inc.
Aon Corporation
Apache Corporation
Apple Computer, Inc.
Applied Materials, Inc.
ARAMARK Corporation
Arrow Electronics, Inc.
ArvinMeritor, Inc.
Asbury Automotive Group, Inc.
Ashland Inc.
Assurant, Inc.

You copy this list and single click the first cell in your Excel sheet.  It should appear like so:

Now we're going to use some formulas along with concatenating strings to achieve our code generation.  If you are building anything dynamic, you must always start with an equals ('=') sign. Example: '= A1' would make the cell in which you currently are show up as the first cell in your worksheet like so:

The functions and operators that I find most useful:
  • & - you use this to concatenate different values together
    • Example: ="first value" & "second value"
  • SUBSTITUTE - Will replace one value with another in a given text
    • Example: =SUBSTITUTE(A1," ","") will replace all empty spaces with nothing

    • LEFT, RIGHT, and MID - Will find the position of a string within a given text

    • Alt + Enter - will put a carriage return in your cell (Enter will take you to the next cell)

    • ROW - will give you the row number of a reference

    LET'S DO IT!

    According to the example description, you need to do a couple things:

    • You need to create the insert statement for company.

    • You need to remove the spaces, commas and periods from the name for the enumeration.

    Let's do the first one. The insert statement will most likely look similar to this:

    INSERT INTO Company (Id, Description) Values (<number>, '<description>')

    First we know that we're going to need an Id. We can use the 'ROW' function described above. Next, we know we can just use the company name as the description, we just need to add quotes.

    Our formula will look like this:

    ="INSERT INTO Company (Id, Description) Values (" & ROW(A1) & ", '" & A1 & "')"


    Now we can copy and paste or just drag our formula down using the little square in the bottom right corner of the cell of our formula like so:


    Now for the enumeration. We will need to remove all spaces, commas and periods in the company name and put a comma at the end. We will use the SUBSTITUTE function to replace the necessary characters and concatenate a comma to the end of each line with the following formula:

    = SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, " ", ""),".",""),",","") & ","


    This may look a little hairy, but all we're doing is removing the spaces, removing the periods from that text, and then removing the commas from that text then concatenating a comma to the end.

    As you can see, it is really easy to get some nice, quick code generation out of excel in a matter of minutes, sometimes seconds if you just know a few tricks. Download the excel file below to take a closer look:


    Example Excel Code Generation Excel File

    No comments: