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.

Name.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.usermodel;
  16. /**
  17. * Represents a defined name for a range of cells.
  18. * <p>
  19. * A name is a meaningful shorthand that makes it easier to understand the purpose of a
  20. * cell reference, constant or a formula.
  21. * </p>
  22. * Examples:
  23. * <pre>{@code
  24. * Sheet sheet = workbook.createSheet("Loan Calculator");
  25. * Name name;
  26. *
  27. * name = workbook.createName();
  28. * name.setNameName("Interest_Rate");
  29. * name.setRefersToFormula("'Loan Calculator'!$E$5");
  30. *
  31. * name = wb.createName();
  32. * name.setNameName("Loan_Amount");
  33. * name.setRefersToFormula("'Loan Calculator'!$E$4");
  34. *
  35. * name = wb.createName();
  36. * name.setNameName("Number_of_Payments");
  37. * name.setRefersToFormula("'Loan Calculator'!$E$10");
  38. *
  39. * name = wb.createName();
  40. * name.setNameName("Monthly_Payment");
  41. * name.setRefersToFormula("-PMT(Interest_Rate/12,Number_of_Payments,Loan_Amount)");
  42. *
  43. * name = wb.createName();
  44. * name.setNameName("Values_Entered");
  45. * name.setRefersToFormula("IF(Loan_Amount*Interest_Rate > 0,1,0)");
  46. *
  47. * }</pre>
  48. */
  49. public interface Name {
  50. /**
  51. * Get the sheets name which this named range is referenced to
  52. *
  53. * @return sheet name, which this named range referred to
  54. */
  55. String getSheetName();
  56. /**
  57. * Gets the name of the named range
  58. *
  59. * @return named range name
  60. */
  61. String getNameName();
  62. /**
  63. * Sets the name of the named range
  64. *
  65. * <p>The following is a list of syntax rules that you need to be aware of when you create and edit names.</p>
  66. * <ul>
  67. * <li><strong>Valid characters</strong>
  68. * The first character of a name must be a letter, an underscore character (_), or a backslash (\).
  69. * Remaining characters in the name can be letters, numbers, periods, and underscore characters.
  70. * </li>
  71. * <li><strong>Cell references disallowed</strong>
  72. * Names cannot be the same as a cell reference, such as Z$100 or R1C1.</li>
  73. * <li><strong>Spaces are not valid</strong>
  74. * Spaces are not allowed as part of a name. Use the underscore character (_) and period (.) as word separators, such as, Sales_Tax or First.Quarter.
  75. * </li>
  76. * <li><strong>Name length</strong>
  77. * A name can contain up to 255 characters.
  78. * </li>
  79. * <li><strong>Case sensitivity</strong>
  80. * Names can contain uppercase and lowercase letters.
  81. * </li>
  82. * </ul>
  83. * <p>
  84. * A name must always be unique within its scope. POI prevents you from defining a name that is not unique
  85. * within its scope. However you can use the same name in different scopes. Example:
  86. * <pre>{@code
  87. * //by default names are workbook-global
  88. * Name name;
  89. * name = workbook.createName();
  90. * name.setNameName("sales_08");
  91. *
  92. * name = workbook.createName();
  93. * name.setNameName("sales_08"); //will throw an exception: "The workbook already contains this name (case-insensitive)"
  94. *
  95. * //create sheet-level name
  96. * name = workbook.createName();
  97. * name.setSheetIndex(0); //the scope of the name is the first sheet
  98. * name.setNameName("sales_08"); //ok
  99. *
  100. * name = workbook.createName();
  101. * name.setSheetIndex(0);
  102. * name.setNameName("sales_08"); //will throw an exception: "The sheet already contains this name (case-insensitive)"
  103. *
  104. * }</pre>
  105. *
  106. * @param name named range name to set
  107. * @throws IllegalArgumentException if the name is invalid or the already exists within its scope (case-insensitive)
  108. */
  109. void setNameName(String name);
  110. /**
  111. * Returns the formula that the name is defined to refer to.
  112. *
  113. * @return the reference for this name, {@code null} if it has not been set yet. Never empty string
  114. * @see #setRefersToFormula(String)
  115. */
  116. String getRefersToFormula();
  117. /**
  118. * Sets the formula that the name is defined to refer to. The following are representative examples:
  119. *
  120. * <ul>
  121. * <li>{@code 'My Sheet'!$A$3}</li>
  122. * <li>{@code 8.3}</li>
  123. * <li>{@code HR!$A$1:$Z$345}</li>
  124. * <li>{@code SUM(Sheet1!A1,Sheet2!B2)}</li>
  125. * <li>{@code -PMT(Interest_Rate/12,Number_of_Payments,Loan_Amount)}</li>
  126. * </ul>
  127. *
  128. * Note: Using relative values like 'A1:B1' can lead to unexpected moving of
  129. * the cell that the name points to when working with the workbook in Microsoft Excel,
  130. * usually using absolute references like '$A$1:$B$1' avoids this, see also
  131. * https://superuser.com/a/1031047/126954
  132. *
  133. * @param formulaText the reference for this name
  134. * @throws IllegalArgumentException if the specified formulaText is unparsable
  135. */
  136. void setRefersToFormula(String formulaText);
  137. /**
  138. * Checks if this name is a function name
  139. *
  140. * @return true if this name is a function name
  141. */
  142. boolean isFunctionName();
  143. /**
  144. * Checks if this name points to a cell that no longer exists
  145. *
  146. * @return {@code true} if the name refers to a deleted cell, {@code false} otherwise
  147. */
  148. boolean isDeleted();
  149. /**
  150. * Checks if this name is hidden, eg one of the built-in Excel
  151. * internal names
  152. *
  153. * @return {@code true} if the name is a hidden name, {@code false} otherwise
  154. */
  155. boolean isHidden();
  156. /**
  157. * Tell Excel that this name applies to the worksheet with the specified index instead of the entire workbook.
  158. *
  159. * @param sheetId the sheet index this name applies to, -1 unsets this property making the name workbook-global
  160. * @throws IllegalArgumentException if the sheet index is invalid.
  161. */
  162. void setSheetIndex(int sheetId);
  163. /**
  164. * Returns the sheet index this name applies to.
  165. *
  166. * @return the sheet index this name applies to, -1 if this name applies to the entire workbook
  167. */
  168. int getSheetIndex();
  169. /**
  170. * Returns the comment the user provided when the name was created.
  171. *
  172. * @return the user comment for this named range
  173. */
  174. String getComment();
  175. /**
  176. * Sets the comment the user provided when the name was created.
  177. *
  178. * @param comment the user comment for this named range
  179. */
  180. void setComment(String comment);
  181. /**
  182. * Indicates that the defined name refers to a user-defined function.
  183. * This attribute is used when there is an add-in or other code project associated with the file.
  184. *
  185. * @param value {@code true} indicates the name refers to a function.
  186. */
  187. void setFunction(boolean value);
  188. }