forked from jeremylightsmith/cards
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numbers_parser.rb
45 lines (38 loc) · 1 KB
/
numbers_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# this is making this a mac only distro, I'm afraid
gem 'rb-appscript'
require 'appscript'
require 'cards/tabular_parser'
module Cards
class NumbersParser
include TabularParser
def initialize(file, table_name)
@file, @table_name = file, table_name
end
def each_unparsed_row
numbers = Appscript.app('Numbers')
document = numbers.open(@file)
table = find_table(document, @table_name)
table.rows.get.each do |row|
values = []
row.cells.get.map do |cell|
value = cell.value.get
value = nil if value == 0
values << value
end
yield values
end
end
def default_output_file
@table_name
end
private
def find_table(document, name)
document.sheets.get.each do |sheet|
sheet.tables.get.each do |table|
return table if table.name.get.downcase == name.downcase
end
end
raise "table #{name} not found in #{document.name}"
end
end
end