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.

Ref3DPtg.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.ss.formula.ptg;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.ss.formula.ExternSheetReferenceToken;
  19. import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
  20. import org.apache.poi.ss.formula.WorkbookDependentFormula;
  21. import org.apache.poi.ss.util.CellReference;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.LittleEndianInput;
  24. import org.apache.poi.util.LittleEndianOutput;
  25. /**
  26. * Reference 3D Ptg<p>
  27. * Defined a cell in extern sheet.<p>
  28. *
  29. * This is HSSF only, as it matches the HSSF file format way of
  30. * referring to the sheet by an extern index. The XSSF equivalent
  31. * is {@link Ref3DPxg}
  32. */
  33. public final class Ref3DPtg extends RefPtgBase implements WorkbookDependentFormula, ExternSheetReferenceToken {
  34. public static final byte sid = 0x3a;
  35. private static final int SIZE = 7; // 6 + 1 for Ptg
  36. private int field_1_index_extern_sheet;
  37. public Ref3DPtg(Ref3DPtg other) {
  38. super(other);
  39. field_1_index_extern_sheet = other.field_1_index_extern_sheet;
  40. }
  41. public Ref3DPtg(LittleEndianInput in) {
  42. field_1_index_extern_sheet = in.readShort();
  43. readCoordinates(in);
  44. }
  45. public Ref3DPtg(String cellref, int externIdx ) {
  46. this(new CellReference(cellref), externIdx);
  47. }
  48. public Ref3DPtg(CellReference c, int externIdx) {
  49. super(c);
  50. setExternSheetIndex(externIdx);
  51. }
  52. public void write(LittleEndianOutput out) {
  53. out.writeByte(sid + getPtgClass());
  54. out.writeShort(getExternSheetIndex());
  55. writeCoordinates(out);
  56. }
  57. @Override
  58. public byte getSid() {
  59. return sid;
  60. }
  61. public int getSize() {
  62. return SIZE;
  63. }
  64. public int getExternSheetIndex() {
  65. return field_1_index_extern_sheet;
  66. }
  67. public void setExternSheetIndex(int index) {
  68. field_1_index_extern_sheet = index;
  69. }
  70. public String format2DRefAsString() {
  71. return formatReferenceAsString();
  72. }
  73. /**
  74. * @return text representation of this cell reference that can be used in text
  75. * formulas. The sheet name will get properly delimited if required.
  76. */
  77. public String toFormulaString(FormulaRenderingWorkbook book) {
  78. return ExternSheetNameResolver.prependSheetName(book, field_1_index_extern_sheet, formatReferenceAsString());
  79. }
  80. public String toFormulaString() {
  81. throw new RuntimeException("3D references need a workbook to determine formula text");
  82. }
  83. @Override
  84. public Ref3DPtg copy() {
  85. return new Ref3DPtg(this);
  86. }
  87. @Override
  88. public Map<String, Supplier<?>> getGenericProperties() {
  89. return GenericRecordUtil.getGenericProperties(
  90. "base", super::getGenericProperties,
  91. "externSheetIndex", this::getExternSheetIndex
  92. );
  93. }
  94. }