Configure Fonts for Rendering Spreadsheets, Recursive Calculation of Formulas & Set Class Identifier for OleObjects with Aspose.Cells for Android 9.0.0

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Aspose.Cells for Android logoWe are pleased to announce the release of Aspose.Cells for Android 9.0.0. This release includes a number of new features, enhancements and bug fixes that further improve the overall stability and usability of the API. Please check the release notes in order to get an idea about what is new and what has been fixed with this revision of Aspose.Cells for Android. If you are planning to upgrade the API from any previous version, we strongly suggest you to check the Public API Changes section to know what has been changed since your current revision of the API.

While you are downloading the latest build, here is a look at the biggest features in this release.

Configure Fonts for Rendering Spreadsheets

Aspose.Cells for Android API has exposed a number of new classes as well as an enumeration to configure font sources for rendering spreadsheets to image formats and PDF. Most important class is the FontConfigs which has many useful factory methods to either set the font sources or set the font substitution along with some inspection APIs to retrieve the list of specified font sources as well as font substitutions.

Set Font Sources

Aspose.Cells APIs search the operating system’s default font directory for the required fonts. In case the required fonts are not available in the system’s font directory then the APIs search through the custom (user defined) directories. The FontConfigs class has exposed a number of ways to set custom font directories & files as detailed below.

  • FontConfigs.setFontFolder method is useful if there is only one folder to be set.
  • FontConfigs.setFontFolders method is useful when the font files reside in multiple folders and you wish to set all folders separately rather than combining all font files in a single folder.
  • FontConfigs.setFontSources method is useful when you wish to load fonts from multiple folders or font files or font data from an array of bytes.

Here is a simple usage scenario of aforementioned methods. Please note, in case more than one methods have been used to specify the font sources then only the last settings will take effect.

//Defining string variables to store paths to font folders & font file
String fontFolder1 = SD_Path + "Arial";
String fontFolder2 = SD_Path + "Calibri";
String fontFile = SD_Path + "Arial/arial.ttf";

//Setting first font folder with setFontFolder method
//Second parameter directs the API to search the sub folders for font files
FontConfigs.setFontFolder(fontFolder1, true);

//Setting both font folders with setFontFolders method
//Second parameter prohibits the API to search the sub folders for font files
FontConfigs.setFontFolders(new String[]{fontFolder1, fontFolder2}, false);

//Defining FolderFontSource
FolderFontSource sourceFolder = new FolderFontSource(fontFolder1, false);

//Defining FileFontSource
FileFontSource sourceFile = new FileFontSource(fontFile);

//Defining MemoryFontSource
File file = new File(fontFile);
byte[] bytes = new byte[(int) file.length()];
MemoryFontSource sourceMemory = new MemoryFontSource(bytes);

//Setting font sources
FontConfigs.setFontSources(new FontSourceBase[] { sourceFolder, sourceFile, sourceMemory});

Substitute Fonts

Aspose.Cells APIs also provide the ability to set user defined font substitutions for rendering purposes. This mechanism is helpful when a required font is not available on the machine where conversion has to take place. You can provide a list of font names as alternative to the originally required font. In order to achieve this, the Aspose.Cells APIs have exposed the FontConfigs.setFontSubstitutes method which accepts 2 parameters. The first parameter is of type String, which should be the name of font which needs to be substituted. The second parameter is an array of type String. You can provide a list of font names as substitution to the original font (specified in the first parameter).

Here is a simple usage scenario of FontConfigs.setFontSubstitutes method.

//Substituting the Arial font with Times New Roman & Calibri
FontConfigs.setFontSubstitutes("Arial", new String[] { "Times New Roman", "Calibri" });

Inspect Configured Font Sources & Substitutions

The Aspose.Cells APIs have also provided means to gather information on what sources and substitutions have been set.

  • FontConfigs.getFontSources method returns an array of type FontSourceBase containing the list of specified font sources. In case, no sources have been set, the FontConfigs.getFontSources method will return an empty array.
  • FontConfigs.getFontSubstitutes method accepts a parameter of type String allowing to specify the font name for which a substitution has been set. In case, no substitution has been set for the specified font name then the FontConfigs.getFontSubstitutes method will return null.

Please check the detailed article on Configurable Font Sources for more in-depth knowledge of newly exposed APIs.

Recursive Calculation of Formulas

Aspose.Cells for Android 9.0.0 has exposed the Boolean type CalculationOptions.Recursive property. Setting the CalculationOptions.Recursive property to true and passing the object to calculateFormula method directs the Aspose.Cells APIs to calculate the dependent cells recursively when calculating cells which depends on other cells.

Below provided code snippet demonstrates the simple usage scenario of newly exposed CalculationOptions.Recursive property. In order to get more details of possible usage scenarios, please check the detailed article on Optimizing Formula Calculation Time.

//Load a sample spreadsheet in an instance of Workbook
Workbook book = new Workbook(SD_Path + "sample.xlsx");

//Initialize CalculationOptions & set Recursive property to true
CalculationOptions options = new CalculationOptions();
options.setRecursive(true);

//Recalculate formulas
book.calculateFormula(options);

Set Class Identifier for OleObjects

Aspose.Cells for Android 9.0.0 has exposed the OleObject.ClassIdentifier property which can be used to retrieve or set the class identifier for an OleObject. Please note, the class identifier decides which application is required to load the embedded resource/OleObject and how. for instance, a PPT (presentation) file can be embedded in a spreadsheet with 2 different views, that is; presentation view or slide view, whereas both views have different class identifier values.

Below provided code snippet demonstrates the simple usage scenario of newly exposed OleObject.ClassIdentifier property. In order to get more details of possible usage scenarios, please check the detailed article on Class Identifier for OleObjects.

//Load a spreadsheet containing a presentation as OleObject
Workbook book = new Workbook(SD_Path + "sample.xls");

//Initialize variables to store properties of OleObject
int upperLeftRow = 0;
int upperLeftColumn = 0;
int height = 0;
int width = 0;
byte[] imageData = null;
int x = 0;
int y = 0;
byte[] objData = null;
String progID = "";
int fileFormatType = 0;
String sourceFullName = "";
Boolean isDisplayAsIcon = false;
byte[] classId = null;

//Get the first worksheet from the collection
Worksheet sheet = book.getWorksheets().get(0);

//Get the first OleObject from the collection
OleObject frame = sheet.getOleObjects().get(0);

//Store the properties in variables
upperLeftRow = frame.getUpperLeftRow();
upperLeftColumn = frame.getUpperLeftColumn();
height = frame.getHeight();
width = frame.getWidth();
imageData = frame.getImageData();
x = frame.getX();
y = frame.getY();
objData = frame.getObjectData();
progID = frame.getProgID();
fileFormatType = frame.getFileFormatType();
sourceFullName = frame.getObjectSourceFullName();
isDisplayAsIcon = frame.getDisplayAsIcon();
classId = frame.getClassIdentifier();

//Initialize a new Workbook instance
book = new Workbook();

//Access first worksheet from the collection
sheet = book.getWorksheets().get(0);

//Insert the OleObject to the worksheet
int oleNumber = sheet.getOleObjects().add(upperLeftRow, upperLeftColumn, height, width, imageData);

//Access newly inserted OleObject
OleObject embeddedObject = sheet.getOleObjects().get(oleNumber);

//Assign previously stored properties to new OleObject
embeddedObject.setX(x);
embeddedObject.setY(y);
embeddedObject.setObjectData(objData);
embeddedObject.setProgID(progID);
embeddedObject.setFileFormatType(fileFormatType);
embeddedObject.setDisplayAsIcon(isDisplayAsIcon);
embeddedObject.setObjectSourceFullName(sourceFullName);
embeddedObject.setAutoSize(false);
if (classId != null)
{
	embeddedObject.setClassIdentifier(classId);
}

Control Chart’s Data Source while Copying Rows

Aspose.Cells for Android API has exposed the Boolean type CopyOptions.ReferToDestinationSheet property along with the an overload of Cells.copyRows method in order to facilitate the copy rows operation when rows to be copied also contains a chart and its data source. You can make use of these new APIs to point the chart’s data source to the source or destination worksheets.

Below provided code snippet demonstrates the simple usage scenario whereas a detailed article can be reviewed at Control the Data Source of Chart while Copying Rows.

//Load a sample spreadsheet in an instance of Workbook
Workbook book = new Workbook(SD_Path + "sample.xlsx");

//Access the worksheet containing the chart & its data source
Worksheet source = book.getWorksheets().get(0);

//Add a new worksheet to the collection
Worksheet destination = book.getWorksheets().get(book.getWorksheets().add());

//Initialize CopyOptions and set its ReferToDestinationSheet property to true
CopyOptions options = new CopyOptions();
options.setReferToDestinationSheet(true);

//Copy the rows
destination.getCells().copyRows(source.getCells(), 0, 0, source.getCells().getMaxDisplayRange().getRowCount(), options);

Implement IFilePathProvider Interface

Aspose.Cells for Android 9.0.0 allows to get/set the IFilePathProvider for exporting worksheets to separate HTML files. These new APIs are helpful in scenarios where hyperlinks in one worksheet points to a location in another worksheet, where application requirement is to render each worksheet to separate HTML file. Implementing the IFilePathProvider allows to keep the aforementioned hyperlinks intact regardless of the fact that they are pointing to a location in a separate resultant HTML file.

Following is the simple usage scenario of HtmlSaveOptions.FilePathProvider property. In order to get more in-depth knowledge of these APIs, please check the detailed article on Implementing IFilePathProvider Interface.

//Load a spreadsheet in an instance of Workbook
Workbook book = new Workbook(SD_Path + "sample.xlsx");

//Save each Worksheet to separate  HTML file
for (int i = 0; i < book.getWorksheets().getCount(); i++)
{
	book.getWorksheets().setActiveSheetIndex(i);

	//Create an instance of HtmlSaveOptions & set FilePathProvider property
	HtmlSaveOptions options = new HtmlSaveOptions();
	options.setExportActiveWorksheetOnly(true);
	options.setFilePathProvider(new IFilePathProvider() 
	{ 
		public String getFullName(String sheetName)
		{
		    if ("Sheet2".equals(sheetName))
		    {
		        return "sheet1.html";
		    }
		    else if ("Sheet3".equals(sheetName))
		    {
		        return "sheet2.html";
		    }

		    return "";
		}
	});

	 //Write HTML file to disc
	 book.save(SD_Path + "sheet"+ i +".html", options);
}

Control the Appearance of Shape’s Textual Contents

Aspose.Cells for Android API has exposed the Shape.TextOptions property which can be used to control all aspects of the shape’s textual contents such as font style, color, font weight and so on.

Below provided code snippet demonstrates the simple usage scenario of Shape.TextOptions property.

//Initialize an instance of Workbook
Workbook book = new Workbook();

//Get the default Worksheet from the Workbook
Worksheet sheet = book.getWorksheets().get(0);

//Add a TextBox to the collection
int textboxIndex = sheet.getTextBoxes().add(2, 1, 160, 200);

//Get the TextBox object
TextBox textbox = sheet.getTextBoxes().get(textboxIndex);

//Add text to the TextBox
textbox.setText("Hello Aspose!");

//Format the textual contents
textbox.getTextOptions().setColor(Color.getRed());
textbox.getTextOptions().setItalic(true);
textbox.getTextOptions().setBold(true);

Other Enhancements & Fixes

Aspose.Cells for Android 9.0.0 has fixed a few critical bugs as well as enhanced its core for more stability. A few of the worth mentioning enhancements are as follow.

Public API Changes

Aspose.Cells for Android 9.0.0 has made a few changes to the publicly exposed APIs in order to keep the API usage simple. Some of the changes are listed as follow.

  • The Worksheet.SetBackground method has been marked obsoleted whereas the alternative approach has been exposed via Worksheet.BackgroundImage property.
  • A few methods such as Worksheet.CopyConditionalFormatting & Workbook.CheckWriteProtectedPassword were obsoleted some releases back. Now they have been completely removed from the public API.
  • The Workbook.RemoveDigitallySign method has been renamed to Workbook.RemoveDigitalSignature.
  • The factory methods such as CellsHelper.setFontDir & setFontDirs have been marked obsoleted whereas the alternative approach has been exposed via FontConfigs class as detailed above.
  • A few of the properties from Shape class has been marked obsoleted whereas alternative properties have been exposed. For instance, the Shape.FillFormat & Shape.LineFormat properties have been replaced by Shape.Fill & Shape.Line properties respectively.
  • The ShapeFont class has been replaced by the TextOptions class whereas the FontSetting.ShapeFont property has been replaced by the FontSetting.TextOptions.

To keep up with our news, you can follow us on Twitter or follow our Facebook page.

One Response to Configure Fonts for Rendering Spreadsheets, Recursive Calculation of Formulas & Set Class Identifier for OleObjects with Aspose.Cells for Android 9.0.0

  1. Pingback: Configure Font Sources for Rendering Spreadshee...

Leave a Reply