Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ExternSheetRecord.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * EXTERNSHEET (0x0017)<br/>
  21. * A List of Indexes to EXTERNALBOOK (supplemental book) Records <p/>
  22. *
  23. * @author Libin Roman (Vista Portal LDT. Developer)
  24. */
  25. public class ExternSheetRecord extends StandardRecord {
  26. public final static short sid = 0x0017;
  27. private List<RefSubRecord> _list;
  28. private static final class RefSubRecord {
  29. public static final int ENCODED_SIZE = 6;
  30. /** index to External Book Block (which starts with a EXTERNALBOOK record) */
  31. private int _extBookIndex;
  32. private int _firstSheetIndex; // may be -1 (0xFFFF)
  33. private int _lastSheetIndex; // may be -1 (0xFFFF)
  34. public void adjustIndex(int offset) {
  35. _firstSheetIndex += offset;
  36. _lastSheetIndex += offset;
  37. }
  38. /** a Constructor for making new sub record
  39. */
  40. public RefSubRecord(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
  41. _extBookIndex = extBookIndex;
  42. _firstSheetIndex = firstSheetIndex;
  43. _lastSheetIndex = lastSheetIndex;
  44. }
  45. /**
  46. * @param in the RecordInputstream to read the record from
  47. */
  48. public RefSubRecord(RecordInputStream in) {
  49. this(in.readShort(), in.readShort(), in.readShort());
  50. }
  51. public int getExtBookIndex(){
  52. return _extBookIndex;
  53. }
  54. public int getFirstSheetIndex(){
  55. return _firstSheetIndex;
  56. }
  57. public int getLastSheetIndex(){
  58. return _lastSheetIndex;
  59. }
  60. @Override
  61. public String toString() {
  62. StringBuffer buffer = new StringBuffer();
  63. buffer.append("extBook=").append(_extBookIndex);
  64. buffer.append(" firstSheet=").append(_firstSheetIndex);
  65. buffer.append(" lastSheet=").append(_lastSheetIndex);
  66. return buffer.toString();
  67. }
  68. public void serialize(LittleEndianOutput out) {
  69. out.writeShort(_extBookIndex);
  70. out.writeShort(_firstSheetIndex);
  71. out.writeShort(_lastSheetIndex);
  72. }
  73. }
  74. public ExternSheetRecord() {
  75. _list = new ArrayList<RefSubRecord>();
  76. }
  77. public ExternSheetRecord(RecordInputStream in) {
  78. _list = new ArrayList<RefSubRecord>();
  79. int nItems = in.readShort();
  80. for (int i = 0 ; i < nItems ; ++i) {
  81. RefSubRecord rec = new RefSubRecord(in);
  82. _list.add(rec);
  83. }
  84. }
  85. /**
  86. * @return number of REF structures
  87. */
  88. public int getNumOfRefs() {
  89. return _list.size();
  90. }
  91. /**
  92. * adds REF struct (ExternSheetSubRecord)
  93. * @param rec REF struct
  94. */
  95. public void addREFRecord(RefSubRecord rec) {
  96. _list.add(rec);
  97. }
  98. /** returns the number of REF Records, which is in model
  99. * @return number of REF records
  100. */
  101. public int getNumOfREFRecords() {
  102. return _list.size();
  103. }
  104. @Override
  105. public String toString() {
  106. StringBuffer sb = new StringBuffer();
  107. int nItems = _list.size();
  108. sb.append("[EXTERNSHEET]\n");
  109. sb.append(" numOfRefs = ").append(nItems).append("\n");
  110. for (int i=0; i < nItems; i++) {
  111. sb.append("refrec #").append(i).append(": ");
  112. sb.append(getRef(i).toString());
  113. sb.append('\n');
  114. }
  115. sb.append("[/EXTERNSHEET]\n");
  116. return sb.toString();
  117. }
  118. @Override
  119. protected int getDataSize() {
  120. return 2 + _list.size() * RefSubRecord.ENCODED_SIZE;
  121. }
  122. @Override
  123. public void serialize(LittleEndianOutput out) {
  124. int nItems = _list.size();
  125. out.writeShort(nItems);
  126. for (int i = 0; i < nItems; i++) {
  127. getRef(i).serialize(out);
  128. }
  129. }
  130. private RefSubRecord getRef(int i) {
  131. return _list.get(i);
  132. }
  133. public void adjustIndex(int extRefIndex, int offset) {
  134. getRef(extRefIndex).adjustIndex(offset);
  135. }
  136. /**
  137. * return the non static version of the id for this record.
  138. */
  139. @Override
  140. public short getSid() {
  141. return sid;
  142. }
  143. public int getExtbookIndexFromRefIndex(int refIndex) {
  144. return getRef(refIndex).getExtBookIndex();
  145. }
  146. /**
  147. * @return -1 if not found
  148. */
  149. public int findRefIndexFromExtBookIndex(int extBookIndex) {
  150. int nItems = _list.size();
  151. for (int i = 0; i < nItems; i++) {
  152. if (getRef(i).getExtBookIndex() == extBookIndex) {
  153. return i;
  154. }
  155. }
  156. return -1;
  157. }
  158. public int getFirstSheetIndexFromRefIndex(int extRefIndex) {
  159. return getRef(extRefIndex).getFirstSheetIndex();
  160. }
  161. /**
  162. * Add a zero-based reference to a {@link org.apache.poi.hssf.record.SupBookRecord}.
  163. * <p>
  164. * If the type of the SupBook record is same-sheet referencing, Add-In referencing,
  165. * DDE data source referencing, or OLE data source referencing,
  166. * then no scope is specified and this value <em>MUST</em> be -2. Otherwise,
  167. * the scope must be set as follows:
  168. * <ol>
  169. * <li><code>-2</code> Workbook-level reference that applies to the entire workbook.</li>
  170. * <li><code>-1</code> Sheet-level reference. </li>
  171. * <li><code>&gt;=0</code> Sheet-level reference. This specifies the first sheet in the reference.
  172. * <p>
  173. * If the SupBook type is unused or external workbook referencing,
  174. * then this value specifies the zero-based index of an external sheet name,
  175. * see {@link org.apache.poi.hssf.record.SupBookRecord#getSheetNames()}.
  176. * This referenced string specifies the name of the first sheet within the external workbook that is in scope.
  177. * This sheet MUST be a worksheet or macro sheet.
  178. * <p>
  179. * <p>
  180. * If the supporting link type is self-referencing, then this value specifies the zero-based index of a
  181. * {@link org.apache.poi.hssf.record.BoundSheetRecord} record in the workbook stream that specifies
  182. * the first sheet within the scope of this reference. This sheet MUST be a worksheet or a macro sheet.
  183. * </p>
  184. * </li>
  185. * </ol>
  186. *
  187. * @param firstSheetIndex the scope, must be -2 for add-in references
  188. * @param lastSheetIndex the scope, must be -2 for add-in references
  189. * @return index of newly added ref
  190. */
  191. public int addRef(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
  192. _list.add(new RefSubRecord(extBookIndex, firstSheetIndex, lastSheetIndex));
  193. return _list.size() - 1;
  194. }
  195. public int getRefIxForSheet(int externalBookIndex, int sheetIndex) {
  196. int nItems = _list.size();
  197. for (int i = 0; i < nItems; i++) {
  198. RefSubRecord ref = getRef(i);
  199. if (ref.getExtBookIndex() != externalBookIndex) {
  200. continue;
  201. }
  202. if (ref.getFirstSheetIndex() == sheetIndex && ref.getLastSheetIndex() == sheetIndex) {
  203. return i;
  204. }
  205. }
  206. return -1;
  207. }
  208. public static ExternSheetRecord combine(ExternSheetRecord[] esrs) {
  209. ExternSheetRecord result = new ExternSheetRecord();
  210. for (int i = 0; i < esrs.length; i++) {
  211. ExternSheetRecord esr = esrs[i];
  212. int nRefs = esr.getNumOfREFRecords();
  213. for (int j=0; j<nRefs; j++) {
  214. result.addREFRecord(esr.getRef(j));
  215. }
  216. }
  217. return result;
  218. }
  219. }