Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bump version number to 0.0.4. Add autoformats for Dates, Floats and o…

…ther numerics with obvious autoformat.
  • Loading branch information...
commit 8987671871a6e393e63410dffcfd39447edc5ecc 1 parent 0c724f7
@ananelson ananelson authored
View
9 Rakefile
@@ -33,3 +33,12 @@ PROJ.exclude = %w{.bzr webby}
PROJ.rdoc.format = 'darkfish'
+
+desc "Run all examples (except the big ones) in examples dir."
+task :examples do
+ `ls examples/*.rb`.chomp.split("\n").each_with_index do |f, i|
+ next if f =~ /big/
+ puts "processing #{f}..."
+ `ruby #{f}`
+ end
+end
View
2  examples/blanks.rb
@@ -15,7 +15,7 @@
ws0.write(1, 1, 'Test', style0)
-0.upto(0x53) do |i|
+0.upto(13) do |i|
borders = Borders.new
borders.left = i
borders.right = i
View
2  examples/format.rb
@@ -8,7 +8,7 @@
ws0.write(1, 1, 'Test', style0)
-(0...0x53).each do |i|
+(0...14).each do |i|
style = StyleFormat.new(:font_name => 'Arial', :font_color_index => i, :font_outline => true)
borders = Borders.new
View
2  examples/set_cell_and_range_style.rb
@@ -4,7 +4,7 @@
book = Workbook.new
s = book.add_sheet
-s.write_arrays([%w{a b c d e}, %w{F G H I J}, %w{k l m n o}])
+s.write_arrays(0, 0, [%w{a b c d e}, %w{F G H I J}, %w{k l m n o}])
s.set_cell_style(2, 2, StyleFormat.new(:font_color => "yellow"))
s.set_range_style(0..1, 1..2, StyleFormat.new(:font_color => "purple"))
View
11 lib/formatting.rb
@@ -218,6 +218,7 @@ def colour=(colour_name)
end
end
alias :color= :colour=
+ alias :color_index= :colour_index=
def to_biff
options = PLAIN
@@ -405,6 +406,10 @@ def self.line_type_directives
LINE_TYPE_DIRECTIVES.collect {|k, v| k}
end
+ def self.line_type_constants
+ LINE_TYPE_DIRECTIVES.collect {|k, v| v}
+ end
+
def initialize(hash = {})
@left = NO_LINE
@right = NO_LINE
@@ -441,6 +446,7 @@ def process_directives(directives)
end
raise "no directives given to process_directives" if args.empty? # maybe don't need this, just get thin black border? but for development I want to know if this happens.
+ raise "too many directives given to process_directives" if args.size > 2
instructions = [THIN, Formatting::COLOURS['black']]
args.each do |a|
@@ -454,6 +460,11 @@ def process_directives(directives)
instructions[0] = line_type_directives_hash[a]
next
end
+
+ if Borders.line_type_constants.include?(a)
+ instructions[0] = a
+ next
+ end
raise "I don't know how to format a border with #{a.inspect}."
end
View
26 lib/row.rb
@@ -65,6 +65,7 @@ def adjust_boundary_column_indexes(*args)
end
end
+ # TODO can we get rid of this? Tests pass if it is commented out.
def style=(style)
adjust_height(style)
@xf_index = @parent_wb.styles.add(style)
@@ -102,7 +103,30 @@ def cell(col_index)
end
def write(col, label, style)
- style = StyleFormat.new(style) if style.is_a?(Hash)
+ case style
+ when StyleFormat
+ # leave it alone
+ when Hash
+ style = StyleFormat.new(style)
+### @export "autoformats"
+ when TrueClass # Automatically apply a nice numeric format.
+ case label
+ when DateTime, Time
+ style = @parent_wb.styles.default_datetime_style
+ when Date
+ style = @parent_wb.styles.default_date_style
+ when Float
+ style = @parent_wb.styles.default_float_style
+ else
+ style = @parent_wb.styles.default_style
+ end
+### @end
+ when NilClass
+ style = @parent_wb.styles.default_style
+ else
+ raise "I don't know how to use this to format a cell #{style.inspect}"
+ end
+
style_index = @parent_wb.styles.add(style)
adjust_height(style)
View
14 lib/style.rb
@@ -97,6 +97,20 @@ def initialize
@default_format = add_style(@default_style)[0]
end
+### @export "autoformats"
+ def default_date_style
+ @default_date_style ||= StyleFormat.new(:number_format_string => 'dd-mmm-yyyy')
+ end
+
+ def default_datetime_style
+ @default_datetime_style ||= StyleFormat.new(:number_format_string => 'dd-mmm-yyyy hh:mm:ss')
+ end
+
+ def default_float_style
+ @default_float_style ||= StyleFormat.new(:number_format_string => '#,##0.00')
+ end
+### @end
+
def add(style)
if style.nil?
0x10 # Return the index of the default style.
View
4 lib/surpass.rb
@@ -1,7 +1,7 @@
module Surpass
# :stopdoc:
- VERSION = '0.0.3'
+ VERSION = '0.0.4'
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
# :startdoc:
@@ -48,4 +48,4 @@ def self.require_all_libs_relative_to( fname, dir = "." )
Surpass.require_all_libs_relative_to(__FILE__)
-require 'date'
+require 'date'
View
2  lib/workbook.rb
@@ -195,7 +195,7 @@ def window_1_record
def data
doc = ExcelDocument.new
- doc.data(to_biff)
+ doc.data(to_biff).read
end
def save(filename = nil)
View
8 lib/worksheet.rb
@@ -270,25 +270,25 @@ def set_range_style(row_range, col_range, style, create_blanks = false)
end
# Write the text stored in label in a single cell at (r,c) according to style.
- def write(r, c, label = "", style = @parent.styles.default_style)
+ def write(r, c, label = "", style = nil)
row(r).write(c, label, style)
end
- def write_array_to_row(array, r, c = 0, style = @parent.styles.default_style)
+ def write_array_to_row(array, r, c = 0, style = true)
array.each_with_index do |a, i|
row(r).write(c + i, a, style)
end
end
alias :rarray :write_array_to_row
- def write_array_to_column(array, c, r = 0, style = @parent.styles.default_style)
+ def write_array_to_column(array, c, r = 0, style = true)
array.each_with_index do |a, i|
row(r + i).write(c, a, style)
end
end
alias :carray :write_array_to_column
- def write_arrays(array_of_arrays, r = 0, c = 0, style = @parent.styles.default_style)
+ def write_arrays(r, c, array_of_arrays, style = true)
array_of_arrays.each_with_index do |a, i|
raise "not an array of arrays!" unless a.is_a?(Array)
write_array_to_row(a, r + i, c, style)
View
BIN  surpass-manual-0-0-3.pdf
Binary file not shown
View
BIN  surpass-manual-0-0-4.pdf
Binary file not shown
View
BIN  webby/content/examples/autoformat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
32 webby/content/examples/autoformat.rb
@@ -0,0 +1,32 @@
+require 'rubygems'
+require 'surpass'
+
+book = Workbook.new(__FILE__.gsub(/rb$/, "xls"))
+sheet = book.add_sheet
+
+# Passing true for the style parameter to write will invoke autoformatting.
+sheet.write(0, 0, "Hello World!", true)
+sheet.write(1, 0, 1, true)
+sheet.write(2, 0, 1.0, true)
+sheet.write(3, 0, Date.today, true)
+sheet.write(4, 0, DateTime.now, true)
+sheet.write(5, 0, Time.now, true)
+
+array_of_arrays = [
+ [1, 2, 3],
+ [1.0, 2.0, 3.0],
+ [Date.today, DateTime.now],
+ %w{a b c}
+]
+
+# Writing arrays will automatically autoformat.
+sheet.write(7, 0, "With autoformat:")
+sheet.write_arrays(8, 0, array_of_arrays)
+
+# Unless you specify your own format, or nil for a generic default.
+sheet.write(13, 0, "Without autoformat:")
+sheet.write_arrays(14, 0, array_of_arrays, nil)
+
+sheet.set_column_widths(0..2, 20)
+
+book.save
View
BIN  webby/content/examples/autoformat.xls
Binary file not shown
View
BIN  webby/content/examples/colours.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  webby/content/examples/data.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
11 webby/content/examples/data.rb
@@ -0,0 +1,11 @@
+require 'rubygems'
+require 'surpass'
+
+book = Workbook.new
+sheet = book.add_sheet
+
+sheet.write(0, 0, "Hello World!")
+
+File.open(__FILE__.gsub(/rb$/, "xls"), "w") do |f|
+ f.write book.data
+end
View
BIN  webby/content/examples/data.xls
Binary file not shown
View
BIN  webby/content/examples/formatting.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  webby/content/examples/formatting.xls
Binary file not shown
View
BIN  webby/content/examples/hello-world.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
34 webby/content/surpass-manual.erb
@@ -35,6 +35,7 @@ sudo gem install surpass
\begin{verbatim}
bzr branch http://ananelson.com/code/surpass
+cd surpass
sudo rake gem:install
\end{verbatim}
@@ -57,6 +58,26 @@ And, here's how it looks.
\chapter{Writing Data} % (fold)
\label{cha:writing_data}
+
+\section{Autoformatting} % (fold)
+\label{sec:autoformatting}
+
+Autoformats are number formats which are automatically applied to Dates, Floats and similar classes. To have autoformats applied, then pass true as the style parameter to the write function.
+
+Here is the relevant code from row.rb:
+<%= lidio("../../lib/row.rb", "autoformats") %>
+
+And here are the default formats being defined in style.rb:
+<%= lidio("../../lib/style.rb", "autoformats") %>
+
+If you use any of the array-writing methods, then autoformatting will be applied by default. To override this behaviour you can pass your own StyleFormat or nil to use the generic default format.
+
+<%= lidio("examples/autoformat.rb") %>
+
+\includegraphics[width=15cm]{examples/autoformat.png}
+
+% section autoformatting (end)
+
% chapter writing_data (end)
@@ -118,3 +139,16 @@ And, here's how it looks.
\includegraphics[width=16cm]{examples/formatting.png}
% chapter formatting (end)
+
+\chapter{Saving} % (fold)
+\label{cha:saving}
+
+Typically, you will call the workbook's save() method to write that workbook to a file. You can pass the filename as an argument to save(), or as an argument to new() when you first instantiate a workbook object.
+
+However, you can also call a workbook's data() method, which gives you direct access to a workbook's binary data. You can write this to a file manually, as in this example:
+
+<%= lidio("examples/data.rb") %>
+
+Or, you could use this data as an argument to Rails' send\_data method.
+
+% chapter saving (end)
Please sign in to comment.
Something went wrong with that request. Please try again.