开发者

How to build a list of strings from Excel formula's parameters in c#

开发者 https://www.devze.com 2023-02-10 01:32 出处:网络
My question may sound weird but that\'s the scenario i\'m at. I need to parse an Excel formula (getting it from Office.Interop.Excel) and get the parameters from the formula (# of parameter can vary)

My question may sound weird but that's the scenario i'm at. I need to parse an Excel formula (getting it from Office.Interop.Excel) and get the parameters from the formula (# of parameter can vary) There are multiple cases to consider. E.g.:

1. myformula("param1", "param2")
2. myformula("param1", A2)
3. myformula("param1", , "param3")
4. myformula(A1, , "param3")
5. myformula("param1, has commas in it", "param2")

Is there a nicer way to parse this as opposed to having multiple if branches (especially considering when string array is mixed, i.e. some parameters have quotations and other parameters are reference parameters)?

Using string split(',') doesn't seem to be too helpful as I can have commas in the parameter itself.

I've also tried

string[] paramArray = new string[]{(parameters)}

where parameters = "\"param1\", \"param2\"";

but that didn't seem to work either (it won't work at all if I have an empty parameter but that's another case).

My outcome should be some sort of an array (or list or any other collect开发者_开发问答ion) that would contain all of the parameters, and if a parameter didn't have quotation marks in it then I would need to evaluate it.

Any help would be appreciated.


I would process the string looking for " or , if " found search fwd looking for the closing " ignoring any , along the way and taking into account the possibility of impeded "'s in the parameter. When you get the closing " resume looking for " or , and repeat.

Another option would be to use split but then check the results for elements beginning with " . Merge these with the next array element to for the complete parameter (may be that > 2 consecutive array element need to be merged) Also watch out for strings like "zzz"",""xxx" (a single string that = zzz","xxx)

0

精彩评论

暂无评论...
验证码 换一张
取 消