org.odftoolkit.simple
Class SpreadsheetDocument

java.lang.Object
  extended by org.odftoolkit.odfdom.pkg.OdfPackageDocument
      extended by org.odftoolkit.odfdom.dom.OdfSchemaDocument
          extended by org.odftoolkit.simple.Document
              extended by org.odftoolkit.simple.SpreadsheetDocument
All Implemented Interfaces:
Closeable, ChartContainer, TableContainer

public class SpreadsheetDocument
extends Document
implements ChartContainer

This class represents an empty ODF spreadsheet document.


Nested Class Summary
static class SpreadsheetDocument.OdfMediaType
          This enum contains all possible media types of SpreadsheetDocument documents.
 
Nested classes/interfaces inherited from class org.odftoolkit.simple.Document
Document.ScriptType
 
Nested classes/interfaces inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
OdfSchemaDocument.OdfXMLFile
 
Nested classes/interfaces inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
OdfPackageDocument.Resource
 
Field Summary
 
Fields inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
mContentDom, mDocumentStyles, mMetaDom, mSettingsDom, mStylesDom
 
Fields inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
mDocumentMediaType, mDocumentPathInPackage, mPackage, ROOT_DOCUMENT_PATH
 
Constructor Summary
protected SpreadsheetDocument(OdfPackage pkg, String internalPath, SpreadsheetDocument.OdfMediaType odfMediaType)
          To avoid data duplication a new document is only created, if not already opened.
 
Method Summary
 Table appendSheet(String name)
          Adds a new blank sheet with the specified name to this document.
 Table appendSheet(Table refTable, String name)
          Adds a new sheet with data from existing table.
 void changeMode(SpreadsheetDocument.OdfMediaType mediaType)
          Changes the document to the given mediatype.
 Chart createChart(String title, DataSet dataset, Rectangle rect)
          Creates a new Chart for this spreadsheet document.
 Chart createChart(String title, SpreadsheetDocument document, CellRangeAddressList cellRangeAddr, boolean firstRowAsLabel, boolean firstColumnAsLabel, boolean rowAsDataSeries, Rectangle rect)
          Creates a new Chart for this spreadsheet document.
 Chart createChart(String title, SpreadsheetDocument document, CellRangeAddressList cellRangeAddr, boolean firstRowAsLabel, boolean firstColumnAsLabel, boolean rowAsDataSeries, Rectangle rect, Cell cell)
          Creates a new Chart for this spreadsheet document.
 Chart createChart(String title, String[] labels, String[] legends, double[][] data, Rectangle rect)
          Creates a new Chart for this spreadsheet document.
 void deleteChartById(String chartId)
          Deletes chart by chart id.
 void deleteChartByTitle(String title)
          Deletes chart(s) by chart title.
 Chart getChartById(String chartId)
          Gets chart with specified id.
 List<Chart> getChartByTitle(String title)
          Gets chart list with specified title.
 int getChartCount()
          Returns the chart count of this container
 OfficeSpreadsheetElement getContentRoot()
          Get the content root of a spreadsheet document.
 Table getSheetByIndex(int index)
          Retrieves sheet by index.
 Table getSheetByName(String name)
          Retrieves sheet by name.
 int getSheetCount()
          Returns the sheet count of this document.
 OdfElement getTableContainerElement()
          Get the ODF element which can have as child element directly according to ODF specification.
 Table insertSheet(int before)
          Inserts a new blank sheet before the reference index.
 Table insertSheet(Table refTable, int before)
          Inserts a new sheet with data from existing table.
static SpreadsheetDocument loadDocument(File file)
          Creates an SpreadsheetDocument from the OpenDocument provided by a File.
static SpreadsheetDocument loadDocument(InputStream inputStream)
          Creates an SpreadsheetDocument from the OpenDocument provided by a resource Stream.
static SpreadsheetDocument loadDocument(String documentPath)
          Loads an SpreadsheetDocument from the provided path.
static SpreadsheetDocument newSpreadsheetDocument()
          Creates an empty spreadsheet document.
static SpreadsheetDocument newSpreadsheetTemplateDocument()
          Creates an empty spreadsheet template.
 void removeSheet(int index)
          Removes the sheet in the specified index.
 
Methods inherited from class org.odftoolkit.simple.Document
addTable, addTable, close, getComponentMap, getContentRoot, getEmbeddedDocument, getEmbeddedDocuments, getEmbeddedDocuments, getLocale, getOdfMediaType, getOfficeMetadata, getScriptType, getSectionByName, getSectionIterator, getTableBuilder, getTableByName, getTableContainerImpl, getTableList, insertDocument, loadDocument, loadDocument, loadTemplate, newImage, removeElementLinkedResource, save, save, setLocale, setLocale, setOdfMediaType, toString
 
Methods inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
getBaseURI, getContentDom, getContentStream, getDocumentStyles, getFileDom, getMasterPages, getMetaDom, getMetaStream, getOfficeMasterStyles, getOrCreateDocumentStyles, getSettingsDom, getSettingsStream, getStylesDom, getStylesStream, getTables, getXMLFilePath
 
Methods inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
flushDoms, getDocumentPath, getFileDom, getMediaTypeString, getPackage, isExternalReference, isRootDocument, loadSubDocument, normalizeDocumentPath, removeDocument, save, setMediaTypeString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SpreadsheetDocument

protected SpreadsheetDocument(OdfPackage pkg,
                              String internalPath,
                              SpreadsheetDocument.OdfMediaType odfMediaType)
To avoid data duplication a new document is only created, if not already opened. A document is cached by this constructor using the internalpath as key.

Method Detail

newSpreadsheetDocument

public static SpreadsheetDocument newSpreadsheetDocument()
                                                  throws Exception
Creates an empty spreadsheet document.

Returns:
ODF spreadsheet document based on a default template*
Throws:
Exception - - if the document could not be created

newSpreadsheetTemplateDocument

public static SpreadsheetDocument newSpreadsheetTemplateDocument()
                                                          throws Exception
Creates an empty spreadsheet template.

Returns:
ODF spreadsheet template based on a default
Throws:
Exception - - if the template could not be created

loadDocument

public static SpreadsheetDocument loadDocument(InputStream inputStream)
                                        throws Exception
Creates an SpreadsheetDocument from the OpenDocument provided by a resource Stream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by SpreadsheetDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument methods. An advantage of caching is that there are no problems overwriting an input file.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
inputStream - - the InputStream of the ODF spreadsheet document.
Returns:
the spreadsheet document created from the given InputStream
Throws:
Exception - - if the document could not be created.

loadDocument

public static SpreadsheetDocument loadDocument(String documentPath)
                                        throws Exception
Loads an SpreadsheetDocument from the provided path.

SpreadsheetDocument relies on the file being available for read access over the whole lifecycle of SpreadsheetDocument.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
documentPath - - the path from where the document can be loaded
Returns:
the spreadsheet document from the given path or NULL if the media type is not supported by SIMPLE.
Throws:
Exception - - if the document could not be created.

loadDocument

public static SpreadsheetDocument loadDocument(File file)
                                        throws Exception
Creates an SpreadsheetDocument from the OpenDocument provided by a File.

SpreadsheetDocument relies on the file being available for read access over the whole lifecycle of SpreadsheetDocument.

If the resource stream is not a ODF spreadsheet document, ClassCastException might be thrown.

Parameters:
file - - a file representing the ODF spreadsheet document.
Returns:
the spreadsheet document created from the given File
Throws:
Exception - - if the document could not be created.

getContentRoot

public OfficeSpreadsheetElement getContentRoot()
                                        throws Exception
Get the content root of a spreadsheet document.

Overrides:
getContentRoot in class Document
Returns:
content root, representing the office:spreadsheet tag
Throws:
Exception - if the file DOM could not be created.

changeMode

public void changeMode(SpreadsheetDocument.OdfMediaType mediaType)
Changes the document to the given mediatype. This method can only be used to convert a document to a related mediatype, e.g. template.

Parameters:
mediaType - the related ODF mimetype

getSheetByIndex

public Table getSheetByIndex(int index)
Retrieves sheet by index.

Parameters:
index - the index of the retrieved sheet, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Since:
0.6

getSheetByName

public Table getSheetByName(String name)
Retrieves sheet by name.

Parameters:
name - the name of the retrieved sheet.
Since:
0.6

appendSheet

public Table appendSheet(String name)
Adds a new blank sheet with the specified name to this document.

Parameters:
name - the name of the new sheet.
Returns:
added sheet.
Since:
0.6

appendSheet

public Table appendSheet(Table refTable,
                         String name)
Adds a new sheet with data from existing table.

NOTE: This method copies data from existing table, including linked resources and styles, if the source table is not in the target document. If these data has dependencies to other data of the source document, the data dependencies will not be copied. For example, document A has two sheets, "Sheet1" and "Sheet2". In "Sheet2", there is a cell with formula, "=sum(Sheet1.A1:Sheet1.A10)". After copy the data of "Sheet2" to the new sheet in document B, the result of this formula would be different or even invalid in document B.

Parameters:
refTable - the reference table, which is the data source of the new sheet.
name - the name of the new sheet.
Returns:
added sheet.
Since:
0.6

insertSheet

public Table insertSheet(int before)
Inserts a new blank sheet before the reference index.

Parameters:
before - the reference index, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Returns:
inserted sheet.
Since:
0.6

insertSheet

public Table insertSheet(Table refTable,
                         int before)
Inserts a new sheet with data from existing table.

NOTE: This method copies data from existing table, including linked resources and styles, if the source table is not in the target document. If these data has dependencies to other data of the source document, the data dependencies will not be copied. For example, document A has two sheets, "Sheet1" and "Sheet2". In "Sheet2", there is a cell with formula, "=sum(Sheet1.A1:Sheet1.A10)". After copy the data of "Sheet2" to the new sheet in document B, the result of this formula would be different or even invalid in document B.

Parameters:
refTable - the reference table, which is the data source of the new sheet.
before - the reference index, which starts from 0 and new sheet would be inserted before it. If the index value is out of range (index >= sheet count or index < 0), this method would return null.
Returns:
inserted sheet.
Since:
0.6

removeSheet

public void removeSheet(int index)
Removes the sheet in the specified index.

Parameters:
index - the index of the removed sheet, which starts from 0. If the index value is out of range (index >= sheet count or index < 0), this method would do nothing.
Since:
0.6

getSheetCount

public int getSheetCount()
Returns the sheet count of this document.

Returns:
the sheet count of this document.
Since:
0.6

getTableContainerElement

public OdfElement getTableContainerElement()
Description copied from interface: TableContainer
Get the ODF element which can have as child element directly according to ODF specification. This Element will help to find the position to insert a new Table. For example, element is usable with element, so TextDocument will return OfficeTextElement. While Presentation Notes is an indirectly TableContainer, which holds Table with the help of its grand-child element , so for Notes, DrawTextBoxElement should be return.

Specified by:
getTableContainerElement in interface TableContainer
Returns:
container element which can hold .

createChart

public Chart createChart(String title,
                         DataSet dataset,
                         Rectangle rect)
Creates a new Chart for this spreadsheet document.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart title.
dataset - chart data set.
rect - chart rectangle.
Returns:
the created chart.
Since:
0.6

createChart

public Chart createChart(String title,
                         SpreadsheetDocument document,
                         CellRangeAddressList cellRangeAddr,
                         boolean firstRowAsLabel,
                         boolean firstColumnAsLabel,
                         boolean rowAsDataSeries,
                         Rectangle rect)
Creates a new Chart for this spreadsheet document.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart title.
document - the data source spreadsheet document.
cellRangeAddr - the cell range address list which is used as chart data set.
firstRowAsLabel - whether uses first row as label.
firstColumnAsLabel - whether uses first column as label.
rowAsDataSeries - whether uses data as series.
rect - chart rectangle.
Returns:
the created chart.
Since:
0.6

createChart

public Chart createChart(String title,
                         String[] labels,
                         String[] legends,
                         double[][] data,
                         Rectangle rect)
Creates a new Chart for this spreadsheet document.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart rectangle.
labels - label strings
legends - legend strings
data - chart data set.
rect - chart rectangle.
Returns:
the created chart.
Since:
0.6

createChart

public Chart createChart(String title,
                         SpreadsheetDocument document,
                         CellRangeAddressList cellRangeAddr,
                         boolean firstRowAsLabel,
                         boolean firstColumnAsLabel,
                         boolean rowAsDataSeries,
                         Rectangle rect,
                         Cell cell)
Creates a new Chart for this spreadsheet document.

Parameters:
title - chart rectangle.
document - the data source spreadsheet document.
cellRangeAddr - the cell range list to be used as chart data.
firstRowAsLabel - whether use first row as label.
firstColumnAsLabel - whether use first column as label.
rowAsDataSeries - whether use row as data series.
rect - chart rectangle.
cell - the position cell where the new chart is inserted.
Returns:
the created chart.
Since:
0.6

deleteChartById

public void deleteChartById(String chartId)
Description copied from interface: ChartContainer
Deletes chart by chart id.

Specified by:
deleteChartById in interface ChartContainer
Parameters:
chartId - the id of specified chart.

deleteChartByTitle

public void deleteChartByTitle(String title)
Description copied from interface: ChartContainer
Deletes chart(s) by chart title.

Specified by:
deleteChartByTitle in interface ChartContainer
Parameters:
title - the title of specified chart(s).

getChartById

public Chart getChartById(String chartId)
Description copied from interface: ChartContainer
Gets chart with specified id.

Specified by:
getChartById in interface ChartContainer
Parameters:
chartId - the id of this chart.
Returns:
the chart with specified id.

getChartByTitle

public List<Chart> getChartByTitle(String title)
Description copied from interface: ChartContainer
Gets chart list with specified title.

Specified by:
getChartByTitle in interface ChartContainer
Parameters:
title - the title of specified chart(s).
Returns:
the chart(s) with specified title.

getChartCount

public int getChartCount()
Description copied from interface: ChartContainer
Returns the chart count of this container

Specified by:
getChartCount in interface ChartContainer
Returns:
the chart count of this container


Copyright © 2010-2011 The Apache Software Foundation. All Rights Reserved.