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.

ColumnInfoRecord.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.BitField;
  19. import org.apache.poi.util.BitFieldFactory;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.LittleEndianOutput;
  22. /**
  23. * Defines with width and formatting for a range of columns
  24. */
  25. public final class ColumnInfoRecord extends StandardRecord {
  26. public static final short sid = 0x007D;
  27. private static final BitField hidden = BitFieldFactory.getInstance(0x01);
  28. private static final BitField outlevel = BitFieldFactory.getInstance(0x0700);
  29. private static final BitField collapsed = BitFieldFactory.getInstance(0x1000);
  30. private int _firstCol;
  31. private int _lastCol;
  32. private int _colWidth;
  33. private int _xfIndex;
  34. private int _options;
  35. // Excel seems write values 2, 10, and 260, even though spec says "must be zero"
  36. private int field_6_reserved;
  37. /**
  38. * Creates a column info record with default width and format
  39. */
  40. public ColumnInfoRecord() {
  41. setColumnWidth(2275);
  42. _options = 2;
  43. _xfIndex = 0x0f;
  44. field_6_reserved = 2; // seems to be the most common value
  45. }
  46. public ColumnInfoRecord(ColumnInfoRecord other) {
  47. super(other);
  48. _firstCol = other._firstCol;
  49. _lastCol = other._lastCol;
  50. _colWidth = other._colWidth;
  51. _xfIndex = other._xfIndex;
  52. _options = other._options;
  53. field_6_reserved = other.field_6_reserved;
  54. }
  55. public ColumnInfoRecord(RecordInputStream in) {
  56. _firstCol = in.readUShort();
  57. _lastCol = in.readUShort();
  58. _colWidth = in.readUShort();
  59. _xfIndex = in.readUShort();
  60. _options = in.readUShort();
  61. switch(in.remaining()) {
  62. case 2: // usual case
  63. field_6_reserved = in.readUShort();
  64. break;
  65. case 1:
  66. // often COLINFO gets encoded 1 byte short
  67. // shouldn't matter because this field is unused
  68. field_6_reserved = in.readByte();
  69. break;
  70. case 0:
  71. // According to bugzilla 48332,
  72. // "SoftArtisans OfficeWriter for Excel" totally skips field 6
  73. // Excel seems to be OK with this, and assumes zero.
  74. field_6_reserved = 0;
  75. break;
  76. default:
  77. throw new RuntimeException("Unusual record size remaining=(" + in.remaining() + ")");
  78. }
  79. }
  80. /**
  81. * set the first column this record defines formatting info for
  82. * @param fc - the first column index (0-based)
  83. */
  84. public void setFirstColumn(int fc) {
  85. _firstCol = fc;
  86. }
  87. /**
  88. * set the last column this record defines formatting info for
  89. * @param lc - the last column index (0-based)
  90. */
  91. public void setLastColumn(int lc) {
  92. _lastCol = lc;
  93. }
  94. /**
  95. * set the columns' width in 1/256 of a character width
  96. * @param cw - column width
  97. */
  98. public void setColumnWidth(int cw) {
  99. _colWidth = cw;
  100. }
  101. /**
  102. * set the columns' default format info
  103. * @param xfi - the extended format index
  104. * @see org.apache.poi.hssf.record.ExtendedFormatRecord
  105. */
  106. public void setXFIndex(int xfi) {
  107. _xfIndex = xfi;
  108. }
  109. /**
  110. * set whether or not these cells are hidden
  111. * @param ishidden - whether the cells are hidden.
  112. */
  113. public void setHidden(boolean ishidden) {
  114. _options = hidden.setBoolean(_options, ishidden);
  115. }
  116. /**
  117. * set the outline level for the cells
  118. * @param olevel -outline level for the cells
  119. */
  120. public void setOutlineLevel(int olevel) {
  121. _options = outlevel.setValue(_options, olevel);
  122. }
  123. /**
  124. * set whether the cells are collapsed
  125. * @param isCollapsed - whether the cells are collapsed
  126. */
  127. public void setCollapsed(boolean isCollapsed) {
  128. _options = collapsed.setBoolean(_options, isCollapsed);
  129. }
  130. /**
  131. * get the first column this record defines formatting info for
  132. * @return the first column index (0-based)
  133. */
  134. public int getFirstColumn() {
  135. return _firstCol;
  136. }
  137. /**
  138. * get the last column this record defines formatting info for
  139. * @return the last column index (0-based)
  140. */
  141. public int getLastColumn() {
  142. return _lastCol;
  143. }
  144. /**
  145. * @return column width in units of 1/256 of a character width
  146. */
  147. public int getColumnWidth() {
  148. return _colWidth;
  149. }
  150. /**
  151. * get the columns' default format info
  152. * @return the extended format index
  153. * @see org.apache.poi.hssf.record.ExtendedFormatRecord
  154. */
  155. public int getXFIndex() {
  156. return _xfIndex;
  157. }
  158. /**
  159. * @return whether the cells are hidden.
  160. */
  161. public boolean getHidden() {
  162. return hidden.isSet(_options);
  163. }
  164. /**
  165. * @return outline level for the cells
  166. */
  167. public int getOutlineLevel() {
  168. return outlevel.getValue(_options);
  169. }
  170. /**
  171. * @return whether the cells are collapsed
  172. */
  173. public boolean getCollapsed() {
  174. return collapsed.isSet(_options);
  175. }
  176. public boolean containsColumn(int columnIndex) {
  177. return _firstCol <= columnIndex && columnIndex <= _lastCol;
  178. }
  179. public boolean isAdjacentBefore(ColumnInfoRecord other) {
  180. return _lastCol == other._firstCol - 1;
  181. }
  182. /**
  183. * @param other the format to match with
  184. *
  185. * @return {@code true} if the format, options and column width match
  186. */
  187. public boolean formatMatches(ColumnInfoRecord other) {
  188. if (_xfIndex != other._xfIndex) {
  189. return false;
  190. }
  191. if (_options != other._options) {
  192. return false;
  193. }
  194. return _colWidth == other._colWidth;
  195. }
  196. public short getSid() {
  197. return sid;
  198. }
  199. public void serialize(LittleEndianOutput out) {
  200. out.writeShort(getFirstColumn());
  201. out.writeShort(getLastColumn());
  202. out.writeShort(getColumnWidth());
  203. out.writeShort(getXFIndex());
  204. out.writeShort(_options);
  205. out.writeShort(field_6_reserved);
  206. }
  207. protected int getDataSize() {
  208. return 12;
  209. }
  210. @Override
  211. public ColumnInfoRecord copy() {
  212. return new ColumnInfoRecord(this);
  213. }
  214. @Override
  215. public HSSFRecordTypes getGenericRecordType() {
  216. return HSSFRecordTypes.COLUMN_INFO;
  217. }
  218. @Override
  219. public Map<String, Supplier<?>> getGenericProperties() {
  220. return GenericRecordUtil.getGenericProperties(
  221. "firstColumn", this::getFirstColumn,
  222. "lastColumn", this::getLastColumn,
  223. "columnWidth", this::getColumnWidth,
  224. "xfIndex", this::getXFIndex,
  225. "options", () -> _options,
  226. "hidden", this::getHidden,
  227. "outlineLevel", this::getOutlineLevel,
  228. "collapsed", this::getCollapsed
  229. );
  230. }
  231. }