com.hfg.xml.msoffice2003.spreadsheetml.ExcelWorksheet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msoffice2003.spreadsheetml;
import java.util.regex.Pattern;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
MSOffice 2003 Excel worksheet.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
// TODO:
// - Make sure the worksheet options are the last subtag.
public class ExcelWorksheet extends XMLTag
{
public static enum Pane
{
LOWER_RIGHT(0),
UPPER_RIGHT(1),
LOWER_LEFT(2),
UPPER_LEFT(3);
private int mValue;
//------------------------------------------------------------------------
private Pane(int inValue)
{
mValue = inValue;
}
//------------------------------------------------------------------------
public int value()
{
return mValue;
}
}
private String mDefaultName = "Sheet" + sID++;
private static int sID = 1;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public ExcelWorksheet()
{
super(SpreadsheetML.WORKSHEET);
setName(mDefaultName);
}
//---------------------------------------------------------------------------
public ExcelWorksheet(String inName)
{
this();
setName(inName);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
public ExcelWorksheet setDefaultColumnWidth(int inValue)
{
setAttribute(SpreadsheetML.DEFAULT_COLUMN_WIDTH_ATT, inValue);
return this;
}
//---------------------------------------------------------------------------
public ExcelWorksheet setDefaultRowHeight(int inValue)
{
setAttribute(SpreadsheetML.DEFAULT_ROW_HEIGHT_ATT, inValue);
return this;
}
//---------------------------------------------------------------------------
public ExcelWorksheet setName(String inName)
{
String name = "";
if (StringUtil.isSet(inName))
{
name = inName.trim();
if (name.length() > SpreadsheetML.WORKSHEET_NAME_MAX_LENGTH)
{
throw new ExcelFormatException("The worksheet name " + StringUtil.singleQuote(name)
+ " exceeds the maximum sheet name length of " + SpreadsheetML.WORKSHEET_NAME_MAX_LENGTH + "!");
}
name = purifyWorksheetName(name);
}
setAttribute(SpreadsheetML.NAME_ATT, name);
return this;
}
//---------------------------------------------------------------------------
public String getName()
{
return getAttributeValue(SpreadsheetML.NAME_ATT.getLocalName());
}
//---------------------------------------------------------------------------
/**
Specifies whether the user can make changes to this Worksheet.
*/
public ExcelWorksheet setProtected(boolean inName)
{
setAttribute(SpreadsheetML.PROTECTED_ATT, inName);
return this;
}
//---------------------------------------------------------------------------
public ExcelTable addTable()
{
ExcelTable table = new ExcelTable();
addSubtag(table);
return table;
}
//---------------------------------------------------------------------------
public ExcelWorksheetOptions addWorksheetOptions()
{
ExcelWorksheetOptions options = new ExcelWorksheetOptions();
addSubtag(options);
return options;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
private static final Pattern WORKSHEET_NAME_PURIFICATION_PATTERN = Pattern.compile("[\u0000\u0003\\:\\\\\\*\\?\\/\\[\\]]");
//---------------------------------------------------------------------------
/**
The name of the Excel worksheet must be unique in the workbook and cannot
begin or end with a single quote (') or contain any of the following characters:
0x0000
0x0003
colon (:)
backslash (\)
asterisk (*)
question mark (?)
forward slash (/)
opening square bracket ([)
closing square bracket (])
*/
private String purifyWorksheetName(String inName)
{
return WORKSHEET_NAME_PURIFICATION_PATTERN.matcher(inName).replaceAll("_");
}
}