write_xlsx 0.85.10 → 0.85.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46ee9d7976dffd23442ab3dc1c87eb79d11786b5c6c9747f805a018a22330698
4
- data.tar.gz: 43c1478b3d812cc7ba3d3d616b9fe9c14ef58b0e63c565d3d90378cdad6cdb92
3
+ metadata.gz: 89f382bd2df994369316a0422365541984f53ec084c5d3bb1c5d5a03523757a9
4
+ data.tar.gz: 363697c51b2250602fd5122aa3e9c7a6c71ec755a671e659f08f19c58b184792
5
5
  SHA512:
6
- metadata.gz: 626c6f160305d4a67487c10174d45daf37cd7e9eef9736f5d6adbf44219e0aa996f52dc07564b3124141cc94d3d286acf9631176b913cf91e733de8ef0fda16d
7
- data.tar.gz: a1d8170e68cbd7ee0f06a5b9ea00aa801bf990a6a06090ce0abe275bd84bd8c1201740e30735b887761a57e0d28357431e6a3d5a5e17a995b6fe2d5ff2c7a43a
6
+ metadata.gz: c719aa61decdc25064a72704d015d8a1eea48b3a94ccb7ccaf01e99456a6521c5c4fc5a5ee738a721c1e06c4632c737cf47d14afd3830a164162a789acc6e452
7
+ data.tar.gz: 96f55161f36511ef22708a377ea647f21f4ae76c978039b0c85c32c5db55ac0330f3e4c7a0e1f030d60c3d00c0c96f6c3f045bbfd240a2e2031c8ced2b2c2945
data/Changes CHANGED
@@ -1,5 +1,8 @@
1
1
  Change history of write_xlsx rubygem.
2
2
 
3
+ 2020-10-23 v0.85.11
4
+ Added Worksheet#update_range_format_with_params
5
+
3
6
  2020-10-23 v0.85.10
4
7
  Fix frozen string litterals problems in Format#set_align
5
8
 
data/README.md CHANGED
@@ -84,7 +84,7 @@ the first worksheet in an Excel XML spreadsheet called ruby.xlsx:
84
84
  ## Copyright
85
85
  Original Perl module was written by John McNamara(jmcnamara@cpan.org).
86
86
 
87
- Converted to ruby by Hideo NAKAMURA(cxn03651@msj.biglobe.ne.jp)
87
+ Converted to ruby by Hideo NAKAMURA(nakamrua.hideo@gmail.com)
88
88
  Copyright (c) 2012-2018 Hideo NAKAMURA.
89
89
 
90
90
  See LICENSE.txt for further details.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #######################################################################
5
+ #
6
+ # A simple example of how to use the write_xlsx gem to
7
+ # update format of the range of cells.
8
+ #
9
+
10
+ require 'write_xlsx'
11
+
12
+ workbook = WriteXLSX.new('update_range_format_with_params.xlsx')
13
+ worksheet = workbook.add_worksheet
14
+
15
+ common_format = workbook.add_format(align: 'center', border: 1)
16
+
17
+ table_contents = [
18
+ ['Table', 'Header', 'Contents'],
19
+ ['table', 'body', 'contents'],
20
+ ['table', 'body', 'contents'],
21
+ ['table', 'body', 'contents']
22
+ ]
23
+
24
+ worksheet.write_col(0, 0, table_contents, common_format)
25
+ worksheet.update_range_format_with_params(
26
+ 0, 0, 0, 2,
27
+ bold: 1, top: 2, bottom: 2, bg_color: 31
28
+ )
29
+ worksheet.update_range_format_with_params(0, 0, 3, 0, left: 2)
30
+ worksheet.update_range_format_with_params(0, 2, 3, 2, right: 2)
31
+ worksheet.update_range_format_with_params(3, 0, 3, 2, bottom: 2)
32
+
33
+ workbook.close
@@ -1 +1 @@
1
- WriteXLSX_VERSION = "0.85.10"
1
+ WriteXLSX_VERSION = "0.85.11"
@@ -2528,6 +2528,91 @@ def write_array_formula(*args)
2528
2528
  end
2529
2529
  end
2530
2530
 
2531
+ #
2532
+ # :call-seq:
2533
+ # update_format_with_params(row, col, format_params)
2534
+ #
2535
+ # Update formatting of the cell to the specified row and column (zero indexed).
2536
+ #
2537
+ # worksheet.update_format_with_params(0, 0, color: 'red')
2538
+ #
2539
+ # This method is used to update formatting of the cell keeping cell contents
2540
+ # and formatting.
2541
+ #
2542
+ # If the cell doesn't have CellData object, this method create a CellData
2543
+ # using write_blank method.
2544
+ # If the cell has CellData, this method fetch contents and format of cell from
2545
+ # the CellData object and recreate CellData using write method.
2546
+ #
2547
+ def update_format_with_params(*args)
2548
+ row, col, params = row_col_notation(args)
2549
+ raise WriteXLSXInsufficientArgumentError if row.nil? || col.nil? || params.nil?
2550
+
2551
+ # Check that row and col are valid and store max and min values
2552
+ check_dimensions(row, col)
2553
+ store_row_col_max_min_values(row, col)
2554
+
2555
+ format = nil
2556
+ cell_data = nil
2557
+ if @cell_data_table[row].nil? || @cell_data_table[row][col].nil?
2558
+ format = @workbook.add_format(params)
2559
+ write_blank(row, col, format)
2560
+ else
2561
+ if @cell_data_table[row][col].xf.nil?
2562
+ format = @workbook.add_format(params)
2563
+ cell_data = @cell_data_table[row][col]
2564
+ else
2565
+ format = @workbook.add_format
2566
+ cell_data = @cell_data_table[row][col]
2567
+ format.copy(cell_data.xf)
2568
+ format.set_format_properties(params)
2569
+ end
2570
+ # keep original value of cell
2571
+ if cell_data.is_a? FormulaCellData
2572
+ value = "=#{cell_data.token}"
2573
+ elsif cell_data.is_a? FormulaArrayCellData
2574
+ value = "{=#{cell_data.token}}"
2575
+ elsif cell_data.is_a? StringCellData
2576
+ value = @workbook.shared_strings.string(cell_data.data[:sst_id])
2577
+ else
2578
+ value = cell_data.data
2579
+ end
2580
+ write(row, col, value, format)
2581
+ end
2582
+ end
2583
+
2584
+ #
2585
+ # :call-seq:
2586
+ # update_range_format_with_params(row_first, col_first, row_last, col_last, format_params)
2587
+ #
2588
+ # Update formatting of cells in range to the specified row and column (zero indexed).
2589
+ #
2590
+ # worksheet.update_range_format_with_params(0, 0, 3, 3, color: 'red')
2591
+ #
2592
+ # This method is used to update formatting of multiple cells keeping cells' contents
2593
+ # and formatting.
2594
+ #
2595
+ #
2596
+ def update_range_format_with_params(*args)
2597
+ row_first, col_first, row_last, col_last, params = row_col_notation(args)
2598
+
2599
+ raise WriteXLSXInsufficientArgumentError if [row_first, col_first, row_last, col_last, params].include?(nil)
2600
+
2601
+ # Swap last row/col with first row/col as necessary
2602
+ row_first, row_last = row_last, row_first if row_first > row_last
2603
+ col_first, col_last = col_last, col_first if col_first > col_last
2604
+
2605
+ # Check that column number is valid and store the max value
2606
+ check_dimensions(row_last, col_last)
2607
+ store_row_col_max_min_values(row_last, col_last)
2608
+
2609
+ (row_first..row_last).each do |row|
2610
+ (col_first..col_last).each do |col|
2611
+ update_format_with_params(row, col, params)
2612
+ end
2613
+ end
2614
+ end
2615
+
2531
2616
  #
2532
2617
  # The outline_settings() method is used to control the appearance of
2533
2618
  # outlines in Excel. Outlines are described in
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestUpdateRangeFormatWithParams < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ @tempfile.close(true)
11
+ end
12
+
13
+ def test_update_range_format_with_params
14
+ @xlsx = 'update_range_format_with_params.xlsx'
15
+ workbook = WriteXLSX.new(@io)
16
+ worksheet = workbook.add_worksheet
17
+
18
+ common_format = workbook.add_format(align: 'center', border: 1)
19
+
20
+ table_contents = [
21
+ ['Table', 'Header', 'Contents'],
22
+ ['table', 'body', 'contents'],
23
+ ['table', 'body', 'contents'],
24
+ ['table', 'body', 'contents']
25
+ ]
26
+
27
+ worksheet.write_col(0, 0, table_contents, common_format)
28
+ worksheet.update_range_format_with_params(
29
+ 0, 0, 0, 2,
30
+ bold: 1, top: 2, bottom: 2, bg_color: 31
31
+ )
32
+ worksheet.update_range_format_with_params(0, 0, 3, 0, left: 2)
33
+ worksheet.update_range_format_with_params(0, 2, 3, 2, right: 2)
34
+ worksheet.update_range_format_with_params(3, 0, 3, 2, bottom: 2)
35
+
36
+ workbook.close
37
+ compare_for_regression(
38
+ nil,
39
+ {'xl/workbook.xml' => ['<workbookView']}
40
+ )
41
+ end
42
+ end
@@ -0,0 +1,111 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'write_xlsx'
4
+ require 'stringio'
5
+
6
+ class TestUpdateFormatMethods < Test::Unit::TestCase
7
+ def setup
8
+ @workbook = WriteXLSX.new(StringIO.new)
9
+ @worksheet = @workbook.add_worksheet('')
10
+ end
11
+
12
+ def test_update_format_with_params_with_insufficient_args_raise_InsufficientArgumentError
13
+ assert_raise(WriteXLSXInsufficientArgumentError) do
14
+ @worksheet.update_format_with_params
15
+ end
16
+ assert_raise(WriteXLSXInsufficientArgumentError) do
17
+ @worksheet.update_format_with_params(0)
18
+ end
19
+ assert_raise(WriteXLSXInsufficientArgumentError) do
20
+ @worksheet.update_format_with_params('A1')
21
+ end
22
+ end
23
+
24
+ def test_update_format_with_params_with_valid_arg_not_raise
25
+ assert_nothing_raised do
26
+ @worksheet.update_format_with_params(0, 0, color: 'red', border: 2)
27
+ end
28
+ assert_nothing_raised do
29
+ @worksheet.update_format_with_params('B2', align: 'center')
30
+ end
31
+ end
32
+
33
+ def test_update_format_with_params_should_write_blank_when_there_is_no_CellData
34
+ assert_nil(@worksheet.instance_variable_get(:@cell_data_table)[0])
35
+ @worksheet.update_format_with_params(0, 0, left: 4)
36
+ assert @worksheet.instance_variable_get(:@cell_data_table)[0][0] != nil
37
+ end
38
+
39
+ def test_update_format_with_params_should_keep_data_when_updating_format
40
+ number = 153
41
+ @worksheet.write(0, 0, number)
42
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
43
+ assert_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][0].data, number)
44
+
45
+ string = 'Hello, World!'
46
+ @worksheet.write(0, 0, string)
47
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
48
+ written_string = @workbook.shared_strings.string(@worksheet.instance_variable_get(:@cell_data_table)[0][0].data[:sst_id])
49
+ assert_equal(written_string, string)
50
+
51
+ formula = '=1+1'
52
+ @worksheet.write(0, 0, formula)
53
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
54
+ assert_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][0].token, '1+1')
55
+
56
+ array_formula = '{=SUM(B1:C1*B2:C2)}'
57
+ @worksheet.write('A1', array_formula)
58
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
59
+ assert_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][0].token, 'SUM(B1:C1*B2:C2)')
60
+
61
+ url = 'https://www.writexlsx.io'
62
+ @worksheet.write(0, 0, url)
63
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
64
+ written_string = @workbook.shared_strings.string(@worksheet.instance_variable_get(:@cell_data_table)[0][0].data[:sst_id])
65
+ assert_equal(written_string, url)
66
+
67
+ string = 'Hello, World!'
68
+ format = @workbook.add_format(color: 'white')
69
+ @worksheet.write(0, 0, string, format)
70
+ @worksheet.update_format_with_params(0, 0, bg_color: 'gray')
71
+ written_string = @workbook.shared_strings.string(@worksheet.instance_variable_get(:@cell_data_table)[0][0].data[:sst_id])
72
+ assert_equal(written_string, string)
73
+ end
74
+
75
+ def test_update_format_with_params_should_not_update_other_cells_format
76
+ format = @workbook.add_format(bold: 1)
77
+ @worksheet.write_row(0, 0, ['', '', '', '', ''], format)
78
+ @worksheet.update_format_with_params(0, 0, bold: 0)
79
+ assert_not_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][0].xf,
80
+ @worksheet.instance_variable_get(:@cell_data_table)[0][1].xf )
81
+ assert_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][0].xf.bold, 0)
82
+ assert_equal(@worksheet.instance_variable_get(:@cell_data_table)[0][1].xf.bold, 1)
83
+ end
84
+
85
+ def test_update_range_format_with_params_with_insufficient_args_raise_InsufficientArgumentError
86
+ assert_raise(WriteXLSXInsufficientArgumentError) do
87
+ @worksheet.update_range_format_with_params
88
+ end
89
+ assert_raise(WriteXLSXInsufficientArgumentError) do
90
+ @worksheet.update_range_format_with_params(0, 0)
91
+ end
92
+ assert_raise(WriteXLSXInsufficientArgumentError) do
93
+ @worksheet.update_range_format_with_params('A1')
94
+ end
95
+ assert_raise(WriteXLSXInsufficientArgumentError) do
96
+ @worksheet.update_range_format_with_params(0, 0, 3, 3)
97
+ end
98
+ assert_raise(WriteXLSXInsufficientArgumentError) do
99
+ @worksheet.update_range_format_with_params('A2:C2')
100
+ end
101
+ end
102
+
103
+ def test_update_range_format_with_params_with_valid_arg_not_raise
104
+ assert_nothing_raised do
105
+ @worksheet.update_range_format_with_params(0, 0, 3, 3, bold: 1)
106
+ end
107
+ assert_nothing_raised do
108
+ @worksheet.update_range_format_with_params('A2:D5', bold: 1)
109
+ end
110
+ end
111
+ end
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.name = "write_xlsx"
8
8
  gem.version = WriteXLSX_VERSION
9
9
  gem.authors = ["Hideo NAKAMURA"]
10
- gem.email = ["cxn03651@msj.biglobe.ne.jp"]
10
+ gem.email = ["nakamura.hideo@gmail.com"]
11
11
  gem.description = "write_xlsx is a gem to create a new file in the Excel 2007+ XLSX format."
12
12
  gem.summary = "write_xlsx is a gem to create a new file in the Excel 2007+ XLSX format."
13
13
  gem.homepage = "http://github.com/cxn03651/write_xlsx#readme"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: write_xlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.85.10
4
+ version: 0.85.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hideo NAKAMURA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-23 00:00:00.000000000 Z
11
+ date: 2020-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -68,7 +68,7 @@ dependencies:
68
68
  version: '0'
69
69
  description: write_xlsx is a gem to create a new file in the Excel 2007+ XLSX format.
70
70
  email:
71
- - cxn03651@msj.biglobe.ne.jp
71
+ - nakamura.hideo@gmail.com
72
72
  executables:
73
73
  - extract_vba.rb
74
74
  extensions: []
@@ -154,6 +154,7 @@ files:
154
154
  - examples/stocks.rb
155
155
  - examples/tab_colors.rb
156
156
  - examples/tables.rb
157
+ - examples/update_range_format_with_params.rb
157
158
  - examples/vbaProject.bin
158
159
  - lib/write_xlsx.rb
159
160
  - lib/write_xlsx/chart.rb
@@ -978,6 +979,7 @@ files:
978
979
  - test/regression/test_tutorial01.rb
979
980
  - test/regression/test_tutorial02.rb
980
981
  - test/regression/test_tutorial03.rb
982
+ - test/regression/test_update_range_format_with_params.rb
981
983
  - test/regression/test_urls_as_strings.rb
982
984
  - test/regression/test_utf8_01.rb
983
985
  - test/regression/test_utf8_03.rb
@@ -1561,6 +1563,7 @@ files:
1561
1563
  - test/regression/xlsx_files/tutorial01.xlsx
1562
1564
  - test/regression/xlsx_files/tutorial02.xlsx
1563
1565
  - test/regression/xlsx_files/tutorial03.xlsx
1566
+ - test/regression/xlsx_files/update_range_format_with_params.xlsx
1564
1567
  - test/regression/xlsx_files/urls_as_strings.xlsx
1565
1568
  - test/regression/xlsx_files/utf8_01.xlsx
1566
1569
  - test/regression/xlsx_files/utf8_03.xlsx
@@ -1636,6 +1639,7 @@ files:
1636
1639
  - test/worksheet/test_sparkline_09.rb
1637
1640
  - test/worksheet/test_sparkline_10.rb
1638
1641
  - test/worksheet/test_sparkline_11.rb
1642
+ - test/worksheet/test_update_format_methods.rb
1639
1643
  - test/worksheet/test_worksheet_01.rb
1640
1644
  - test/worksheet/test_worksheet_02.rb
1641
1645
  - test/worksheet/test_worksheet_03.rb
@@ -2491,6 +2495,7 @@ test_files:
2491
2495
  - test/regression/test_tutorial01.rb
2492
2496
  - test/regression/test_tutorial02.rb
2493
2497
  - test/regression/test_tutorial03.rb
2498
+ - test/regression/test_update_range_format_with_params.rb
2494
2499
  - test/regression/test_urls_as_strings.rb
2495
2500
  - test/regression/test_utf8_01.rb
2496
2501
  - test/regression/test_utf8_03.rb
@@ -3074,6 +3079,7 @@ test_files:
3074
3079
  - test/regression/xlsx_files/tutorial01.xlsx
3075
3080
  - test/regression/xlsx_files/tutorial02.xlsx
3076
3081
  - test/regression/xlsx_files/tutorial03.xlsx
3082
+ - test/regression/xlsx_files/update_range_format_with_params.xlsx
3077
3083
  - test/regression/xlsx_files/urls_as_strings.xlsx
3078
3084
  - test/regression/xlsx_files/utf8_01.xlsx
3079
3085
  - test/regression/xlsx_files/utf8_03.xlsx
@@ -3149,6 +3155,7 @@ test_files:
3149
3155
  - test/worksheet/test_sparkline_09.rb
3150
3156
  - test/worksheet/test_sparkline_10.rb
3151
3157
  - test/worksheet/test_sparkline_11.rb
3158
+ - test/worksheet/test_update_format_methods.rb
3152
3159
  - test/worksheet/test_worksheet_01.rb
3153
3160
  - test/worksheet/test_worksheet_02.rb
3154
3161
  - test/worksheet/test_worksheet_03.rb