spreadsheet_report 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/spreadsheet_report.rb +14 -10
  2. metadata +2 -2
@@ -9,7 +9,11 @@ class SpreadsheetReport
9
9
  yield(self)
10
10
  end
11
11
 
12
- def worksheet(name, query)
12
+ # Writes a report to a worksheet. By default, if cols is left nil, the report
13
+ # will base cols on the results of the query (but it's a hash, so no ordering
14
+ # is guaranteed). If cols is given, only those columns are reported, and the
15
+ # spreadsheet columns are populated in the order that they are given.
16
+ def worksheet(name, query, cols = nil)
13
17
  worksheet = @spreadsheet.worksheets.find { |ws| ws.title == name }
14
18
 
15
19
  # create the worksheet if we didn't find it
@@ -20,24 +24,23 @@ class SpreadsheetReport
20
24
 
21
25
  # run the query
22
26
  result = ActiveRecord::Base.connection.select_all(query)
27
+
28
+ # figure out the column names being used
29
+ if cols.nil? && result.size > 0
30
+ cols = result[0].keys
31
+ end
23
32
 
24
33
  # resize the worksheet
25
- rows = result.size + 1
26
- keys = []
27
- if result.size > 0
28
- keys = result[0].keys
29
- end
30
- cols = keys.size
31
- worksheet = @spreadsheet.add_worksheet(name, result.size + 1, keys.size)
34
+ worksheet = @spreadsheet.add_worksheet(name, result.size + 1, cols.size)
32
35
 
33
36
  # add the title cells
34
- keys.each_with_index do |key, index|
37
+ cols.each_with_index do |key, index|
35
38
  worksheet[1, index + 1] = key
36
39
  end
37
40
 
38
41
  current_row = 2
39
42
  result.each do |row_data|
40
- keys.each_with_index do |key, index|
43
+ cols.each_with_index do |key, index|
41
44
  worksheet[current_row, index + 1] = row_data[key]
42
45
  end
43
46
  current_row += 1
@@ -48,5 +51,6 @@ class SpreadsheetReport
48
51
  end
49
52
 
50
53
  worksheet.save
54
+ worksheet.synchronize
51
55
  end
52
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreadsheet_report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gnoso, Inc.
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-25 00:00:00 -04:00
12
+ date: 2009-09-28 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency