write_xlsx 0.54.0 → 0.55.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
Binary file
@@ -4412,4 +4412,429 @@ def test_tables
4412
4412
  workbook.close
4413
4413
  compare_xlsx(File.join(@perl_output, @xlsx), @xlsx)
4414
4414
  end
4415
+
4416
+ def test_sparklines1
4417
+ @xlsx = 'sparklines1.xlsx'
4418
+ workbook = WriteXLSX.new(@xlsx)
4419
+ worksheet = workbook.add_worksheet
4420
+
4421
+ # Some sample data to plot.
4422
+ data = [
4423
+ [ -2, 2, 3, -1, 0 ],
4424
+ [ 30, 20, 33, 20, 15 ],
4425
+ [ 1, -1, -1, 1, -1 ]
4426
+ ]
4427
+
4428
+ # Write the sample data to the worksheet.
4429
+ worksheet.write_col('A1', data)
4430
+
4431
+ # Add a line sparkline (the default) with markers.
4432
+ worksheet.add_sparkline(
4433
+ {
4434
+ :location => 'F1',
4435
+ :range => 'Sheet1!A1:E1',
4436
+ :markers => 1
4437
+ }
4438
+ )
4439
+
4440
+ # Add a column sparkline with non-default style.
4441
+ worksheet.add_sparkline(
4442
+ {
4443
+ :location => 'F2',
4444
+ :range => 'Sheet1!A2:E2',
4445
+ :type => 'column',
4446
+ :style => 12
4447
+ }
4448
+ )
4449
+
4450
+ # Add a win/loss sparkline with negative values highlighted.
4451
+ worksheet.add_sparkline(
4452
+ {
4453
+ :location => 'F3',
4454
+ :range => 'Sheet1!A3:E3',
4455
+ :type => 'win_loss',
4456
+ :negative_points => 1
4457
+ }
4458
+ )
4459
+
4460
+ workbook.close
4461
+ compare_xlsx(File.join(@perl_output, @xlsx), @xlsx)
4462
+ end
4463
+
4464
+ def test_sparklines2
4465
+ @xlsx = 'sparklines2.xlsx'
4466
+ workbook = WriteXLSX.new(@xlsx)
4467
+ worksheet1 = workbook.add_worksheet
4468
+ worksheet2 = workbook.add_worksheet
4469
+ bold = workbook.add_format(:bold => 1)
4470
+ row = 1
4471
+
4472
+ # Set the columns widths to make the output clearer.
4473
+ worksheet1.set_column('A:A', 14)
4474
+ worksheet1.set_column('B:B', 50)
4475
+ worksheet1.set_zoom(150)
4476
+
4477
+ # Headings.
4478
+ worksheet1.write('A1', 'Sparkline', bold)
4479
+ worksheet1.write('B1', 'Description', bold)
4480
+
4481
+ ##########################################################################
4482
+ #
4483
+ str = 'A default "line" sparkline.'
4484
+
4485
+ worksheet1.add_sparkline(
4486
+ {
4487
+ :location => 'A2',
4488
+ :range => 'Sheet2!A1:J1'
4489
+ }
4490
+ )
4491
+
4492
+ worksheet1.write(row, 1, str)
4493
+ row += 1
4494
+
4495
+ ##########################################################################
4496
+ #
4497
+ str = 'A default "column" sparkline.'
4498
+
4499
+ worksheet1.add_sparkline(
4500
+ {
4501
+ :location => 'A3',
4502
+ :range => 'Sheet2!A2:J2',
4503
+ :type => 'column'
4504
+ }
4505
+ )
4506
+
4507
+ worksheet1.write(row, 1, str)
4508
+ row += 1
4509
+
4510
+ ##########################################################################
4511
+ #
4512
+ str = 'A default "win/loss" sparkline.'
4513
+
4514
+ worksheet1.add_sparkline(
4515
+ {
4516
+ :location => 'A4',
4517
+ :range => 'Sheet2!A3:J3',
4518
+ :type => 'win_loss'
4519
+ }
4520
+ )
4521
+
4522
+ worksheet1.write(row, 1, str)
4523
+ row += 2
4524
+
4525
+ ##########################################################################
4526
+ #
4527
+ str = 'Line with markers.'
4528
+
4529
+ worksheet1.add_sparkline(
4530
+ {
4531
+ :location => 'A6',
4532
+ :range => 'Sheet2!A1:J1',
4533
+ :markers => 1
4534
+ }
4535
+ )
4536
+
4537
+ worksheet1.write(row, 1, str)
4538
+ row += 1
4539
+
4540
+ ##########################################################################
4541
+ #
4542
+ str = 'Line with high and low points.'
4543
+
4544
+ worksheet1.add_sparkline(
4545
+ {
4546
+ :location => 'A7',
4547
+ :range => 'Sheet2!A1:J1',
4548
+ :high_point => 1,
4549
+ :low_point => 1
4550
+ }
4551
+ )
4552
+
4553
+ worksheet1.write(row, 1, str)
4554
+ row += 1
4555
+
4556
+ ##########################################################################
4557
+ #
4558
+ str = 'Line with first and last point markers.'
4559
+
4560
+ worksheet1.add_sparkline(
4561
+ {
4562
+ :location => 'A8',
4563
+ :range => 'Sheet2!A1:J1',
4564
+ :first_point => 1,
4565
+ :last_point => 1
4566
+ }
4567
+ )
4568
+
4569
+ worksheet1.write(row, 1, str)
4570
+ row += 1
4571
+
4572
+ ##########################################################################
4573
+ #
4574
+ str = 'Line with negative point markers.'
4575
+
4576
+ worksheet1.add_sparkline(
4577
+ {
4578
+ :location => 'A9',
4579
+ :range => 'Sheet2!A1:J1',
4580
+ :negative_points => 1
4581
+ }
4582
+ )
4583
+
4584
+ worksheet1.write(row, 1, str)
4585
+ row += 1
4586
+
4587
+ ##########################################################################
4588
+ #
4589
+ str = 'Line with axis.'
4590
+
4591
+ worksheet1.add_sparkline(
4592
+ {
4593
+ :location => 'A10',
4594
+ :range => 'Sheet2!A1:J1',
4595
+ :axis => 1
4596
+ }
4597
+ )
4598
+
4599
+ worksheet1.write(row, 1, str)
4600
+ row += 2
4601
+
4602
+ ##########################################################################
4603
+ #
4604
+ str = 'Column with default style (1).'
4605
+
4606
+ worksheet1.add_sparkline(
4607
+ {
4608
+ :location => 'A12',
4609
+ :range => 'Sheet2!A2:J2',
4610
+ :type => 'column'
4611
+ }
4612
+ )
4613
+
4614
+ worksheet1.write(row, 1, str)
4615
+ row += 1
4616
+
4617
+ ##########################################################################
4618
+ #
4619
+ str = 'Column with style 2.'
4620
+
4621
+ worksheet1.add_sparkline(
4622
+ {
4623
+ :location => 'A13',
4624
+ :range => 'Sheet2!A2:J2',
4625
+ :type => 'column',
4626
+ :style => 2
4627
+ }
4628
+ )
4629
+
4630
+ worksheet1.write(row, 1, str)
4631
+ row += 1
4632
+
4633
+ ##########################################################################
4634
+ #
4635
+ str = 'Column with style 3.'
4636
+
4637
+ worksheet1.add_sparkline(
4638
+ {
4639
+ :location => 'A14',
4640
+ :range => 'Sheet2!A2:J2',
4641
+ :type => 'column',
4642
+ :style => 3
4643
+ }
4644
+ )
4645
+
4646
+ worksheet1.write(row, 1, str)
4647
+ row += 1
4648
+
4649
+ ##########################################################################
4650
+ #
4651
+ str = 'Column with style 4.'
4652
+
4653
+ worksheet1.add_sparkline(
4654
+ {
4655
+ :location => 'A15',
4656
+ :range => 'Sheet2!A2:J2',
4657
+ :type => 'column',
4658
+ :style => 4
4659
+ }
4660
+ )
4661
+
4662
+ worksheet1.write(row, 1, str)
4663
+ row += 1
4664
+
4665
+ ##########################################################################
4666
+ #
4667
+ str = 'Column with style 5.'
4668
+
4669
+ worksheet1.add_sparkline(
4670
+ {
4671
+ :location => 'A16',
4672
+ :range => 'Sheet2!A2:J2',
4673
+ :type => 'column',
4674
+ :style => 5
4675
+ }
4676
+ )
4677
+
4678
+ worksheet1.write(row, 1, str)
4679
+ row += 1
4680
+
4681
+ ##########################################################################
4682
+ #
4683
+ str = 'Column with style 6.'
4684
+
4685
+ worksheet1.add_sparkline(
4686
+ {
4687
+ :location => 'A17',
4688
+ :range => 'Sheet2!A2:J2',
4689
+ :type => 'column',
4690
+ :style => 6
4691
+ }
4692
+ )
4693
+
4694
+ worksheet1.write(row, 1, str)
4695
+ row += 1
4696
+
4697
+ ##########################################################################
4698
+ #
4699
+ str = 'Column with a user defined colour.'
4700
+
4701
+ worksheet1.add_sparkline(
4702
+ {
4703
+ :location => 'A18',
4704
+ :range => 'Sheet2!A2:J2',
4705
+ :type => 'column',
4706
+ :series_color => '#E965E0'
4707
+ }
4708
+ )
4709
+
4710
+ worksheet1.write(row, 1, str)
4711
+ row += 2
4712
+
4713
+ ##########################################################################
4714
+ #
4715
+ str = 'A win/loss sparkline.'
4716
+
4717
+ worksheet1.add_sparkline(
4718
+ {
4719
+ :location => 'A20',
4720
+ :range => 'Sheet2!A3:J3',
4721
+ :type => 'win_loss'
4722
+ }
4723
+ )
4724
+
4725
+ worksheet1.write(row, 1, str)
4726
+ row += 1
4727
+
4728
+ ##########################################################################
4729
+ #
4730
+ str = 'A win/loss sparkline with negative points highlighted.'
4731
+
4732
+ worksheet1.add_sparkline(
4733
+ {
4734
+ :location => 'A21',
4735
+ :range => 'Sheet2!A3:J3',
4736
+ :type => 'win_loss',
4737
+ :negative_points => 1
4738
+ }
4739
+ )
4740
+
4741
+ worksheet1.write(row, 1, str)
4742
+ row += 2
4743
+
4744
+ ##########################################################################
4745
+ #
4746
+ str = 'A left to right column (the default).'
4747
+
4748
+ worksheet1.add_sparkline(
4749
+ {
4750
+ :location => 'A23',
4751
+ :range => 'Sheet2!A4:J4',
4752
+ :type => 'column',
4753
+ :style => 20
4754
+ }
4755
+ )
4756
+
4757
+ worksheet1.write(row, 1, str)
4758
+ row += 1
4759
+
4760
+ ##########################################################################
4761
+ #
4762
+ str = 'A right to left column.'
4763
+
4764
+ worksheet1.add_sparkline(
4765
+ {
4766
+ :location => 'A24',
4767
+ :range => 'Sheet2!A4:J4',
4768
+ :type => 'column',
4769
+ :style => 20,
4770
+ :reverse => 1
4771
+ }
4772
+ )
4773
+
4774
+ worksheet1.write(row, 1, str)
4775
+ row += 1
4776
+
4777
+ ##########################################################################
4778
+ #
4779
+ str = 'Sparkline and text in one cell.'
4780
+
4781
+ worksheet1.add_sparkline(
4782
+ {
4783
+ :location => 'A25',
4784
+ :range => 'Sheet2!A4:J4',
4785
+ :type => 'column',
4786
+ :style => 20
4787
+ }
4788
+ )
4789
+
4790
+ worksheet1.write(row, 0, 'Growth')
4791
+ worksheet1.write(row, 1, str)
4792
+ row += 2
4793
+
4794
+ ##########################################################################
4795
+ #
4796
+ str = 'A grouped sparkline. Changes are applied to all three.'
4797
+
4798
+ worksheet1.add_sparkline(
4799
+ {
4800
+ :location => [ 'A27', 'A28', 'A29' ],
4801
+ :range => [ 'Sheet2!A5:J5', 'Sheet2!A6:J6', 'Sheet2!A7:J7' ],
4802
+ :markers => 1
4803
+ }
4804
+ )
4805
+
4806
+ worksheet1.write(row, 1, str)
4807
+ row += 1
4808
+
4809
+ ##########################################################################
4810
+ # Create a second worksheet with data to plot.
4811
+ #
4812
+
4813
+ worksheet2.set_column('A:J', 11)
4814
+
4815
+ data = [
4816
+ # Simple line data.
4817
+ [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
4818
+
4819
+ # Simple column data.
4820
+ [ 30, 20, 33, 20, 15, 5, 5, 15, 10, 15 ],
4821
+
4822
+ # Simple win/loss data.
4823
+ [ 1, 1, -1, -1, 1, -1, 1, 1, 1, -1 ],
4824
+
4825
+ # Unbalanced histogram.
4826
+ [ 5, 6, 7, 10, 15, 20, 30, 50, 70, 100 ],
4827
+
4828
+ # Data for the grouped sparkline example.
4829
+ [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
4830
+ [ 3, -1, 0, -2, 3, 2, 1, 0, 2, 1 ],
4831
+ [ 0, -2, 3, 2, 1, 0, 1, 2, 3, 1 ]
4832
+ ]
4833
+
4834
+ # Write the sample data to the worksheet.
4835
+ worksheet2.write_col('A1', data)
4836
+
4837
+ workbook.close
4838
+ compare_xlsx(File.join(@perl_output, @xlsx), @xlsx)
4839
+ end
4415
4840
  end
@@ -0,0 +1,119 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'write_xlsx'
4
+ require 'stringio'
5
+
6
+ class TestCondFormat20 < Test::Unit::TestCase
7
+ def setup
8
+ @workbook = WriteXLSX.new(StringIO.new)
9
+ @worksheet = @workbook.add_worksheet('')
10
+ end
11
+
12
+ ###############################################################################
13
+ #
14
+ # Test the _assemble_xml_file() method.
15
+ #
16
+ # Test conditional formats.
17
+ #
18
+ def test_conditional_formats
19
+ @worksheet.select
20
+
21
+ # Start test code.
22
+ @worksheet.write('A1', 10)
23
+ @worksheet.write('A2', 20)
24
+ @worksheet.write('A3', 30)
25
+ @worksheet.write('A4', 40)
26
+
27
+ @worksheet.conditional_formatting('A1:A4',
28
+ {
29
+ :type => 'text',
30
+ :criteria => 'begins with',
31
+ :value => 'b',
32
+ :format => nil
33
+ }
34
+ )
35
+
36
+ @worksheet.conditional_formatting('A1:A4',
37
+ {
38
+ :type => 'text',
39
+ :criteria => 'begins with',
40
+ :value => 'bc',
41
+ :format => nil
42
+ }
43
+ )
44
+
45
+ @worksheet.conditional_formatting('A1:A4',
46
+ {
47
+ :type => 'text',
48
+ :criteria => 'ends with',
49
+ :value => 'z',
50
+ :format => nil
51
+ }
52
+ )
53
+
54
+ @worksheet.conditional_formatting('A1:A4',
55
+ {
56
+ :type => 'text',
57
+ :criteria => 'ends with',
58
+ :value => 'yz',
59
+ :format => nil
60
+ }
61
+ )
62
+
63
+ @worksheet.assemble_xml_file
64
+ result = got_to_array(@worksheet.instance_variable_get(:@writer).string)
65
+
66
+ expected = expected_to_array(expected_xml)
67
+ assert_equal(expected, result)
68
+ end
69
+
70
+ def expected_xml
71
+ <<EOS
72
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
73
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
74
+ <dimension ref="A1:A4"/>
75
+ <sheetViews>
76
+ <sheetView tabSelected="1" workbookViewId="0"/>
77
+ </sheetViews>
78
+ <sheetFormatPr defaultRowHeight="15"/>
79
+ <sheetData>
80
+ <row r="1" spans="1:1">
81
+ <c r="A1">
82
+ <v>10</v>
83
+ </c>
84
+ </row>
85
+ <row r="2" spans="1:1">
86
+ <c r="A2">
87
+ <v>20</v>
88
+ </c>
89
+ </row>
90
+ <row r="3" spans="1:1">
91
+ <c r="A3">
92
+ <v>30</v>
93
+ </c>
94
+ </row>
95
+ <row r="4" spans="1:1">
96
+ <c r="A4">
97
+ <v>40</v>
98
+ </c>
99
+ </row>
100
+ </sheetData>
101
+ <conditionalFormatting sqref="A1:A4">
102
+ <cfRule type="beginsWith" priority="1" operator="beginsWith" text="b">
103
+ <formula>LEFT(A1,1)="b"</formula>
104
+ </cfRule>
105
+ <cfRule type="beginsWith" priority="2" operator="beginsWith" text="bc">
106
+ <formula>LEFT(A1,2)="bc"</formula>
107
+ </cfRule>
108
+ <cfRule type="endsWith" priority="3" operator="endsWith" text="z">
109
+ <formula>RIGHT(A1,1)="z"</formula>
110
+ </cfRule>
111
+ <cfRule type="endsWith" priority="4" operator="endsWith" text="yz">
112
+ <formula>RIGHT(A1,2)="yz"</formula>
113
+ </cfRule>
114
+ </conditionalFormatting>
115
+ <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
116
+ </worksheet>
117
+ EOS
118
+ end
119
+ end