Implementera Cell.FormulaLocal liknande Excel VBA Range.FormulaLocal

Möjliga användningsscenario

Microsoft Excel-formler kan ha olika namn i olika platser eller språk. Till exempel kallas funktionen SUM för SUMME på tyska. Aspose.Cells kan inte hantera icke-engelska funktionsnamn. I Microsoft Excel VBA finns det en Range.FormulaLocal egenskap som returnerar namnet på funktionen enligt dess språk eller plats. Aspose.Cells tillhandahåller också egenskapen Cell.FormulaLocal för detta ändamål. Dock fungerar denna egenskap bara om du implementerar GlobalizationSettings.getLocalFunctionName(String standardName) metod. 

Implementera Cell.FormulaLocal liknande Excel VBA Range.FormulaLocal

Följande exempelkod förklarar hur man implementerar GlobalizationSettings.getLocalFunctionName(String standardName) metod. Metoden returnerar det lokala namnet på den standardiserade funktionen. Om det standardiserade funktionsnamnet är SUM returnerar den UserFormulaLocal_SUM. Du kan ändra koden efter dina behov och returnera de korrekta lokala funktionsnamnen, t.ex. SUM är SUMME på tyska och TEXT är ТЕКСТ på ryska. Se även konsoloutputen från exemplet nedan för referens.

Exempelkod

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
package AsposeCellsExamples.WorkbookSettings;
import com.aspose.cells.*;
import AsposeCellsExamples.Utils;
public class Implement_Cell_FormulaLocal_SimilarTo_Range_FormulaLocal {
//Implement GlobalizationSettings class
class GS extends GlobalizationSettings {
public String getLocalFunctionName(String standardName)
{
//Change the SUM function name as per your needs.
if(standardName.equals("SUM"))
{
return "UserFormulaLocal_SUM";
}
//Change the AVERAGE function name as per your needs.
if (standardName.equals("AVERAGE"))
{
return "UserFormulaLocal_AVERAGE";
}
return "";
}//getLocalFunctionName
}//GS extends GlobalizationSettings
public void Run() throws Exception {
//Create workbook
Workbook wb = new Workbook();
//Assign GlobalizationSettings implementation class
wb.getSettings().setGlobalizationSettings(new GS());
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Access some cell
Cell cell = ws.getCells().get("C4");
//Assign SUM formula and print its FormulaLocal
cell.setFormula("SUM(A1:A2)");
System.out.println("Formula Local: " + cell.getFormulaLocal());
//Assign AVERAGE formula and print its FormulaLocal
cell.setFormula("=AVERAGE(B1:B2, B5)");
System.out.println("Formula Local: " + cell.getFormulaLocal());
}
public static void main(String[] args) throws Exception {
System.out.println("Aspose.Cells for Java Version: " + CellsHelper.getVersion());
Implement_Cell_FormulaLocal_SimilarTo_Range_FormulaLocal pg = new Implement_Cell_FormulaLocal_SimilarTo_Range_FormulaLocal();
pg.Run();
// Print the message
System.out.println("Implement_Cell_FormulaLocal_SimilarTo_Range_FormulaLocal executed successfully.");
}
}

Konsoloutput

 Formula Local: =UserFormulaLocal_SUM(A1:A2)

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