axlsx_rad 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 834382ebf19e94d28b67e1b8fdf27036cef2c31e
4
+ data.tar.gz: 4deb68fde5206dd7292903386aa30e8d8c61689d
5
+ SHA512:
6
+ metadata.gz: c8d48310cb59052aab604e80bc6b56efc499b9fea63eb16792e64f0c647adc10a4689543565cbc2c8465ad983c8839bc3f4cd8004a966cb04fde5079e1c6eacf
7
+ data.tar.gz: e328b22d9cc7f3431717d7a70cc33bf510a7caafbdf9ce469c23c8d742718c582f619ce68e7090cff9b6b8d997f7e53c5870fa30f8ebf412da9f5d8f31c1f97b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ CHANGELOG
2
+ ---------
3
+
4
+ - **2014-03-21**: 0.0.1
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 John Wang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,150 @@
1
+ AxlsxRad - Rapid XLSX Writer for Customized Spreadsheets
2
+ ========================================================
3
+
4
+ Synopsis
5
+ --------
6
+
7
+ AxlsxRad provides a rapid capability to generate styled spreadsheets of JSON
8
+ objects to enable the creation of reports with both per-row and per-cell
9
+ highlighting.
10
+
11
+ This is accomplished by leveraging JsonDoc's encapsulation of JSON objects
12
+ with easy retrieval of property names and values as arrays that are used by
13
+ AxlsxRad to populate XLSX spreadsheets. This is done by simply providing an
14
+ array of JSON property keys and then adding JsonDoc objects to the worksheet.
15
+
16
+ Axlsx additionally supports rapid styling of spreadsheets by accepting a
17
+ set of styles and rules for applying those styles to each row. Both a header
18
+ row and data rows can be styled with both default background styles for the
19
+ entire row and custom styles on a per cell basis. Per-cell styles are assigned
20
+ by JsonDoc property key so there is no need to create arrays of styles per
21
+ row and to remember which column is located in which array index - it is all
22
+ done automatically.
23
+
24
+ Customization consists of:
25
+
26
+ * a list of JSON property keys that are used as columns
27
+ * a dictionary of styles by style key
28
+ * overridable header and row methods tto provide custom styling rules
29
+
30
+ Finally, Axlsx provides full access to the Axlsx package, workbook and
31
+ worksheet objects so fast customization can be supported in addition to
32
+ standard access.
33
+
34
+ Installing
35
+ ----------
36
+
37
+ Download and install axlsx_rad with the following:
38
+
39
+ gem install axlsx_rad
40
+
41
+ #Examples
42
+ ---------
43
+
44
+ require 'axlsx_rad'
45
+
46
+ # Create an AxlsxRad::Config object, often per-worksheet
47
+ # Define worksheet columns. These keys should exist in JsonDoc
48
+ # object / subclass properties.
49
+ columns = [ :first_name, :last_name, :email_address, :github_username ]
50
+
51
+ # Define styles
52
+ styles = {
53
+ :blue => { :bg_color => '00CCFF' },
54
+ :green => { :bg_color => '00FF99' },
55
+ :ltgreen => { :bg_color => '99FF99' },
56
+ :yellow => { :bg_color => 'FFFF99' },
57
+ :orange => { :bg_color => 'FFCC66' },
58
+ :white => { :bg_color => 'FFFFFF' },
59
+ :head => { :bg_color => '00FFFF', :b => true}
60
+ }
61
+
62
+ # Subclass AxlsxRad::Config object. This object has attributes: oColumns
63
+ # and dStylesCfg. It doesn't matter how they are set but they should be
64
+ # set before instantiating a AxlsxRad::Worksheet object. Additionally,
65
+ # the #getStylesForHeader() and #getStylesForDocument( document ) methods
66
+ # should be overridden to return a style hash as shown below:
67
+ #
68
+ # The contract contains: @aColumns, @dStylesCfg, #getStylesForHeader, and
69
+ # #getStylesForDocument( JsonDoc::Document ).
70
+ #
71
+ # The style names used in the reponse hash should correspond to the style
72
+ # keys used in the styles configuration hash above.
73
+
74
+ class MyWorksheetConfig < AxlsxRad::Config
75
+ def initialize(columns=[],styles={})
76
+ @aColumns = columns
77
+ @dStylesCfg = styles
78
+ end
79
+ def getStylesForHeader()
80
+ return { :bgstyle => :head }
81
+ end
82
+ def getStylesForDocument( oDocument=nil )
83
+ dStyles = { :bgstyle => :white, :properties => {} }
84
+ if oDocument.getProp(:github_username)
85
+ dStyles[:bgstyle] = :green
86
+ dstyles[:properties][:github_username] = :yellow
87
+ elsif oDocument.getProp(:email_address)
88
+ dStyles[:bgstyle] = :blue
89
+ dstyles[:properties][:email_address] = :yellow
90
+ end
91
+ return dStyles
92
+ end
93
+ end
94
+
95
+ # Instantiate a config object
96
+ config = MyWorksheetConfig.new( columns, styles )
97
+
98
+ # Instantiate a workbook
99
+ workbook = AxlsxRad::Workbook.new
100
+
101
+ # Instantiate a worksheet with name and config
102
+ worksheet = workbook.addWorksheet( "Users", config )
103
+
104
+ # Add documents where user is a JsonDoc::Document subclass
105
+ # with properties matching those tested against in Axlsx::Config
106
+ # subclass.
107
+
108
+ users.each do |user|
109
+ worksheet.addDocument( user )
110
+ end
111
+
112
+ # Write XLSX spreadsheet
113
+ workbook.oAxlsx.serialize('example_users.xlsx')
114
+
115
+ #Documentation
116
+ --------------
117
+
118
+ This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:
119
+
120
+ $ gem install yard
121
+ $ yard server -g
122
+
123
+ #Change Log
124
+ -----------
125
+
126
+ - **2014-03-20**: 0.0.1
127
+ - Initial release
128
+
129
+ #Links
130
+ ------
131
+
132
+ Axlsx
133
+
134
+ https://rubygems.org/gems/axlsx
135
+
136
+ JsonDoc
137
+
138
+ https://rubygems.org/gems/jsondoc
139
+
140
+ #Copyright and License
141
+ ----------------------
142
+
143
+ AxlsxRad &copy; 2014 by [John Wang](mailto:johncwang@gmail.com).
144
+
145
+ AxlsxRad is licensed under the MIT license. Please see the LICENSE document for more information.
146
+
147
+ Warranty
148
+ --------
149
+
150
+ This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the library.'
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/test_*.rb'
11
+ t.verbose = false
12
+ end
13
+
14
+ desc 'Generate YARD documentation.'
15
+ task :gendoc do
16
+ #puts 'yard doc generation disabled until JRuby build native extensions for redcarpet or yard removes the dependency.'
17
+ system "yardoc"
18
+ system "yard stats --list-undoc"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,21 @@
1
+ module AxlsxRad
2
+ class Config
3
+ attr_accessor :aColumns
4
+ attr_accessor :dStylesCfg
5
+ def initialize(aColumns=[],dStylesCfg={})
6
+ @aColumns = aColumns
7
+ @dStylesCfg = dStylesCfg
8
+ end
9
+ # Override this method with rules to return style key
10
+ def getStylesForHeader()
11
+ return { :bgstyle => :head }
12
+ end
13
+ def getStylesForDocument(oJsondoc=nil)
14
+ dStyles = { :bgstyle => nil, :properties => {} }
15
+ unless oJsondoc.is_a?(JsonDoc::Document)
16
+ raise ArgumentError, 'E_NOT_JSONDOC'
17
+ end
18
+ return dStyles
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,149 @@
1
+ require 'axlsx'
2
+
3
+ module AxlsxRad
4
+ class Workbook
5
+ attr_accessor :oAxlsx
6
+
7
+ def initialize(oAxlsx=nil)
8
+ @oAxlsx = oAxlsx || Axlsx::Package.new
9
+ @dWorksheets = {}
10
+ end
11
+
12
+ def addWorksheet(sWorksheetName=nil,oWsConfig=nil)
13
+ if @dWorksheets.has_key?(sWorksheetName)
14
+ raise RuntimeError, "E_WORKSHEET_EXISTS [#{sWorksheetName}]"
15
+ end
16
+ oAwsWorksheet = AxlsxRad::Worksheet.new(@oAxlsx,sWorksheetName,oWsConfig)
17
+ @dWorksheets[ sWorksheetName ] = oAwsWorksheet
18
+ return oAwsWorksheet
19
+ end
20
+
21
+ def addWorksheetDocument(sWorksheetName=nil,oJsondoc=nil)
22
+ if sWorksheetName.nil? || ! sWorksheetName.kind_of?(String)
23
+ raise ArgumentError, 'E_BAD_WORKSHEET_NAME'
24
+ elsif ! @dWorksheets.has_key?( sWorksheetName )
25
+ raise ArgumentError, 'E_WORKSHEET_DOES_NOT_EXIST'
26
+ end
27
+ @dWorksheets[ sWorksheetName ].addDocument( oJsondoc )
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ module AxlsxRad
34
+ class Worksheet
35
+ attr_accessor :oAxlsx
36
+ attr_accessor :oAxlsxWorksheet
37
+
38
+ def initialize(oAxlsx=nil,sName=nil,oWsConfig=nil)
39
+ @oAxlsx = oAxlsx
40
+ unless oWsConfig.is_a?(AxlsxRad::Config)
41
+ raise ArgumentError, 'E_NO_AXLSX_RAD_WORKSHEET_CONFIG'
42
+ end
43
+ @oAxlsxWorksheet = @oAxlsx.workbook.add_worksheet(:name => sName)
44
+ @oWsConfig = oWsConfig
45
+ self.loadWsConfig( oWsConfig )
46
+ @bHasHead = false
47
+ end
48
+
49
+ def loadWsConfig(oWsConfig=nil)
50
+ @aColumns = oWsConfig.aColumns || []
51
+ @dStylesCfg = oWsConfig.dStylesCfg.is_a?(Hash) ? oWsConfig.dStylesCfg : {}
52
+ @dStylesAxlsx = getStylesAxlsx( @oAxlsxWorksheet, @dStylesCfg )
53
+ @dStylesColumns = getStylesColumns( @dStylesAxlsx, @aColumns )
54
+ end
55
+
56
+ def addDocument(oJsondoc=nil)
57
+ unless @bHasHead
58
+ aStyles = getStylesForHeader
59
+ aValues = oJsondoc.getDescArrayForProperties( @aColumns )
60
+ if aStyles.is_a?(Fixnum) || aStyles.is_a?(Array)
61
+ @oAxlsxWorksheet.add_row aValues, :style => aStyles
62
+ else
63
+ @oAxlsxWorksheet.add_row aValues
64
+ end
65
+ @bHasHead = true
66
+ end
67
+ aValues = oJsondoc.getValArrayForProperties( @aColumns )
68
+ aStyles = getStylesForDocument( oJsondoc )
69
+ if aStyles.is_a?(Fixnum) || aStyles.is_a?(Array)
70
+ @oAxlsxWorksheet.add_row aValues, :style => aStyles
71
+ else
72
+ @oAxlsxWorksheet.add_row aValues
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def symbolizeArray(aAny=[])
79
+ (0..aAny.length-1).each do |i|
80
+ aAny[i] = aAny[i].to_sym if aAny[i].is_a?(String)
81
+ end
82
+ return aAny
83
+ end
84
+
85
+ def getStylesAxlsx(oAxlsxWorksheet=nil,dStylesCfg=nil)
86
+ dStylesAxlsx = {}
87
+ dStylesCfg.keys.each do |yStyle|
88
+ dStylesAxlsx[ yStyle ] = oAxlsxWorksheet.styles.add_style( @dStylesCfg[ yStyle ] )
89
+ end
90
+ return dStylesAxlsx
91
+ end
92
+
93
+ def getStylesColumns(dStylesAxlsx=nil,aColumns=nil)
94
+ dStylesColumns = {}
95
+ dStylesAxlsx.keys.each do |yStyle|
96
+ dStylesColumns[yStyle] ||= []
97
+ oStyleAxlsx = dStylesAxlsx[yStyle]
98
+ (0..aColumns.length-1).each do |i|
99
+ dStylesColumns[yStyle].push( oStyleAxlsx )
100
+ end
101
+ end
102
+ return dStylesColumns
103
+ end
104
+
105
+ def getStylesForHeader()
106
+ aStyles = nil
107
+ if @oWsConfig.is_a?(AxlsxRad::Config)
108
+ dStyles = @oWsConfig.getStylesForHeader
109
+ aStyles = getStylesArrayForHash( dStyles )
110
+ end
111
+ return aStyles
112
+ end
113
+
114
+ def getStylesForDocument(oJsondoc=nil)
115
+ aStyle = nil
116
+ if @oWsConfig.is_a?(AxlsxRad::Config)
117
+ dStyle = @oWsConfig.getStylesForDocument(oJsondoc)
118
+ aStyle = getStylesArrayForHash( dStyle )
119
+ end
120
+ return aStyle
121
+ end
122
+
123
+ def getStylesArrayForHash(dStyles={})
124
+ yStyleBg = dStyles.has_key?(:bgstyle) ? dStyles[:bgstyle] : nil
125
+ yStyleBg = yStyleBg.to_sym if yStyleBg.is_a?(String)
126
+ aStyles = []
127
+ if dStyles.has_key?(:properties) && dStyles[:properties].is_a?(Hash) \
128
+ && dStyles[:properties].keys.length > 0
129
+ @aColumns.each do |yColumn|
130
+ if dStyles[:properties].has_key?(yColumn)
131
+ yStyleCell = dStyles[:properties][yColumn]
132
+ yStyleCell = yStyleCell.to_sym if yStyleCell.is_a?(String)
133
+ aStyles.push( @dStylesAxlsx[ yStyleCell ] )
134
+ elsif yStyleBg
135
+ aStyles.push( @dStylesAxlsx[ yStyleBg ] )
136
+ else
137
+ aStyles.push( nil )
138
+ end
139
+ end
140
+ elsif yStyleBg
141
+ aStyles = @dStylesColumns[yStyleBg]
142
+ else
143
+ aStyles = nil
144
+ end
145
+ return aStyles
146
+ end
147
+
148
+ end
149
+ end
data/lib/axlsx_rad.rb ADDED
@@ -0,0 +1,4 @@
1
+ module AxlsxRad
2
+ require 'axlsx_rad/workbook'
3
+ require 'axlsx_rad/config'
4
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'axlsx_rad'
3
+
4
+ class AxlsxRadTest < Test::Unit::TestCase
5
+ def testSetup
6
+
7
+ config = AxlsxRad::Config.new
8
+ workbook = AxlsxRad::Workbook.new
9
+ worksheet = workbook.addWorksheet("My Sheet",config)
10
+
11
+ assert_equal 'AxlsxRad::Config', config.class.name
12
+ assert_equal 'AxlsxRad::Workbook', workbook.class.name
13
+ assert_equal 'AxlsxRad::Worksheet', worksheet.class.name
14
+
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: axlsx_rad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: axlsx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.5
27
+ description: Efficiently create styled spreadsheets using JsonDoc::Document objects
28
+ email: john@johnwang.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - CHANGELOG.md
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - VERSION
38
+ - lib/axlsx_rad.rb
39
+ - lib/axlsx_rad/config.rb
40
+ - lib/axlsx_rad/workbook.rb
41
+ - test/test_setup.rb
42
+ homepage: http://johnwang.com/
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.1
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: AxlsxRad - Rapid XLSX Writer for Customized Spreadsheets
66
+ test_files: []