@Jayshiv,
Aspose.Cells does not have a built-in method to directly differentiate between a formula that is only a cell reference (e.g., =A1) and one that performs a calculation (e.g., =A1+A2). You should parse or analyze the formula string manually to determine this by yourselves.
The recommended way is to get the formula string using Cell.Formula and check if it contains any operators or function calls besides a simple cell or range reference.
Here is the general approach in C#:
e.g.,
public static bool IsCalculationFormula(Cell cell)
{
if (cell.IsFormula)
{
string formula = cell.Formula.Trim();
// Remove the leading '=' for easier parsing
if (formula.StartsWith("="))
{
formula = formula.Substring(1);
}
// Check for common Excel function patterns (e.g., SUM(, IF(, etc.)
// This is a simple check; a more robust solution might use regex or a list of known functions.
if (formula.Contains("("))
{
// Likely a function call if it has parentheses (like =SUM(A1:A5))
return true;
}
// Check for common operators (arithmetic, comparison, concatenation, etc.)
// Reference operators (colon, space, comma) are tricky as they also appear in simple range references (A1:B5, A1:B5 A7:B8, (A1, B2))
// So we need to be careful with them.
string[] operators = { "+", "-", "*", "/", "^", "&", "<", ">", "=" };
foreach (string op in operators)
{
if (formula.Contains(op))
{
// Simple checks might flag "=A1+A2" but also potentially something like "='Sheet1'!A1" if the sheet name has an operator in the name.
// A better approach is to check if the *only* thing is a simple reference.
// The most reliable way for simple cases is to ensure the string is just a cell/range address.
// It is very difficult to do this perfectly with string parsing alone.
// For example, "=A1" or "='Sheet name'!A1" should be fine, but "=A1+A2" should not.
// The presence of any arithmetic/comparison operator generally means a calculation.
return true;
}
}
// If no operators or functions are found, it might be a simple reference.
// We can use a regex to validate if it's a valid simple cell or range reference.
// A simple reference is a single, valid cell reference (e.g., A1, $B$2, Sheet1!A1, [Book1.xlsx]Sheet1!A1)
// Regex for simple cell references (not fully comprehensive, but covers common cases)
// This is complex due to sheet names and external links.
}
return false; // Not a formula or likely a simple reference
}
Another Approach: Using Precedents
A potentially good method involves using Aspose.Cells’ built-in formula auditing capabilities to check the structure of the formula. The Cell.GetPrecedents() method returns a ReferredAreaCollection which contains all the cells that the formula refers to. See the document for your reference. Precedents and Dependents|Documentation
Hope, this helps a bit.