⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namerecord.java

📁 java读写Excel文档的API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      detailLength = AREA_RANGE_LENGTH;
    }

    int length = NAME_HEADER_LENGTH + detailLength;    
    length += builtInName != null ? 1 : name.length();
    data = new byte[length];

    // Options
    int options = 0;

    if (builtInName != null)
    {
      options |= 0x20;
    }
    IntegerHelper.getTwoBytes(options, data, 0);

    // Keyboard shortcut
    data[2] = 0;

    // Length of the name in chars
    if (builtInName != null)
    {
      data[3] = (byte) 0x1;
    }
    else
    {
      data[3] = (byte) name.length();    
    }

    // Size of the definitions
    IntegerHelper.getTwoBytes(detailLength, data, 4);

    // Sheet index
    IntegerHelper.getTwoBytes(sheetRef, data, 6);
    IntegerHelper.getTwoBytes(sheetRef, data, 8);

    // Byte 10-13 are optional lengths [0,0,0,0]    
    // Byte 14 is length of name which is not used.    

    // The name
    if (builtInName != null)
    {
      data[15] = (byte) builtInName.getValue();
    }
    else
    {
      StringHelper.getBytes(name, data, 15);
    }

    // The actual range definition.
    int pos = builtInName != null ? 16 : name.length() + 15;

    // If there are multiple ranges for the name, we must specify a 
    // subExpression type rather than areaReference and then put out 
    // multiple areaReference entries with an end byte.
    if (ranges.length > 1)
    {
      data[pos++] = subExpression;
      // Length of remaining bytes excluding itself
      IntegerHelper.getTwoBytes(detailLength - 3, data, pos);
      pos += 2;
      byte[] rd;
      for (int i = 0 ; i < ranges.length ; i++)
      {
        data[pos++] = areaReference;
        rd = ranges[i].getData();        
        System.arraycopy(rd, 0, data, pos, rd.length);
        pos += rd.length;
      }
      data[pos] = 0x10;
    }
    else
    {
      // Range format - area
      data[pos] = areaReference;

      // The range data
      byte[] rd = ranges[0].getData();
      System.arraycopy(rd, 0, data, pos+1, rd.length);
    }

    return data;
  }

  /**
   * Accessor for the name 
   *
   * @return the name
   */
  public String getName()
  {
    return name;
  }

  /**
   * Accessor for the index of this name in the name table
   *
   * @return the index of this name in the name table
   */
  public int getIndex()
  {
    return index;
  }
  
  /**
   * The 0-based index sheet reference for a record name
   *     0 is for a global reference
   *
   * @return the sheet reference for name formula
   */
  public int getSheetRef()
  {
    return sheetRef;
  }
  
  /**
   * Set the index sheet reference for a record name
   *     0 is for a global reference
   *
   */
  public void setSheetRef(int i)
  {
    sheetRef = i;
    IntegerHelper.getTwoBytes(sheetRef, data, 8);
  }

  /**
   * Gets the array of ranges for this name
   * @return the ranges
   */
  public NameRange[] getRanges()
  {
    return ranges;
  }

  /**
   * Called when a row is inserted on the 
   *
   * @param sheetIndex the sheet index on which the column was inserted
   * @param row the column number which was inserted
   */
  void rowInserted(int sheetIndex, int row)
  {
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (sheetIndex != ranges[i].getExternalSheet())
      {
        continue; // shame on me - this is no better than a goto
      }

      if (row <= ranges[i].getFirstRow())
      {
        ranges[i].incrementFirstRow();
        modified = true;
      }

      if (row <= ranges[i].getLastRow())
      {
        ranges[i].incrementLastRow();
        modified = true;
      }
    }
  }

  /**
   * Called when a row is removed on the worksheet
   *
   * @param sheetIndex the sheet index on which the column was inserted
   * @param row the column number which was inserted
   * @reeturn TRUE if the name is to be removed entirely, FALSE otherwise
   */
  boolean rowRemoved(int sheetIndex, int row)
  {
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (sheetIndex != ranges[i].getExternalSheet())
      {
        continue; // shame on me - this is no better than a goto
      }

      if (row == ranges[i].getFirstRow() && row == ranges[i].getLastRow())
      {
        // remove the range
        ranges[i] = EMPTY_RANGE;
      }

      if (row < ranges[i].getFirstRow() && row > 0)
      {
        ranges[i].decrementFirstRow();
        modified = true;
      }

      if (row <= ranges[i].getLastRow())
      {
        ranges[i].decrementLastRow();
        modified = true;
      }
    }

    // If all ranges are empty, then remove the name
    int emptyRanges = 0;
    for (int i = 0 ; i < ranges.length; i++)
    {
      if (ranges[i] == EMPTY_RANGE)
      {
        emptyRanges++;
      }
    }

    if (emptyRanges == ranges.length)
    {
      return true;
    }
   
    // otherwise just remove the empty ones
    NameRange[] newRanges = new NameRange[ranges.length - emptyRanges];
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (ranges[i] != EMPTY_RANGE)
      {
        newRanges[i] = ranges[i];
      }
    }

    ranges = newRanges;

    return false;
  }

  /**
   * Called when a row is removed on the worksheet
   *
   * @param sheetIndex the sheet index on which the column was inserted
   * @param row the column number which was inserted
   * @reeturn TRUE if the name is to be removed entirely, FALSE otherwise
   */
  boolean columnRemoved(int sheetIndex, int col)
  {
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (sheetIndex != ranges[i].getExternalSheet())
      {
        continue; // shame on me - this is no better than a goto
      }

      if (col == ranges[i].getFirstColumn() && col == 
          ranges[i].getLastColumn())
      {
        // remove the range
        ranges[i] = EMPTY_RANGE;
      }

      if (col < ranges[i].getFirstColumn() && col > 0)
      {
        ranges[i].decrementFirstColumn();
        modified = true;
      }

      if (col <= ranges[i].getLastColumn())
      {
        ranges[i].decrementLastColumn();
        modified = true;
      }
    }

    // If all ranges are empty, then remove the name
    int emptyRanges = 0;
    for (int i = 0 ; i < ranges.length; i++)
    {
      if (ranges[i] == EMPTY_RANGE)
      {
        emptyRanges++;
      }
    }

    if (emptyRanges == ranges.length)
    {
      return true;
    }
   
    // otherwise just remove the empty ones
    NameRange[] newRanges = new NameRange[ranges.length - emptyRanges];
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (ranges[i] != EMPTY_RANGE)
      {
        newRanges[i] = ranges[i];
      }
    }

    ranges = newRanges;

    return false;
  }


  /**
   * Called when a row is inserted on the 
   *
   * @param sheetIndex the sheet index on which the column was inserted
   * @param col the column number which was inserted
   */
  void columnInserted(int sheetIndex, int col)
  {
    for (int i = 0 ; i < ranges.length ; i++)
    {
      if (sheetIndex != ranges[i].getExternalSheet())
      {
        continue; // shame on me - this is no better than a goto
      }

      if (col <= ranges[i].getFirstColumn())
      {
        ranges[i].incrementFirstColumn();
        modified = true;
      }

      if (col <= ranges[i].getLastColumn())
      {
        ranges[i].incrementLastColumn();
        modified = true;
      }
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -