Search in sources :

Example 1 with NameRecord

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);
    }
}
Also used : NameRecord(org.apache.poi.hssf.record.NameRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 2 with NameRecord

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;
}
Also used : OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 3 with NameRecord

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);
        }
    }
}
Also used : NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) NameRecord(org.apache.poi.hssf.record.NameRecord)

Example 4 with NameRecord

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;
}
Also used : NameRecord(org.apache.poi.hssf.record.NameRecord)

Example 5 with NameRecord

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;
}
Also used : NameRecord(org.apache.poi.hssf.record.NameRecord)

Aggregations

NameRecord (org.apache.poi.hssf.record.NameRecord)23 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)9 Test (org.junit.Test)9 Ptg (org.apache.poi.ss.formula.ptg.Ptg)7 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)4 Record (org.apache.poi.hssf.record.Record)3 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)3 NameXPtg (org.apache.poi.ss.formula.ptg.NameXPtg)3 Ref3DPtg (org.apache.poi.ss.formula.ptg.Ref3DPtg)3 UnionPtg (org.apache.poi.ss.formula.ptg.UnionPtg)3 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)3 InternalSheet (org.apache.poi.hssf.model.InternalSheet)2 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)2 EOFRecord (org.apache.poi.hssf.record.EOFRecord)2 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)2 HSSFName (org.apache.poi.hssf.usermodel.HSSFName)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 OperandPtg (org.apache.poi.ss.formula.ptg.OperandPtg)2