range.java

来自「EXCEL read and write」· Java 代码 · 共 1,078 行 · 第 1/3 页

JAVA
1,078
字号
   * Used to get the number of sections in a range. If this range is smaller   * than a section, it will return 1 for its containing section.   *   * @return The number of sections in this range.   */  public int numSections()  {    initSections();    return _sectionEnd - _sectionStart;  }  /**   * Used to get the number of paragraphs in a range. If this range is smaller   * than a paragraph, it will return 1 for its containing paragraph.   *   * @return The number of paragraphs in this range.   */  public int numParagraphs()  {    initParagraphs();    return _parEnd - _parStart;  }  /**   *   * @return The number of characterRuns in this range.   */  public int numCharacterRuns()  {    initCharacterRuns();    return _charEnd - _charStart;  }  /**   * Inserts text into the front of this range.   *   * @param text The text to insert   * @return The character run that text was inserted into.   */  public CharacterRun insertBefore(String text)    //throws UnsupportedEncodingException  {    initAll();    TextPiece tp = (TextPiece)_text.get(_textStart);    StringBuffer sb = (StringBuffer)tp.getStringBuffer();    // Since this is the first item in our list, it is safe to assume that    // _start >= tp.getStart()    int insertIndex = _start - tp.getStart();    sb.insert(insertIndex, text);    int adjustedLength = _doc.getTextTable().adjustForInsert(_textStart, text.length());    _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);    _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);    _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);	adjustForInsert(adjustedLength);	// update the FIB.CCPText + friends fields	adjustFIB(text.length());    return getCharacterRun(0);  }  /**   * Inserts text onto the end of this range   *   * @param text The text to insert   * @return The character run the text was inserted into.   */  public CharacterRun insertAfter(String text)  {    initAll();    int listIndex = _textEnd - 1;    TextPiece tp = (TextPiece)_text.get(listIndex);    StringBuffer sb = (StringBuffer)tp.getStringBuffer();    int insertIndex = _end - tp.getStart();    if (tp.getStringBuffer().charAt(_end - 1) == '\r' && text.charAt(0) != '\u0007')    {      insertIndex--;    }    sb.insert(insertIndex, text);    int adjustedLength = _doc.getTextTable().adjustForInsert(listIndex, text.length());    _doc.getCharacterTable().adjustForInsert(_charEnd - 1, adjustedLength);    _doc.getParagraphTable().adjustForInsert(_parEnd - 1, adjustedLength);    _doc.getSectionTable().adjustForInsert(_sectionEnd - 1, adjustedLength);    adjustForInsert(text.length());    return getCharacterRun(numCharacterRuns() - 1);  }  /**   * Inserts text into the front of this range and it gives that text the   * CharacterProperties specified in props.   *   * @param text The text to insert.   * @param props The CharacterProperties to give the text.   * @return A new CharacterRun that has the given text and properties and is n   *         ow a part of the document.   */  public CharacterRun insertBefore(String text, CharacterProperties props)    //throws UnsupportedEncodingException  {    initAll();    PAPX papx = (PAPX)_paragraphs.get(_parStart);    short istd = papx.getIstd();    StyleSheet ss = _doc.getStyleSheet();    CharacterProperties baseStyle = ss.getCharacterStyle(istd);    byte[] grpprl = CharacterSprmCompressor.compressCharacterProperty(props, baseStyle);    SprmBuffer buf = new SprmBuffer(grpprl);    _doc.getCharacterTable().insert(_charStart, _start, buf);    return insertBefore(text);  }  /**   * Inserts text onto the end of this range and gives that text the   * CharacterProperties specified in props.   *   * @param text The text to insert.   * @param props The CharacterProperties to give the text.   * @return A new CharacterRun that has the given text and properties and is n   *         ow a part of the document.   */  public CharacterRun insertAfter(String text, CharacterProperties props)    //throws UnsupportedEncodingException  {    initAll();    PAPX papx = (PAPX)_paragraphs.get(_parEnd - 1);    short istd = papx.getIstd();    StyleSheet ss = _doc.getStyleSheet();    CharacterProperties baseStyle = ss.getCharacterStyle(istd);    byte[] grpprl = CharacterSprmCompressor.compressCharacterProperty(props, baseStyle);    SprmBuffer buf = new SprmBuffer(grpprl);    _doc.getCharacterTable().insert(_charEnd, _end, buf);    _charEnd++;    return insertAfter(text);  }  /**   * Inserts and empty paragraph into the front of this range.   *   * @param props The properties that the new paragraph will have.   * @param styleIndex The index into the stylesheet for the new paragraph.   * @return The newly inserted paragraph.   */  public Paragraph insertBefore(ParagraphProperties props, int styleIndex)    //throws UnsupportedEncodingException  {   return this.insertBefore(props, styleIndex, "\r");  }  /**   * Inserts a paragraph into the front of this range. The paragraph will   * contain one character run that has the default properties for the   * paragraph's style.   *   * It is necessary for the text to end with the character '\r'   *   * @param props The paragraph's properties.   * @param styleIndex The index of the paragraph's style in the style sheet.   * @param text The text to insert.   * @return A newly inserted paragraph.   */  protected Paragraph insertBefore(ParagraphProperties props, int styleIndex, String text)    //throws UnsupportedEncodingException  {    initAll();    StyleSheet ss = _doc.getStyleSheet();    ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex);    CharacterProperties baseChp = ss.getCharacterStyle(styleIndex);    byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle);    byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE];    LittleEndian.putShort(withIndex, (short)styleIndex);    System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length);    SprmBuffer buf = new SprmBuffer(withIndex);    _doc.getParagraphTable().insert(_parStart, _start, buf);    insertBefore(text, baseChp);    return getParagraph(0);  }  /**   * Inserts and empty paragraph into the end of this range.   *   * @param props The properties that the new paragraph will have.   * @param styleIndex The index into the stylesheet for the new paragraph.   * @return The newly inserted paragraph.   */  public Paragraph insertAfter(ParagraphProperties props, int styleIndex)    //throws UnsupportedEncodingException  {    return this.insertAfter(props, styleIndex, "\r");  }  /**  * Inserts a paragraph into the end of this range. The paragraph will  * contain one character run that has the default properties for the  * paragraph's style.  *  * It is necessary for the text to end with the character '\r'  *  * @param props The paragraph's properties.  * @param styleIndex The index of the paragraph's style in the style sheet.  * @param text The text to insert.  * @return A newly inserted paragraph.  */  protected Paragraph insertAfter(ParagraphProperties props, int styleIndex, String text)    //throws UnsupportedEncodingException  {    initAll();    StyleSheet ss = _doc.getStyleSheet();    ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex);    CharacterProperties baseChp = ss.getCharacterStyle(styleIndex);    byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle);    byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE];    LittleEndian.putShort(withIndex, (short)styleIndex);    System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length);    SprmBuffer buf = new SprmBuffer(withIndex);    _doc.getParagraphTable().insert(_parEnd, _end, buf);    _parEnd++;    insertAfter(text, baseChp);    return getParagraph(numParagraphs() - 1);  }  public void delete()  {    initAll();    int numSections = _sections.size();    int numRuns = _characters.size();    int numParagraphs = _paragraphs.size();    int numTextPieces = _text.size();    for (int x = _charStart; x < numRuns; x++)    {      CHPX chpx = (CHPX)_characters.get(x);      chpx.adjustForDelete(_start, _end - _start);    }    for (int x = _parStart; x < numParagraphs; x++)    {      PAPX papx = (PAPX)_paragraphs.get(x);      //System.err.println("Paragraph " + x + " was " + papx.getStart() + " -> " + papx.getEnd());      papx.adjustForDelete(_start, _end - _start);      //System.err.println("Paragraph " + x + " is now " + papx.getStart() + " -> " + papx.getEnd());    }    for (int x = _sectionStart; x < numSections; x++)    {      SEPX sepx = (SEPX)_sections.get(x);      //System.err.println("Section " + x + " was " + sepx.getStart() + " -> " + sepx.getEnd());      sepx.adjustForDelete(_start, _end - _start);      //System.err.println("Section " + x + " is now " + sepx.getStart() + " -> " + sepx.getEnd());    }        for (int x = _textStart; x < numTextPieces; x++)    {    	TextPiece piece = (TextPiece)_text.get(x);    	piece.adjustForDelete(_start, _end - _start);    }	// update the FIB.CCPText + friends field	adjustFIB(-(_end - _start));  }  /**   * Inserts a simple table into the beginning of this range. The number of   * columns is determined by the TableProperties passed into this function.   *   * @param props The table properties for the table.   * @param rows The number of rows.   * @return The empty Table that is now part of the document.   */  public Table insertBefore(TableProperties props, int rows)  {    ParagraphProperties parProps = new ParagraphProperties();    parProps.setFInTable((byte)1);    parProps.setTableLevel((byte)1);    int columns = props.getItcMac();    for (int x = 0; x < rows; x++)    {      Paragraph cell = this.insertBefore(parProps, StyleSheet.NIL_STYLE);      cell.insertAfter(String.valueOf('\u0007'));      for(int y = 1; y < columns; y++)      {        cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE);        cell.insertAfter(String.valueOf('\u0007'));      }      cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE, String.valueOf('\u0007'));      cell.setTableRowEnd(props);    }    return new Table(_start, _start + (rows * (columns + 1)), this, 1);  }  /**   * Inserts a list into the beginning of this range.   *   * @param props The properties of the list entry. All list entries are   *        paragraphs.   * @param listID The id of the list that contains the properties.   * @param level The indentation level of the list.   * @param styleIndex The base style's index in the stylesheet.   * @return The empty ListEntry that is now part of the document.   */  public ListEntry insertBefore(ParagraphProperties props, int listID, int level, int styleIndex)  {    ListTables lt = _doc.getListTables();    if (lt.getLevel(listID, level) == null)    {      throw new NoSuchElementException("The specified list and level do not exist");    }    int ilfo = lt.getOverrideIndexFromListID(listID);    props.setIlfo(ilfo);    props.setIlvl((byte)level);    return (ListEntry)insertBefore(props, styleIndex);  }  /**   * Inserts a list into the beginning of this range.   *   * @param props The properties of the list entry. All list entries are   *        paragraphs.   * @param listID The id of the list that contains the properties.   * @param level The indentation level of the list.   * @param styleIndex The base style's index in the stylesheet.   * @return The empty ListEntry that is now part of the document.   */  public ListEntry insertAfter(ParagraphProperties props, int listID, int level, int styleIndex)  {    ListTables lt = _doc.getListTables();    if (lt.getLevel(listID, level) == null)    {      throw new NoSuchElementException("The specified list and level do not exist");    }    int ilfo = lt.getOverrideIndexFromListID(listID);    props.setIlfo(ilfo);    props.setIlvl((byte)level);    return (ListEntry)insertAfter(props, styleIndex);  }  /**   * Replace (one instance of) a piece of text with another...

⌨️ 快捷键说明

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