You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExternSheetRecord.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.common.usermodel.GenericRecord;
  21. import org.apache.poi.util.GenericRecordJsonWriter;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.LittleEndianOutput;
  24. /**
  25. * EXTERNSHEET (0x0017)<p>
  26. * A List of Indexes to EXTERNALBOOK (supplemental book) Records
  27. */
  28. public class ExternSheetRecord extends StandardRecord {
  29. public static final short sid = 0x0017;
  30. private final List<RefSubRecord> _list = new ArrayList<>();
  31. private static final class RefSubRecord implements GenericRecord {
  32. public static final int ENCODED_SIZE = 6;
  33. /** index to External Book Block (which starts with a EXTERNALBOOK record) */
  34. private final int _extBookIndex;
  35. private int _firstSheetIndex; // may be -1 (0xFFFF)
  36. private int _lastSheetIndex; // may be -1 (0xFFFF)
  37. public RefSubRecord(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
  38. _extBookIndex = extBookIndex;
  39. _firstSheetIndex = firstSheetIndex;
  40. _lastSheetIndex = lastSheetIndex;
  41. }
  42. public RefSubRecord(RefSubRecord other) {
  43. _extBookIndex = other._extBookIndex;
  44. _firstSheetIndex = other._firstSheetIndex;
  45. _lastSheetIndex = other._lastSheetIndex;
  46. }
  47. /**
  48. * @param in the RecordInputstream to read the record from
  49. */
  50. public RefSubRecord(RecordInputStream in) {
  51. this(in.readShort(), in.readShort(), in.readShort());
  52. }
  53. public int getExtBookIndex(){
  54. return _extBookIndex;
  55. }
  56. public int getFirstSheetIndex(){
  57. return _firstSheetIndex;
  58. }
  59. public int getLastSheetIndex(){
  60. return _lastSheetIndex;
  61. }
  62. @Override
  63. public String toString() {
  64. return GenericRecordJsonWriter.marshal(this);
  65. }
  66. public void serialize(LittleEndianOutput out) {
  67. out.writeShort(_extBookIndex);
  68. out.writeShort(_firstSheetIndex);
  69. out.writeShort(_lastSheetIndex);
  70. }
  71. @Override
  72. public Map<String, Supplier<?>> getGenericProperties() {
  73. return GenericRecordUtil.getGenericProperties(
  74. "extBookIndex", this::getExtBookIndex,
  75. "firstSheetIndex", this::getFirstSheetIndex,
  76. "lastSheetIndex", this::getLastSheetIndex
  77. );
  78. }
  79. }
  80. public ExternSheetRecord() {}
  81. public ExternSheetRecord(ExternSheetRecord other) {
  82. other._list.stream().map(RefSubRecord::new).forEach(_list::add);
  83. }
  84. public ExternSheetRecord(RecordInputStream in) {
  85. int nItems = in.readShort();
  86. for (int i = 0 ; i < nItems ; ++i) {
  87. RefSubRecord rec = new RefSubRecord(in);
  88. _list.add(rec);
  89. }
  90. }
  91. /**
  92. * @return number of REF structures
  93. */
  94. public int getNumOfRefs() {
  95. return _list.size();
  96. }
  97. /**
  98. * adds REF struct (ExternSheetSubRecord)
  99. * @param rec REF struct
  100. */
  101. public void addREFRecord(RefSubRecord rec) {
  102. _list.add(rec);
  103. }
  104. /** returns the number of REF Records, which is in model
  105. * @return number of REF records
  106. */
  107. public int getNumOfREFRecords() {
  108. return _list.size();
  109. }
  110. @Override
  111. protected int getDataSize() {
  112. return 2 + _list.size() * RefSubRecord.ENCODED_SIZE;
  113. }
  114. @Override
  115. public void serialize(LittleEndianOutput out) {
  116. int nItems = _list.size();
  117. out.writeShort(nItems);
  118. for (int i = 0; i < nItems; i++) {
  119. getRef(i).serialize(out);
  120. }
  121. }
  122. private RefSubRecord getRef(int i) {
  123. return _list.get(i);
  124. }
  125. public void removeSheet(int sheetIdx) {
  126. int nItems = _list.size();
  127. for (int i = 0; i < nItems; i++) {
  128. RefSubRecord refSubRecord = _list.get(i);
  129. if(refSubRecord.getFirstSheetIndex() == sheetIdx &&
  130. refSubRecord.getLastSheetIndex() == sheetIdx) {
  131. // removing the entry would mess up the sheet index in Formula of NameRecord
  132. _list.set(i, new RefSubRecord(refSubRecord.getExtBookIndex(), -1, -1));
  133. } else if (refSubRecord.getFirstSheetIndex() > sheetIdx &&
  134. refSubRecord.getLastSheetIndex() > sheetIdx) {
  135. _list.set(i, new RefSubRecord(refSubRecord.getExtBookIndex(), refSubRecord.getFirstSheetIndex()-1, refSubRecord.getLastSheetIndex()-1));
  136. }
  137. }
  138. }
  139. /**
  140. * return the non static version of the id for this record.
  141. */
  142. @Override
  143. public short getSid() {
  144. return sid;
  145. }
  146. /**
  147. * @param refIndex specifies the n-th refIndex
  148. *
  149. * @return the index of the SupBookRecord for this index
  150. */
  151. public int getExtbookIndexFromRefIndex(int refIndex) {
  152. RefSubRecord refRec = getRef(refIndex);
  153. return refRec.getExtBookIndex();
  154. }
  155. /**
  156. * @param extBookIndex external sheet reference index
  157. *
  158. * @return -1 if not found
  159. */
  160. public int findRefIndexFromExtBookIndex(int extBookIndex) {
  161. int nItems = _list.size();
  162. for (int i = 0; i < nItems; i++) {
  163. if (getRef(i).getExtBookIndex() == extBookIndex) {
  164. return i;
  165. }
  166. }
  167. return -1;
  168. }
  169. /**
  170. * Returns the first sheet that the reference applies to, or
  171. * -1 if the referenced sheet can't be found, or -2 if the
  172. * reference is workbook scoped.
  173. *
  174. * @param extRefIndex external sheet reference index
  175. *
  176. * @return the first sheet that the reference applies to, or
  177. * -1 if the referenced sheet can't be found, or -2 if the
  178. * reference is workbook scoped
  179. */
  180. public int getFirstSheetIndexFromRefIndex(int extRefIndex) {
  181. return getRef(extRefIndex).getFirstSheetIndex();
  182. }
  183. /**
  184. * Returns the last sheet that the reference applies to, or
  185. * -1 if the referenced sheet can't be found, or -2 if the
  186. * reference is workbook scoped.
  187. * For a single sheet reference, the first and last should be
  188. * the same.
  189. *
  190. * @param extRefIndex external sheet reference index
  191. *
  192. * @return the last sheet that the reference applies to, or
  193. * -1 if the referenced sheet can't be found, or -2 if the
  194. * reference is workbook scoped.
  195. */
  196. public int getLastSheetIndexFromRefIndex(int extRefIndex) {
  197. return getRef(extRefIndex).getLastSheetIndex();
  198. }
  199. /**
  200. * Add a zero-based reference to a {@link org.apache.poi.hssf.record.SupBookRecord}.
  201. * <p>
  202. * If the type of the SupBook record is same-sheet referencing, Add-In referencing,
  203. * DDE data source referencing, or OLE data source referencing,
  204. * then no scope is specified and this value <em>MUST</em> be -2. Otherwise,
  205. * the scope must be set as follows:
  206. * <ol>
  207. * <li><code>-2</code> Workbook-level reference that applies to the entire workbook.</li>
  208. * <li><code>-1</code> Sheet-level reference. </li>
  209. * <li><code>&gt;=0</code> Sheet-level reference. This specifies the first sheet in the reference.
  210. * <p>If the SupBook type is unused or external workbook referencing,
  211. * then this value specifies the zero-based index of an external sheet name,
  212. * see {@link org.apache.poi.hssf.record.SupBookRecord#getSheetNames()}.
  213. * This referenced string specifies the name of the first sheet within the external workbook that is in scope.
  214. * This sheet MUST be a worksheet or macro sheet.</p>
  215. *
  216. * <p>If the supporting link type is self-referencing, then this value specifies the zero-based index of a
  217. * {@link org.apache.poi.hssf.record.BoundSheetRecord} record in the workbook stream that specifies
  218. * the first sheet within the scope of this reference. This sheet MUST be a worksheet or a macro sheet.
  219. * </p>
  220. * </li>
  221. * </ol>
  222. *
  223. * @param extBookIndex the external book block index
  224. * @param firstSheetIndex the scope, must be -2 for add-in references
  225. * @param lastSheetIndex the scope, must be -2 for add-in references
  226. * @return index of newly added ref
  227. */
  228. public int addRef(int extBookIndex, int firstSheetIndex, int lastSheetIndex) {
  229. _list.add(new RefSubRecord(extBookIndex, firstSheetIndex, lastSheetIndex));
  230. return _list.size() - 1;
  231. }
  232. public int getRefIxForSheet(int externalBookIndex, int firstSheetIndex, int lastSheetIndex) {
  233. int nItems = _list.size();
  234. for (int i = 0; i < nItems; i++) {
  235. RefSubRecord ref = getRef(i);
  236. if (ref.getExtBookIndex() != externalBookIndex) {
  237. continue;
  238. }
  239. if (ref.getFirstSheetIndex() == firstSheetIndex &&
  240. ref.getLastSheetIndex() == lastSheetIndex) {
  241. return i;
  242. }
  243. }
  244. return -1;
  245. }
  246. public static ExternSheetRecord combine(ExternSheetRecord[] esrs) {
  247. ExternSheetRecord result = new ExternSheetRecord();
  248. for (ExternSheetRecord esr : esrs) {
  249. int nRefs = esr.getNumOfREFRecords();
  250. for (int j=0; j<nRefs; j++) {
  251. result.addREFRecord(esr.getRef(j));
  252. }
  253. }
  254. return result;
  255. }
  256. @Override
  257. public ExternSheetRecord copy() {
  258. return new ExternSheetRecord(this);
  259. }
  260. @Override
  261. public HSSFRecordTypes getGenericRecordType() {
  262. return HSSFRecordTypes.EXTERN_SHEET;
  263. }
  264. @Override
  265. public Map<String, Supplier<?>> getGenericProperties() {
  266. return GenericRecordUtil.getGenericProperties("refrec", () -> _list);
  267. }
  268. }