xlsx,csv: trim whitespace, yield to optional block

Closes #108 and #79
This commit is contained in:
Andy Meneely
2015-10-13 10:37:49 -04:00
parent 521bfac6b2
commit c8e6f9803c
9 changed files with 317 additions and 15 deletions
+29 -4
View File
@@ -3,7 +3,7 @@ require 'squib'
Squib::Deck.new(cards: 3) do
background color: :white
# Takes the first sheet by default
# Reads the first sheet by default (sheet 0)
# Outputs a hash of arrays with the header names as keys
data = xlsx file: 'sample.xlsx'
@@ -11,8 +11,33 @@ Squib::Deck.new(cards: 3) do
text str: data['Level'], x: 65, y: 65, font: 'Arial 72'
text str: data['Description'], x: 65, y: 600, font: 'Arial 36'
# You can also specify the sheet, starting at 0
data = xlsx file: 'sample.xlsx', sheet: 2
save format: :png, prefix: 'sample_excel_' #save to individual pngs
end
save format: :png, prefix: 'sample_excel_'
# Here's another example, a bit more realistic. Here's what's going on:
# * We call xlsx from Squib directly - BEFORE Squib::Deck creation. This
# allows us to infer the number of cards based on the size of the "Name"
# field
# * We make use of quantity explosion. Fields named "Qty" or "Quantity"
# (any capitalization), or any other in the "qty_header" get expanded by the
# number given
# * We also make sure that trailing and leading whitespace is stripped
# from each value. This is the default behavior in Squib, but the options
# are here just to make sure.
resource_data = Squib.xlsx(file: 'sample.xlsx', sheet: 2, strip: true) do |header, value|
case header
when 'Cost'
"$#{value}k" # e.g. "3" becomes "$3k"
else
value # always return the original value if you didn't do anything to it
end
end
Squib::Deck.new(cards: resource_data['Name'].size) do
background color: :white
rect width: :deck, height: :deck
text str: resource_data['Name'], align: :center, width: :deck, hint: 'red'
text str: resource_data['Cost'], align: :right, width: :deck, hint: 'red'
save_sheet prefix: 'sample_excel_resources_' #save to a whole sheet
end
Binary file not shown.