Ticket T933994
Visible to All Users

How to parse a cell formula and extract cell references used in this formula

created 4 years ago

I have application which read data from excel file including formula. my routine extract all cell address from formula.
few sample formula which my application can still parse using RegEx follows

a) (W2/R2)-1
b) W23-U23

But Now a formula bit long which my RegEx could not parse. the formula looks like (U2+W2)/2*1000/W25
This is my code which parse formula and iterate in cell address

C#
string regex = @"\$?[A-Z]{1,3}\$?[0-9]{1,7}"; string strDefaultCalculations="(U2+W2)/2*1000/W25"; List<string> lstAddress = Regex.Matches(strDefaultCalculations, regex).Cast<Match>().Select(m => m.Value).ToList(); foreach (var data in lstAddress) { }

but above routine can parse the formula like (W2/R2)-1 AND W23-U23

So i thought i am using DevExpress Spread sheet control. so just curious to know that Spread sheet control has any function where i will pass my complex formula and that function will return all cell address as a list of string ?

if Spread sheet control does not have this functionality then please tell me what component exist in devexpress library which can do the job what i am looking for. i am using DXperience Subscription.

it is very urgent please guide me how to achieve my goal. if any 3rd party free library does this job then mention the name too.

thanks

Answers approved by DevExpress Support

created 4 years ago

Hi,

You can use the Formula Engine for this purpose. First, use the FormulaEngine.Parse method to get a ParsedExpression instance. Then, use the ParsedExpression.GetRanges method to get a collection of referenced cell ranges.

Should you have further questions, please let me know.

    Show previous comments (9)
    DevExpress Support Team 4 years ago

      Hi Tridip,

      As I recall, we already discussed this question in your other ticket: Winform Spreadsheet control: How to get cell address. The same approach is applicable here as well.

        Yes solved. here is working code

        C#
        using DevExpress.Spreadsheet; using System.IO; DevExpress.Spreadsheet.IWorkbook workbook = null; DevExpress.Spreadsheet.Worksheet sheet = null; //sample formula =D2-D19 workbook = spreadsheetControl1.Document; sheet = workbook.Worksheets[0]; if (sheet[11, 39].Formula != "") { DevExpress.Spreadsheet.Formulas.ParsedExpression pe = spreadsheetControl1.Document.FormulaEngine.Parse(sheet[11, 39].Formula); List<Range> lstRanges = pe.GetRanges(); if (lstRanges.Count > 0) { foreach(Range r in lstRanges) { string xx= r.GetReferenceA1(); } } }

        thanks

        DevExpress Support Team 4 years ago

          Thank you for informing us that you resolved the issue.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.