I’m nedding to implement excel formula autofill in C#.
Let’s suppose this formula is located at B100:
=SUM($B$99:B99)
I want to make this formula different at C100:
=SUM($B$99:C99)
This formula is only an example. Some real examples are:
=(SUM($B${0}:B{0})/SUM({1}!$B${0}:{1}!B{0}) -1)
=SUM(B{0}:B{1})
=B{0} + B{1}
=C{0}+ B{1}
=$B${0}+ AC{1}
(consider {0} and {1} are, in fact, numbers)
What I need to do, generically, is to pick these column names and “increment” them. Column names surrounded by $ in formulas should not be updated.
How to identify these fields with regex?
Here’s a regex solution that solely deals with formulas. I’ll leave the Excel stuff to you. Provided you have a collection of strings representing your formulas, you can run them through this to increment your column names.
Some comments:
The regex pattern was actually the easy part. It will just match any letter-number sequence, and ignores $A$1 and $A1 types of cells. The tricky part was the logic to increment the column. I’ve added comments to clarify that bit so grab some coffee and read it over 🙂
I’m sure this could be enhanced but this is what I had time for.
The results should look like this (I removed the cell breakdown for brevity):