use of org.apache.poi.hssf.record.NameRecord in project poi by apache.
the class HSSFName method setNameName.
/**
* Sets the name of the named range
*
* <p>The following is a list of syntax rules that you need to be aware of when you create and edit names.</p>
* <ul>
* <li><strong>Valid characters</strong>
* The first character of a name must be a letter, an underscore character (_), or a backslash (\).
* Remaining characters in the name can be letters, numbers, periods, and underscore characters.
* </li>
* <li><strong>Cell references disallowed</strong>
* Names cannot be the same as a cell reference, such as Z$100 or R1C1.</li>
* <li><strong>Spaces are not valid</strong>
* Spaces are not allowed as part of a name. Use the underscore character (_) and period (.) as word separators, such as, Sales_Tax or First.Quarter.
* </li>
* <li><strong>Name length</strong>
* A name can contain up to 255 characters.
* </li>
* <li><strong>Case sensitivity</strong>
* Names can contain uppercase and lowercase letters.
* </li>
* </ul>
*
* <p>
* A name must always be unique within its scope. POI prevents you from defining a name that is not unique
* within its scope. However you can use the same name in different scopes. Example:
* <pre><blockquote>
* //by default names are workbook-global
* HSSFName name;
* name = workbook.createName();
* name.setNameName("sales_08");
*
* name = workbook.createName();
* name.setNameName("sales_08"); //will throw an exception: "The workbook already contains this name (case-insensitive)"
*
* //create sheet-level name
* name = workbook.createName();
* name.setSheetIndex(0); //the scope of the name is the first sheet
* name.setNameName("sales_08"); //ok
*
* name = workbook.createName();
* name.setSheetIndex(0);
* name.setNameName("sales_08"); //will throw an exception: "The sheet already contains this name (case-insensitive)"
*
* </blockquote></pre>
* </p>
*
* @param nameName named range name to set
* @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive)
*/
public void setNameName(String nameName) {
validateName(nameName);
InternalWorkbook wb = _book.getWorkbook();
_definedNameRec.setNameText(nameName);
int sheetNumber = _definedNameRec.getSheetNumber();
//Check to ensure no other names have the same case-insensitive name
final int lastNameIndex = wb.getNumNames() - 1;
for (int i = lastNameIndex; i >= 0; i--) {
NameRecord rec = wb.getNameRecord(i);
if (rec != _definedNameRec) {
if (rec.getNameText().equalsIgnoreCase(nameName) && sheetNumber == rec.getSheetNumber()) {
String msg = "The " + (sheetNumber == 0 ? "workbook" : "sheet") + " already contains this name: " + nameName;
_definedNameRec.setNameText(nameName + "(2)");
throw new IllegalArgumentException(msg);
}
}
}
// Update our comment, if there is one
if (_commentRec != null) {
_commentRec.setNameText(nameName);
_book.getWorkbook().updateNameCommentRecordCache(_commentRec);
}
}
use of org.apache.poi.hssf.record.NameRecord in project poi by apache.
the class InternalWorkbook method cloneFilter.
public NameRecord cloneFilter(int filterDbNameIndex, int newSheetIndex) {
NameRecord origNameRecord = getNameRecord(filterDbNameIndex);
// copy original formula but adjust 3D refs to the new external sheet index
int newExtSheetIx = checkExternSheet(newSheetIndex);
Ptg[] ptgs = origNameRecord.getNameDefinition();
for (int i = 0; i < ptgs.length; i++) {
Ptg ptg = ptgs[i];
if (ptg instanceof Area3DPtg) {
Area3DPtg a3p = (Area3DPtg) ((OperandPtg) ptg).copy();
a3p.setExternSheetIndex(newExtSheetIx);
ptgs[i] = a3p;
} else if (ptg instanceof Ref3DPtg) {
Ref3DPtg r3p = (Ref3DPtg) ((OperandPtg) ptg).copy();
r3p.setExternSheetIndex(newExtSheetIx);
ptgs[i] = r3p;
}
}
NameRecord newNameRecord = createBuiltInName(NameRecord.BUILTIN_FILTER_DB, newSheetIndex + 1);
newNameRecord.setNameDefinition(ptgs);
newNameRecord.setHidden(true);
return newNameRecord;
}
use of org.apache.poi.hssf.record.NameRecord in project poi by apache.
the class InternalWorkbook method updateNamesAfterCellShift.
/**
* Updates named ranges due to moving of cells
*
* @param shifter the formula shifter
*/
public void updateNamesAfterCellShift(FormulaShifter shifter) {
for (int i = 0; i < getNumNames(); ++i) {
NameRecord nr = getNameRecord(i);
Ptg[] ptgs = nr.getNameDefinition();
if (shifter.adjustFormula(ptgs, nr.getSheetNumber())) {
nr.setNameDefinition(ptgs);
}
}
}
use of org.apache.poi.hssf.record.NameRecord in project poi by apache.
the class InternalWorkbook method createBuiltInName.
/**
* Generates a NameRecord to represent a built-in region
*
* @param builtInName the built-in name
* @param sheetNumber the sheet number
*
* @return a new NameRecord
*/
public NameRecord createBuiltInName(byte builtInName, int sheetNumber) {
if (sheetNumber < 0 || sheetNumber + 1 > Short.MAX_VALUE) {
throw new IllegalArgumentException("Sheet number [" + sheetNumber + "]is not valid ");
}
NameRecord name = new NameRecord(builtInName, sheetNumber);
if (linkTable.nameAlreadyExists(name)) {
throw new RuntimeException("Builtin (" + builtInName + ") already exists for sheet (" + sheetNumber + ")");
}
addName(name);
return name;
}
use of org.apache.poi.hssf.record.NameRecord in project poi by apache.
the class HSSFWorkbook method createBuiltInName.
HSSFName createBuiltInName(byte builtinCode, int sheetIndex) {
NameRecord nameRecord = workbook.createBuiltInName(builtinCode, sheetIndex + 1);
HSSFName newName = new HSSFName(this, nameRecord, null);
names.add(newName);
return newName;
}
Aggregations