实现类似于Excel VBA Range.FormulaLocal的Cell.FormulaLocal

可能的使用场景

Microsoft Excel公式在不同语言、地区或国家/地区可能有不同的名称。例如,SUM 函数在德语中称为SUMME。Aspose.Cells无法处理非英语函数名称。在Microsoft Excel VBA中,有一个Range.FormulaLocal 属性,根据其语言或地区返回函数的名称。Aspose.Cells也为此提供了Cell.FormulaLocal属性。但是,只有在您实现GlobalizationSettings.GetLocalFunctionName(string standardName)方法时,此属性才能正常工作。

实现类似于Excel VBA Range.FormulaLocal的Cell.FormulaLocal

以下示例代码解释了如何实现GlobalizationSettings.GetLocalFunctionName(string standardName)方法。该方法返回标准函数的本地名称。如果标准函数名称为SUM,它将返回UserFormulaLocal_SUM。您可以根据需要更改代码并返回正确的本地函数名称,例如SUM 在德语中是SUMMETEXT 在俄语中是ТЕКСТ。还请参考下面给出的示例代码的控制台输出作为参考。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aspose.Cells.Examples.CSharp.WorkbookSettings
{
class Implement_Cell_FormulaLocal_SimilarTo_Range_FormulaLocal
{
//Implement GlobalizationSettings class
class GS : GlobalizationSettings
{
public override string GetLocalFunctionName(string standardName)
{
//Change the SUM function name as per your needs.
if (standardName == "SUM")
{
return "UserFormulaLocal_SUM";
}
//Change the AVERAGE function name as per your needs.
if (standardName == "AVERAGE")
{
return "UserFormulaLocal_AVERAGE";
}
return "";
}//GetLocalFunctionName
}//GS:GlobalizationSettings
public static void Run()
{
//Create workbook
Workbook wb = new Workbook();
//Assign GlobalizationSettings implementation class
wb.Settings.GlobalizationSettings = new GS();
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Access some cell
Cell cell = ws.Cells["C4"];
//Assign SUM formula and print its FormulaLocal
cell.Formula = "SUM(A1:A2)";
Console.WriteLine("Formula Local: " + cell.FormulaLocal);
//Assign AVERAGE formula and print its FormulaLocal
cell.Formula = "=AVERAGE(B1:B2, B5)";
Console.WriteLine("Formula Local: " + cell.FormulaLocal);
}
}
}

控制台输出

Formula Local: =UserFormulaLocal_SUM(A1:A2)

Formula Local: =UserFormulaLocal_AVERAGE(B1:B2,B5)