summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/defined_name.rb
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-07-31 13:06:33 +0900
committerRandy Morgan <[email protected]>2012-07-31 13:17:03 +0900
commitc2a4f6a0f0c74b337447343e5f72674695737d88 (patch)
tree24e9c7c2a86aa85b16862d037c8660df22bbc473 /lib/axlsx/workbook/defined_name.rb
parenta4067c3301aea5470c636d206034e4e787196df6 (diff)
downloadcaxlsx-c2a4f6a0f0c74b337447343e5f72674695737d88.tar.gz
caxlsx-c2a4f6a0f0c74b337447343e5f72674695737d88.zip
Adding in proper defined names so we can repeat header rows for each printed page.
Diffstat (limited to 'lib/axlsx/workbook/defined_name.rb')
-rw-r--r--lib/axlsx/workbook/defined_name.rb159
1 files changed, 159 insertions, 0 deletions
diff --git a/lib/axlsx/workbook/defined_name.rb b/lib/axlsx/workbook/defined_name.rb
new file mode 100644
index 00000000..391425c7
--- /dev/null
+++ b/lib/axlsx/workbook/defined_name.rb
@@ -0,0 +1,159 @@
+# <definedNames>
+# <definedName name="_xlnm.Print_Titles" localSheetId="0">Sheet1!$1:$1</definedName>
+# </definedNames>
+
+#<xsd:complexType name="CT_DefinedName">
+# <xsd:simpleContent>
+# <xsd:extension base="ST_Formula">
+# <xsd:attribute name="name" type="s:ST_Xstring" use="required"/>
+# <xsd:attribute name="comment" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="customMenu" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="description" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="help" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="statusBar" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="localSheetId" type="xsd:unsignedInt" use="optional"/>
+# <xsd:attribute name="hidden" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="function" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="vbProcedure" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="xlm" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="functionGroupId" type="xsd:unsignedInt" use="optional"/>
+# <xsd:attribute name="shortcutKey" type="s:ST_Xstring" use="optional"/>
+# <xsd:attribute name="publishToServer" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="workbookParameter" type="xsd:boolean" use="optional" default="false"/>
+# </xsd:extenstion>
+# </xsd:simpleContent>
+
+module Axlsx
+ # This element defines the defined names that are defined within this workbook.
+ # Defined names are descriptive text that is used to represents a cell, range of cells, formula, or constant value.
+ # Use easy-to-understand names, such as Products, to refer to hard to understand ranges, such as Sales!C20:C30.
+ # A defined name in a formula can make it easier to understand the purpose of the formula.
+ # @example
+ # The formula =SUM(FirstQuarterSales) might be easier to identify than =SUM(C20:C30
+ #
+ # Names are available to any sheet.
+ # @example
+ # If the name ProjectedSales refers to the range A20:A30 on the first worksheet in a workbook,
+ # you can use the name ProjectedSales on any other sheet in the same workbook to refer to range A20:A30 on the first worksheet.
+ # Names can also be used to represent formulas or values that do not change (constants).
+ #
+ # @example
+ # The name SalesTax can be used to represent the sales tax amount (such as 6.2 percent) applied to sales transactions.
+ # You can also link to a defined name in another workbook, or define a name that refers to cells in another workbook.
+ #
+ # @example
+ # The formula =SUM(Sales.xls!ProjectedSales) refers to the named range ProjectedSales in the workbook named Sales.
+ # A compliant producer or consumer considers a defined name in the range A1-XFD1048576 to be an error.
+ # All other names outside this range can be defined as names and overrides a cell reference if an ambiguity exists.
+ #
+ # @example
+ # For clarification: LOG10 is always a cell reference, LOG10() is always formula, LOGO1000 can be a defined name that overrides a cell reference.
+ class DefinedName
+ # creates a new DefinedName.
+ # @param [String] formula - the formula the defined name references
+ # @param [Hash] options - A hash of key/value pairs that will be mapped to this instances attributes.
+ # @option [String] name - Specifies the name that appears in the user interface for the defined name.
+ # This attribute is required.
+ # The following built-in names are defined in this SpreadsheetML specification:
+ # Print
+ # _xlnm.Print_Area: this defined name specifies the workbook's print area.
+ # _xlnm.Print_Titles: this defined name specifies the row(s) or column(s) to repeat
+ # the top of each printed page.
+ # Filter & Advanced Filter
+ # _xlnm.Criteria: this defined name refers to a range containing the criteria values
+ # to be used in applying an advanced filter to a range of data.
+ # _xlnm._FilterDatabase: can be one of the following
+ # a. this defined name refers to a range to which an advanced filter has been
+ # applied. This represents the source data range, unfiltered.
+ # b. This defined name refers to a range to which an AutoFilter has been
+ # applied.
+ # _xlnm.Extract: this defined name refers to the range containing the filtered output
+ # values resulting from applying an advanced filter criteria to a source range.
+ # Miscellaneous
+ # _xlnm .Consolidate_Area: the defined name refers to a consolidation area.
+ # _xlnm .Database: the range specified in the defined name is from a database data source.
+ # _xlnm .Sheet_Title: the defined name refers to a sheet title.
+ # @option [String] comment - A comment to optionally associate with the name
+ # @option [String] custom_menu - The menu text for the defined name
+ # @option [String] description - An optional description for the defined name
+ # @option [String] help - The help topic to display for this defined name
+ # @option [String] status_bar - The text to display on the application status bar when this defined name has focus
+ # @option [String] local_sheet_id - Specifies the sheet index in this workbook where data from an external reference is displayed
+ # @option [Boolean] hidden - Specifies a boolean value that indicates whether the defined name is hidden in the user interface.
+ # @option [Boolean] function - Specifies a boolean value that indicates that the defined name refers to a user-defined function.
+ # This attribute is used when there is an add-in or other code project associated with the file.
+ # @option [Boolean] vb_proceedure - Specifies a boolean value that indicates whether the defined name is related to an external function, command, or other executable code.
+ # @option [Boolean] xlm - Specifies a boolean value that indicates whether the defined name is related to an external function, command, or other executable code.
+ # @option [Integer] function_group_id - Specifies the function group index if the defined name refers to a function.
+ # The function group defines the general category for the function.
+ # This attribute is used when there is an add-in or other code project associated with the file.
+ # See Open Office XML Part 1 for more info.
+ # @option [String] short_cut_key - Specifies the keyboard shortcut for the defined name.
+ # @option [Boolean] publish_to_server - Specifies a boolean value that indicates whether the defined name is included in the
+ # version of the workbook that is published to or rendered on a Web or application server.
+ # @option [Boolean] workbook_parameter - Specifies a boolean value that indicates that the name is used as a workbook parameter on a
+ # version of the workbook that is published to or rendered on a Web or application server.
+ def initialize(formula, options={})
+ @formula = formula
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+ attr_reader :local_sheet_id
+
+ def local_sheet_id=(value)
+ Axlsx::validate_unsigned_int(value)
+ @local_sheet_id = value
+ end
+ # string attributes that will be added when this class is evaluated
+ STRING_ATTRIBUTES = [:short_cut_key, :status_bar, :help, :description, :custom_menu, :comment, :name]
+
+ # boolean attributes that will be added when this class is evaluated
+ BOOLEAN_ATTRIBUTES = [:workbook_parameter, :publish_to_server, :xml, :vb_proceedure, :function, :hidden]
+
+ # Dynamically create string attribute accessors
+ STRING_ATTRIBUTES.each do |attr|
+ class_eval %{
+ # The #{attr} attribute reader
+ # @return [String]
+ attr_reader :#{attr}
+
+ # The #{attr} writer
+ # @param [String] value The value to assign to #{attr}
+ # @return [String]
+ def #{attr}=(value)
+ Axlsx::validate_string(value)
+ @#{attr}= value
+ end
+ }
+ end
+
+ # Dynamically create boolean attribute accessors
+ BOOLEAN_ATTRIBUTES.each do |attr|
+ class_eval %{
+ # The #{attr} attribute reader
+ # @return [Boolean]
+ attr_reader :#{attr}
+
+ # The #{attr} writer
+ # @param [Boolean] value The value to assign to #{attr}
+ # @return [Boolean]
+ def #{attr}=(value)
+ Axlsx::validate_boolean(value)
+ @#{attr} = value
+ end
+ }
+ end
+
+ def to_xml_string(str='')
+ str << '<definedName'
+ instance_values.each do |name, value|
+ unless name == 'formula'
+ str << ' ' << Axlsx::camel(name, false) << "='#{value}'"
+ end
+ end
+ str << '>' << @formula
+ str << '</definedName>'
+ end
+ end
+end