Skip to content

Commit b65e36b

Browse files
committed
add example/autofit.rb
1 parent 29c018a commit b65e36b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

examples/autofit.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: utf-8 -*-
3+
4+
#
5+
# An example of using simulated autofit to automatically adjust the width of
6+
# worksheet columns based on the data in the cells.
7+
#
8+
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
9+
#
10+
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
11+
#
12+
# convert to Ruby by Hideo NAKAMURA, nakamura.hideo@gmail.com
13+
#
14+
require 'write_xlsx'
15+
16+
workbook = WriteXLSX.new('autofit.xlsx')
17+
worksheet = workbook.add_worksheet
18+
19+
# Write some worksheet data to demonstrate autofitting.
20+
worksheet.write(0, 0, "Foo")
21+
worksheet.write(1, 0, "Food")
22+
worksheet.write(2, 0, "Foody")
23+
worksheet.write(3, 0, "Froody")
24+
25+
worksheet.write(0, 1, 12345)
26+
worksheet.write(1, 1, 12345678)
27+
worksheet.write(2, 1, 12345)
28+
29+
worksheet.write(0, 2, "Some longer text")
30+
31+
worksheet.write(0, 3, 'http://www.google.com')
32+
worksheet.write(1, 3, 'https://github.com')
33+
34+
# Autofit the worksheet
35+
worksheet.autofit
36+
37+
workbook.close

test/perl_output/autofit.xlsx

5.83 KB
Binary file not shown.

test/test_example_match.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,34 @@ def autofilter_data
343343
EOS
344344
end
345345

346+
def test_autofit
347+
@xlsx = 'autofit.xlsx'
348+
workbook = WriteXLSX.new(@io)
349+
worksheet = workbook.add_worksheet
350+
351+
# Write some worksheet data to demonstrate autofitting.
352+
worksheet.write(0, 0, "Foo")
353+
worksheet.write(1, 0, "Food")
354+
worksheet.write(2, 0, "Foody")
355+
worksheet.write(3, 0, "Froody")
356+
357+
worksheet.write(0, 1, 12345)
358+
worksheet.write(1, 1, 12345678)
359+
worksheet.write(2, 1, 12345)
360+
361+
worksheet.write(0, 2, "Some longer text")
362+
363+
worksheet.write(0, 3, 'http://www.google.com')
364+
worksheet.write(1, 3, 'https://github.com')
365+
366+
# Autofit the worksheet
367+
worksheet.autofit
368+
369+
workbook.close
370+
store_to_tempfile
371+
compare_xlsx(File.join(@perl_output, @xlsx), @tempfile.path)
372+
end
373+
346374
def test_background
347375
@xlsx = 'background.xlsx'
348376
workbook = WriteXLSX.new(@io)

0 commit comments

Comments
 (0)