Implémenter Cell.FormulaLocal similaire à Excel VBA Range.FormulaLocal
Scénarios d’utilisation possibles
Les formules Microsoft Excel peuvent avoir des noms différents dans différentes régions, langues ou localités. Par exemple, la fonction SUM est appelée SUMME en allemand. Aspose.Cells ne peut pas fonctionner avec des noms de fonction non-anglais. En VBA Excel, il existe une propriété Range.FormulaLocal qui retourne le nom de la fonction selon sa langue ou région. Aspose.Cells fournit également la propriété Cell.FormulaLocal à cet effet. Cependant, cette propriété ne fonctionnera que si vous implémentez la méthode GlobalizationSettings.getLocalFunctionName(String standardName).
Implémenter Cell.FormulaLocal similaire à Excel VBA Range.FormulaLocal
Le code d’exemple suivant explique comment implémenter la méthode GlobalizationSettings.getLocalFunctionName(String standardName). La méthode retourne le nom local de la fonction standard. Si le nom de la fonction standard est SUM, elle retourne UserFormulaLocal_SUM. Vous pouvez modifier le code selon vos besoins et retourner les noms de fonctions locaux corrects, par exemple SUM est SUMME en allemand et TEXT est ТЕКСТ en russe. Veuillez également consulter la sortie de la console du code d’exemple ci-dessous pour référence.
Code d’exemple
// 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."); | |
} | |
} |
Sortie console
Formula Local: =UserFormulaLocal_SUM(A1:A2)
Formula Local: =UserFormulaLocal_AVERAGE(B1:B2,B5)