xlsxtream 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b825830ee0ab905c1f96c80dd54ba8c307578d165ec429c5ffd275e64720225c
4
- data.tar.gz: b5b75f151994915de22b8a8533c92f3cdb35d9acfb7a8940b2a4baf7f688cce7
3
+ metadata.gz: e8cc8d872dffb10c74eca0a31318350d2343ee609475a67a1aedc4c515ecc786
4
+ data.tar.gz: 715eae8f94c4a59f86b4abb1ec263266a481383fee3ceee912804f96cc6c592a
5
5
  SHA512:
6
- metadata.gz: d91bb3c4804ac213150e6dfdff6a73f8919ac7f1ce649545af96a8bc328e45180d3ed06cfb0fd2431452d0a8f6999150fd12ccee70ac5dbbbdd23962e11b13e2
7
- data.tar.gz: 46d67651baf5d02202563fe4a1c2f756320626ec405cfae9a47b3f03e8b4d6b0b38b60d49889ef365a83deccb296baa6ded0666f141d707d2ea62d157c524bed
6
+ metadata.gz: d4c2025bc90c39d416af05af138c91439d4c0a16e9f20c0be718505c8809013044f060938284006d536be11862974f2041c983da0885f788fd02c09355df6c80
7
+ data.tar.gz: 70e5a60021a8c64a7adda9a3bcbac071a73f2fce8cc62c709ab502c34693e5c931532c5215023a10c627b31b1b68eeff9bc26f7b90e5d386455ab70f20aa581b
@@ -7,6 +7,7 @@ before_install: gem install bundler -v '~> 1.15' --conservative --minimal-deps
7
7
 
8
8
  rvm:
9
9
  - 2.1.10
10
- - 2.2.7
11
- - 2.3.4
12
- - 2.4.1
10
+ - 2.2.10
11
+ - 2.3.7
12
+ - 2.4.4
13
+ - 2.5.1
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.1.0 (2018-07-21)
4
+
5
+ - New `:columns` option, allowing column widths to be specified (#25)
6
+ - Fix compatibility with `ruby --enable-frozen-string-literal` (#27)
7
+ - Support giving the worksheet name as an option to write\_worksheet (#28)
8
+
3
9
  ## 2.0.1 (2018-03-11)
4
10
 
5
11
  - Rescue gracefully from invalid dates with auto-format (#22)
@@ -48,7 +54,7 @@
48
54
 
49
55
  ## 0.3.0 (2017-07-12)
50
56
 
51
- - Add support for auto-formatting
57
+ - Add support for auto-formatting (#8)
52
58
 
53
59
  ## 0.2.0 (2017-02-20)
54
60
 
data/README.md CHANGED
@@ -61,7 +61,7 @@ end
61
61
  # for the workbook or a single worksheet. The SST has to be kept in memory,
62
62
  # so do not use it if you have a huge amount of rows or a little duplication
63
63
  # of content across cells. A single SST is used for the whole workbook.
64
- xlsx.write_worksheet('SheetWithSST', use_shared_strings: true) do |sheet|
64
+ xlsx.write_worksheet(name: 'SheetWithSST', use_shared_strings: true) do |sheet|
65
65
  sheet << ['the', 'same', 'old', 'story']
66
66
  sheet << ['the', 'old', 'same', 'story']
67
67
  sheet << ['old', 'the', 'same', 'story']
@@ -72,7 +72,7 @@ end
72
72
  # "Number stored as text". Dates and times must be in the ISO-8601 format and
73
73
  # numeric values must contain only numbers and an optional decimal separator.
74
74
  # The strings true and false are detected as boolean values.
75
- xlsx.write_worksheet('SheetWithAutoFormat', auto_format: true) do |sheet|
75
+ xlsx.write_worksheet(name: 'SheetWithAutoFormat', auto_format: true) do |sheet|
76
76
  # these two rows will be identical in the xlsx-output
77
77
  sheet << [true, 11.85, DateTime.parse('2050-01-01T12:00'), Date.parse('1984-01-01')]
78
78
  sheet << ['true', '11.85', '2050-01-01T12:00', '1984-01-01']
@@ -90,8 +90,20 @@ Xlsxtream::Workbook.new(io, font: {
90
90
  family: 'Roman' # Swiss, Modern, Script, Decorative
91
91
  })
92
92
 
93
+ # Specifying column widths in pixels or characters; 3 column example;
94
+ # "pixel" widths appear to be *relative* to an assumed 11pt Calibri
95
+ # font, so if selecting a different font or size (see above), do not
96
+ # adjust widths to match. Calculate pixel widths for 11pt Calibri.
97
+ Xlsxtream::Workbook.new(io, columns: [
98
+ { width_pixels: 33 },
99
+ { width_chars: 7 },
100
+ { width_chars: 24 }
101
+ ])
102
+ # The :columns option can also be given to write_worksheet, so it's
103
+ # possible to have multiple worksheets with different column widths.
93
104
  ```
94
105
 
106
+
95
107
  ## Compatibility
96
108
 
97
109
  The current version of Xlsxtream requires at least Ruby 2.1.0.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "xlsxtream/version"
2
3
 
3
4
  module Xlsxtream
@@ -5,4 +6,5 @@ end
5
6
 
6
7
  require "xlsxtream/workbook"
7
8
  require "xlsxtream/worksheet"
9
+ require "xlsxtream/columns"
8
10
  require "xlsxtream/row"
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ module Xlsxtream
3
+ class Columns
4
+
5
+ # Pass an Array of column options Hashes. Symbol Hash keys and associated
6
+ # values are as follows:
7
+ #
8
+ # +width_chars+:: Approximate column with in characters, calculated per
9
+ # MSDN docs as if using a default 11 point Calibri font
10
+ # for a 96 DPI target. Specify as an integer.
11
+ #
12
+ # +width_pixels+:: Exact with of column in pixels. Specify as a Float.
13
+ # Overrides +width_chars+ if that is also provided.
14
+ #
15
+ def initialize(column_options_array)
16
+ @columns = column_options_array
17
+ end
18
+
19
+ def to_xml
20
+ xml = String.new('<cols>')
21
+
22
+ @columns.each_with_index do |column, index|
23
+ width_chars = column[ :width_chars ]
24
+ width_pixels = column[ :width_pixels ]
25
+
26
+ if width_chars.nil? && width_pixels.nil?
27
+ xml << %Q{<col min="#{index + 1}" max="#{index + 1}"/>}
28
+ else
29
+
30
+ # https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.column.aspx
31
+ #
32
+ # Truncate(
33
+ # [{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]
34
+ # /{Maximum Digit Width}*256
35
+ # )/256
36
+ #
37
+ # "Using the Calibri font as an example, the maximum digit width of
38
+ # 11 point font size is 7 pixels (at 96 dpi)"
39
+ #
40
+ # By observation, though, I note that a different spreadsheet-wide
41
+ # font size selected via the Workbook's ":font => { :size => ... }"
42
+ # options Hash entry results in Excel, at least, scaling the given
43
+ # widths in proportion with the requested font size change. We do
44
+ # not, apparently, need to do that ourselves and run the calculation
45
+ # based on the reference 11 point -> 7 pixel figure.
46
+ #
47
+ width_pixels ||= ((((width_chars * 7.0) + 5) / 7) * 256).truncate() / 256.0
48
+
49
+ xml << %Q{<col min="#{index + 1}" max="#{index + 1}" width="#{width_pixels}" customWidth="1"/>}
50
+ end
51
+ end
52
+
53
+ xml << '</cols>'
54
+ end
55
+
56
+ end
57
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
3
  class Error < StandardError; end
3
4
  class Deprecation < StandardError; end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "pathname"
2
3
 
3
4
  module Xlsxtream
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
3
  module IO
3
4
  class Hash
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "zip"
2
3
  require "xlsxtream/errors"
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
3
  module IO
3
4
  class Stream
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "zip_tricks"
2
3
 
3
4
  module Xlsxtream
@@ -8,7 +9,7 @@ module Xlsxtream
8
9
  def initialize(body)
9
10
  @streamer = ::ZipTricks::Streamer.new(body)
10
11
  @wf = nil
11
- @buffer = ''
12
+ @buffer = String.new
12
13
  end
13
14
 
14
15
  def <<(data)
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
  require "date"
3
3
  require "xlsxtream/xml"
4
4
 
@@ -27,8 +27,8 @@ module Xlsxtream
27
27
  end
28
28
 
29
29
  def to_xml
30
- column = 'A'
31
- xml = %Q{<row r="#{@rownum}">}
30
+ column = String.new('A')
31
+ xml = String.new(%Q{<row r="#{@rownum}">})
32
32
 
33
33
  @row.each do |value|
34
34
  cid = "#{column}#{@rownum}"
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
3
  class SharedStringTable < Hash
3
4
  def initialize
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
- VERSION = '2.0.1'.freeze
3
+ VERSION = '2.1.0'.freeze
3
4
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
  require "xlsxtream/errors"
3
3
  require "xlsxtream/xml"
4
4
  require "xlsxtream/shared_string_table"
@@ -65,13 +65,14 @@ module Xlsxtream
65
65
  end
66
66
  use_sst = options.fetch(:use_shared_strings, @options[:use_shared_strings])
67
67
  auto_format = options.fetch(:auto_format, @options[:auto_format])
68
+ columns = options.fetch(:columns, @options[:columns])
68
69
  sst = use_sst ? @sst : nil
69
70
 
70
- name ||= "Sheet#{@worksheets.size + 1}"
71
+ name = name || options[:name] || "Sheet#{@worksheets.size + 1}"
71
72
  sheet_id = @worksheets[name]
72
73
  @io.add_file "xl/worksheets/sheet#{sheet_id}.xml"
73
74
 
74
- worksheet = Worksheet.new(@io, :sst => sst, :auto_format => auto_format)
75
+ worksheet = Worksheet.new(@io, :sst => sst, :auto_format => auto_format, :columns => columns)
75
76
  yield worksheet if block_given?
76
77
  worksheet.close
77
78
 
@@ -104,7 +105,7 @@ module Xlsxtream
104
105
  end
105
106
 
106
107
  def write_workbook
107
- rid = "rId0"
108
+ rid = String.new("rId0")
108
109
  @io.add_file "xl/workbook.xml"
109
110
  @io << XML.header
110
111
  @io << XML.strip(<<-XML)
@@ -184,7 +185,7 @@ module Xlsxtream
184
185
  end
185
186
 
186
187
  def write_workbook_rels
187
- rid = "rId0"
188
+ rid = String.new("rId0")
188
189
  @io.add_file "xl/_rels/workbook.xml.rels"
189
190
  @io << XML.header
190
191
  @io << '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
  require "xlsxtream/xml"
3
3
  require "xlsxtream/row"
4
4
 
@@ -28,6 +28,14 @@ module Xlsxtream
28
28
  @io << XML.header
29
29
  @io << XML.strip(<<-XML)
30
30
  <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
31
+ XML
32
+
33
+ columns = Array(@options[:columns])
34
+ unless columns.empty?
35
+ @io << Columns.new(columns).to_xml
36
+ end
37
+
38
+ @io << XML.strip(<<-XML)
31
39
  <sheetData>
32
40
  XML
33
41
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Xlsxtream
2
3
  module XML
3
4
  XML_ESCAPES = {
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'xlsxtream/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlsxtream
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Bünemann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-11 00:00:00.000000000 Z
11
+ date: 2018-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zip_tricks
@@ -112,6 +112,7 @@ files:
112
112
  - bin/console
113
113
  - bin/setup
114
114
  - lib/xlsxtream.rb
115
+ - lib/xlsxtream/columns.rb
115
116
  - lib/xlsxtream/errors.rb
116
117
  - lib/xlsxtream/io/directory.rb
117
118
  - lib/xlsxtream/io/hash.rb
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
146
  version: '0'
146
147
  requirements: []
147
148
  rubyforge_project:
148
- rubygems_version: 2.7.6
149
+ rubygems_version: 2.7.7
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: Xlsxtream is a streaming XLSX spreadsheet writer