summaryrefslogtreecommitdiffhomepage
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
parenta4067c3301aea5470c636d206034e4e787196df6 (diff)
downloadcaxlsx-c2a4f6a0f0c74b337447343e5f72674695737d88.tar.gz
caxlsx-c2a4f6a0f0c74b337447343e5f72674695737d88.zip
Adding in proper defined names so we can repeat header rows for each printed page.
-rw-r--r--.~lock.example.xlsx#2
-rwxr-xr-xexamples/example.rb6
-rw-r--r--lib/axlsx/drawing/d_lbls.rb5
-rw-r--r--lib/axlsx/workbook/defined_name.rb159
-rw-r--r--lib/axlsx/workbook/defined_names.rb17
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter.rb2
6 files changed, 188 insertions, 3 deletions
diff --git a/.~lock.example.xlsx# b/.~lock.example.xlsx#
index b9e92393..3f2abfdb 100644
--- a/.~lock.example.xlsx#
+++ b/.~lock.example.xlsx#
@@ -1 +1 @@
-,randym,Morgans-MacBook-Air.local,26.07.2012 08:17,file:///Users/randym/Library/Application%20Support/LibreOffice/3; \ No newline at end of file
+,randym,Morgans-MacBook-Air.local,31.07.2012 13:05,file:///Users/randym/Library/Application%20Support/LibreOffice/3; \ No newline at end of file
diff --git a/examples/example.rb b/examples/example.rb
index 9a355036..2da0f5f2 100755
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -314,6 +314,12 @@ wb.add_worksheet(:name => "No Gridlines") do |sheet|
end
##```
+#```ruby
+wb.add_worksheet(:name => "repeated header") do |sheet|
+ sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
+ 200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
+ wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
+end
# Sheet Protection and excluding cells from locking.
unlocked = wb.styles.add_style :locked => false
diff --git a/lib/axlsx/drawing/d_lbls.rb b/lib/axlsx/drawing/d_lbls.rb
index f7126a67..eb4bfea5 100644
--- a/lib/axlsx/drawing/d_lbls.rb
+++ b/lib/axlsx/drawing/d_lbls.rb
@@ -91,7 +91,10 @@ module Axlsx
def to_xml_string(str = '')
validate_attributes_for_chart_type
str << '<c:dLbls>'
- instance_values.each { |name, value| str << "<c:#{Axlsx::camel(name, false)} val='#{value}' />" }
+ instance_values.each do |key, value|
+ next if key == :formula
+ str << "<c:#{Axlsx::camel(key, false)} val='#{value}' />"
+ end
str << '</c:dLbls>'
end
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
diff --git a/lib/axlsx/workbook/defined_names.rb b/lib/axlsx/workbook/defined_names.rb
new file mode 100644
index 00000000..88a8b85f
--- /dev/null
+++ b/lib/axlsx/workbook/defined_names.rb
@@ -0,0 +1,17 @@
+module Axlsx
+
+ class DefinedNames < SimpleTypedList
+
+ def initialize
+ super DefinedName
+ end
+
+ def to_xml_string(str = '')
+ return if @list.empty?
+ str << "<definedNames>"
+ each { |defined_name| defined_name.to_xml_string(str) }
+ str << '</definedNames>'
+ end
+ end
+end
+
diff --git a/lib/axlsx/workbook/worksheet/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter.rb
index ff0c93d1..10873119 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter.rb
@@ -13,7 +13,7 @@ module Axlsx
def defined_name
return unless range
Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
- end
+ end
def to_xml_string(str='')
str << "<autoFilter ref='#{range}'></autoFilter>"