ods_extractor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +2 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/ods_extractor/csv_output.rb +25 -0
- data/lib/ods_extractor/row_output.rb +33 -0
- data/lib/ods_extractor/sax_handler.rb +73 -0
- data/lib/ods_extractor/sheet_filter_handler.rb +51 -0
- data/lib/ods_extractor/version.rb +5 -0
- data/lib/ods_extractor.rb +50 -0
- data/ods_extractor.gemspec +44 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2bd1a5fbb6b584b6978706613bf810ffa5456914e471c1d999c11ddb7c3a90ae
|
4
|
+
data.tar.gz: eed9cc31d0260f547016963dc6250dc130ef1d0bc5c71281ab0106f43a29c1ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9923cb14b0355eb6e46458fa6a4d6672ac3caa60c7893115f277123f9ac83615bd085de959998a74fc4763babf5630d5ac5102484134f3b818d4d03e78f2c721
|
7
|
+
data.tar.gz: 2d27af1cd9c8dd2235af9cd64bc5767048c95625254d4d68eae89e26f1815ace0e94c00da6cdaecb370995cc098cf4607629ea163b7af54a791ede026ffb8bbf
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at me@julik.nl. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Julik Tarkhanov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# ODS Extractor
|
2
|
+
|
3
|
+
This gem will help you export multiple sheets from OpenOffice Calc documents. The documents often contain multiple sheets,
|
4
|
+
and even though there is a command on OpenOffice to export a single sheet as CSV it will only export the sheet which
|
5
|
+
is currently selected. You will also need a full install of OpenOffice to do this (which, in a server environment, will mean
|
6
|
+
also a full GUI environment).
|
7
|
+
|
8
|
+
There _are_ ways to use the `--headless` option to extract the sheets that you want, but you need to create a macro
|
9
|
+
and load it into openoffice, and then trigger it from the commandline before exporting.
|
10
|
+
Or you need to drive OO from UNO which you also need to install (see below in "Other solutions").
|
11
|
+
|
12
|
+
But instead of using a full OpenOffice/LibreOffice install we can also solve this from the other end and just parse the ODS document ourselves.
|
13
|
+
This is easier than it might seem at first glance, but there are is a pitfall. There are gems which manipulate spreadsheet
|
14
|
+
documents in this way - but they first load the entire document into memory, usually as a DOM. However this quickly breaks
|
15
|
+
down when huge documents are involved.
|
16
|
+
|
17
|
+
This gem uses a SAX parser to ingest the spreadsheets in a streaming fashion. An ODS document is just a ZIP with a huge XML inside
|
18
|
+
of it. For opening the ZIP file [zip_tricks](https://github.com/WeTransfer/zip_tricks) is used. For parsing the XML
|
19
|
+
a SAX parser from Nokogiri is used - you are likely to have Nokogiri already installed, as Rails uses it to sanitize HTML. The extraction
|
20
|
+
of the rows happens as the XML is fed to Nokogiri directly from the ZIP file, without having to create any intermediate files on
|
21
|
+
the file system.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You need access to an IO object with random access which contains your ODS file. Imagine you want to capture all rows from the `Expenses` sheet
|
26
|
+
in your ODS, and you want to process them inline, as they get parsed:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# The output receives the sheet name and the row contents. `use_header_rows`
|
30
|
+
# will yield `Hash` objects with headers from the first row as keys.
|
31
|
+
output = ODSExtractor::RowOutput.new(use_header_row: true) do |sheet_name:, row:|
|
32
|
+
LineItem.create!(expenditure: row.fetch("Expenditure"), amount: row.fetch('Amount').to_i)
|
33
|
+
end
|
34
|
+
|
35
|
+
File.open(__dir__ + '/big_spreadsheet.ods') do |f|
|
36
|
+
ODSExtractor.extract(input_io: f, output: out, sheet_names: "Expenses")
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'ods_extractor'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle install
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install ods_extractor
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/julik/ods_extractor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/julik/ods_extractor/blob/master/CODE_OF_CONDUCT.md).
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
+
|
70
|
+
## Code of Conduct
|
71
|
+
|
72
|
+
Everyone interacting in the ODSExtractor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/julik/ods_extractor/blob/master/CODE_OF_CONDUCT.md).
|
73
|
+
|
74
|
+
## Other solutions
|
75
|
+
|
76
|
+
* https://www.linuxjournal.com/content/convert-spreadsheets-csv-files-python-and-pyuno-part-1v2
|
77
|
+
* https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=79869
|
78
|
+
* https://www.briankoponen.com/libreoffice-export-sheets-csv/
|
79
|
+
* https://ask.libreoffice.org/t/how-to-convert-specific-sheet-to-csv-via-command-line/11842
|
80
|
+
* https://askubuntu.com/questions/1042624/how-to-split-an-ods-spreadsheet-file-into-csv-files-per-sheet-on-the-terminal
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "ods_extractor"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class ODSExtractor::CSVOutput
|
2
|
+
def initialize(csv_directory_path)
|
3
|
+
require "csv"
|
4
|
+
@csv_directory_path = csv_directory_path
|
5
|
+
end
|
6
|
+
|
7
|
+
def start_sheet(sheet_name)
|
8
|
+
@write_output_file = File.open(File.join(@csv_directory_path, sheet_name_to_csv_file_name(sheet_name)), "wb")
|
9
|
+
end
|
10
|
+
|
11
|
+
def write_row(row_values)
|
12
|
+
@write_output_file << CSV.generate_line(row_values, force_quotes: true) # => "foo,0\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
def end_sheet
|
16
|
+
@write_output_file.close
|
17
|
+
end
|
18
|
+
|
19
|
+
def sheet_name_to_csv_file_name(sheet_name)
|
20
|
+
# This is a subtle spot where there can be a security problem - we take an unsanitized sheet name
|
21
|
+
# and we include it in a filesystem path. So some precaution needs to be taken.
|
22
|
+
sanitized_sheet_name = File.basename(File.expand_path(sheet_name))
|
23
|
+
"#{sanitized_sheet_name}.csv"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class ODSExtractor::RowOutput
|
2
|
+
def initialize(use_header_row:, &row_handler_block)
|
3
|
+
@header_cells = []
|
4
|
+
@row_handler = row_handler_block
|
5
|
+
@use_header_row = use_header_row
|
6
|
+
end
|
7
|
+
|
8
|
+
def start_sheet(sheet_name)
|
9
|
+
@sheet_name = sheet_name
|
10
|
+
@header_cells = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def write_row(row_values)
|
14
|
+
if @use_header_row
|
15
|
+
if @header_cells
|
16
|
+
@row_handler.call(sheet_name: @sheet_name, row: build_row_hash(row_values))
|
17
|
+
else
|
18
|
+
@header_cells = row_values.map(&:to_s)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
@row_handler.call(sheet_name: @sheet_name, row: row_values)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def end_sheet
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_row_hash(row_values)
|
29
|
+
padded_row = row_values.take(@header_cells.length)
|
30
|
+
(@header_cells.length - padded_row.length).times { padded_row << nil }
|
31
|
+
@header_cells.zip(padded_row).to_h
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
# Because few people use this often: an XML SAX parser parses the document in a streaming fashion instead of
|
4
|
+
# reconstructing a DOM tree. You can build a DOM tree based on a SAX parser but not the other way around. SAX parsers become
|
5
|
+
# useful when parsing documents which are very large - and our "contents.xml" _is_ damn large indeed. To use a SAX parser we need
|
6
|
+
# to write a handler, in the handler we are going to capture the elements we care about. The OpenOasis schema structures
|
7
|
+
# a single sheet inside a "table:table" element, then every row is in "table:table-row", then every cell is within
|
8
|
+
# "table:table-cell". Anything further down we can treat as text and just capture "as-is" (there are some wrapper tags
|
9
|
+
# for paragraphs but these are not really important for our mission).
|
10
|
+
class ODSExtractor::SAXHandler < Nokogiri::XML::SAX::Document
|
11
|
+
MAX_CELLS_PER_ROW = 2**14
|
12
|
+
MAX_ROWS_PER_SHEET = 2**20
|
13
|
+
|
14
|
+
def initialize(output_handler)
|
15
|
+
@out = output_handler
|
16
|
+
end
|
17
|
+
|
18
|
+
def start_element(name, attributes = [])
|
19
|
+
case name
|
20
|
+
when "table:table"
|
21
|
+
sheet_name = attributes.to_h.fetch("table:name")
|
22
|
+
@out.start_sheet(sheet_name)
|
23
|
+
@rows_output_so_far = 0
|
24
|
+
when "table:table-row"
|
25
|
+
# Here be dragons: https://stackoverflow.com/a/2741709/153886
|
26
|
+
# Both rows and cells are actually _sparsely_ recorded in the XML, see below
|
27
|
+
# for the same for cells.
|
28
|
+
@row_repeats_n_times = attributes.to_h.fetch("table:number-rows-repeated", "1").to_i
|
29
|
+
if @rows_output_so_far + @row_repeats_n_times >= MAX_ROWS_PER_SHEET
|
30
|
+
# The ODS table contains "at most"
|
31
|
+
# 1048576 rows. When we are at the last row, ODS will helpfully
|
32
|
+
# tell us that there are "that many" repeat empty rows until the end of sheet.
|
33
|
+
# These cells are useless for us of course, but if we repeat them literally
|
34
|
+
# we will still output them to the CSV. We can use this to detect our last row.
|
35
|
+
@row_repeats_n_times = 0
|
36
|
+
end
|
37
|
+
# and prepare an empty row
|
38
|
+
@row = []
|
39
|
+
when "table:table-cell"
|
40
|
+
@cell_repeats_n_times = attributes.to_h.fetch("table:number-columns-repeated", "1").to_i
|
41
|
+
if @row.length + @cell_repeats_n_times >= MAX_CELLS_PER_ROW
|
42
|
+
# Again something pertinent: the ODS table contains "at most"
|
43
|
+
# 2**14 columns - 16384. When we are at the last cell of the row, ODS will helpfully
|
44
|
+
# tell us that there are "that many" repeat empty cells until the next row starts.
|
45
|
+
# We can thus detect the last row by it having number-columns-repeated which creates N
|
46
|
+
# similar cells. If we encounter that we can simply omit that cell, it is most certainly empty
|
47
|
+
@cell_repeats_n_times = 0
|
48
|
+
end
|
49
|
+
@charbuf = String.new(capacity: 512) # Create a string which is unlikely to be resized all the time
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def characters(string)
|
54
|
+
# @charbuf is only not-nil when we are inside a "table:table-cell" element, this allows us to skip
|
55
|
+
# any chardata that is outside of cells for whatever reason
|
56
|
+
@charbuf << string if @charbuf
|
57
|
+
end
|
58
|
+
|
59
|
+
def end_element(name)
|
60
|
+
case name
|
61
|
+
when "table:table"
|
62
|
+
@out.end_sheet
|
63
|
+
when "table:table-row"
|
64
|
+
@rows_output_so_far += @row_repeats_n_times
|
65
|
+
@row_repeats_n_times.times do
|
66
|
+
@out.write_row(@row)
|
67
|
+
end
|
68
|
+
when "table:table-cell"
|
69
|
+
@cell_repeats_n_times.times { @row << @charbuf.strip } # Have to strip due to XML having sometimes-significant whitespace
|
70
|
+
@charbuf = nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class ODSExtractor::SheetFilterHandler < Nokogiri::XML::SAX::Document
|
2
|
+
def initialize(sax_handler, filter_definition)
|
3
|
+
@handler = sax_handler
|
4
|
+
@sheet_name_filter_proc = build_filter_proc(filter_definition)
|
5
|
+
@bypass = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def start_element(name, attributes = [])
|
9
|
+
if name == "table:table"
|
10
|
+
sheet_name = attributes.to_h.fetch("table:name")
|
11
|
+
if @sheet_name_filter_proc.call(sheet_name)
|
12
|
+
@bypass = false
|
13
|
+
@handler.start_element(name, attributes)
|
14
|
+
else
|
15
|
+
@bypass = true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@handler.start_element(name, attributes) unless @bypass
|
20
|
+
end
|
21
|
+
|
22
|
+
def error(string)
|
23
|
+
raise ODSExtractor::Error, "XML parse error: #{string}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def characters(string)
|
27
|
+
return if @bypass
|
28
|
+
@handler.characters(string)
|
29
|
+
end
|
30
|
+
|
31
|
+
def end_element(name)
|
32
|
+
return if @bypass
|
33
|
+
@handler.end_element(name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_filter_proc(filter_definition)
|
37
|
+
case filter_definition
|
38
|
+
when String
|
39
|
+
->(sheet_name) { sheet_name == filter_definition }
|
40
|
+
when Regexp
|
41
|
+
->(sheet_name) { sheet_name =~ filter_definition }
|
42
|
+
when Array
|
43
|
+
filter_definitions = filter_definition.map { |defn| build_filter_proc(defn) }
|
44
|
+
->(sheet_name) { filter_definitions.any? { |prc| prc.call(sheet_name) } }
|
45
|
+
when Method, Proc
|
46
|
+
filter_definition.to_proc # Return as is
|
47
|
+
else
|
48
|
+
"Sheet name filter must be an Array of String|Regexp|callable or a String|Regexp|callable"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ods_extractor/version"
|
4
|
+
require_relative "ods_extractor/csv_output"
|
5
|
+
require_relative "ods_extractor/sax_handler"
|
6
|
+
require_relative "ods_extractor/row_output"
|
7
|
+
require_relative "ods_extractor/sheet_filter_handler"
|
8
|
+
|
9
|
+
require "zip_tricks"
|
10
|
+
require "nokogiri"
|
11
|
+
|
12
|
+
module ODSExtractor
|
13
|
+
class Error < StandardError; end
|
14
|
+
ACCEPT_ALL_SHEETS_PROC = ->(_sheet_name) { true }
|
15
|
+
PROGRESS_HANDLER_PROC = ->(bytes_read, bytes_remaining) { true }
|
16
|
+
CHUNK_SIZE = 32 * 1024
|
17
|
+
|
18
|
+
def self.extract(input_io:, output_handler:, sheet_names: ACCEPT_ALL_SHEETS_PROC, progress_handler_proc: PROGRESS_HANDLER_PROC)
|
19
|
+
# Feed the XML from the extractor directly to the SAX parser
|
20
|
+
entries = ZipTricks::FileReader.read_zip_structure(io: input_io)
|
21
|
+
contentx_xml_zip_entry = entries.find { |e| e.filename == "content.xml" }
|
22
|
+
|
23
|
+
raise Error, "No `content.xml` found in the ODS file" unless contentx_xml_zip_entry
|
24
|
+
|
25
|
+
sax_handler = ODSExtractor::SAXHandler.new(output_handler)
|
26
|
+
sax_filter = ODSExtractor::SheetFilterHandler.new(sax_handler, sheet_names)
|
27
|
+
|
28
|
+
# Because we do not have a random access IO to the deflated XML inside the zip, but
|
29
|
+
# we will be reading the deflated bytes and inflating them ourselves, we can't really
|
30
|
+
# use the standard Parser - we need to use the PushParser. The Parser "reads" by itself
|
31
|
+
# from the IO it has been given, PushParser can be fed bytes as we deflate them.
|
32
|
+
push_parser = Nokogiri::XML::SAX::PushParser.new(sax_filter)
|
33
|
+
|
34
|
+
# The "extract" call reads N bytes, inflates them and then returns them. We do not
|
35
|
+
# know how big the inflated data will be before we inflate it, and the libxml2
|
36
|
+
# push parser will abort with an error if we force-feed it chunks which are too big.
|
37
|
+
# So read smol.
|
38
|
+
ex = contentx_xml_zip_entry.extractor_from(input_io)
|
39
|
+
progress_handler_proc.call(0, contentx_xml_zip_entry.uncompressed_size)
|
40
|
+
bytes_read = 0
|
41
|
+
until ex.eof?
|
42
|
+
chunk = ex.extract(CHUNK_SIZE)
|
43
|
+
bytes_read += chunk.bytesize
|
44
|
+
progress_handler_proc.call(bytes_read, contentx_xml_zip_entry.uncompressed_size - bytes_read)
|
45
|
+
push_parser << chunk
|
46
|
+
end
|
47
|
+
ensure
|
48
|
+
push_parser&.finish
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ods_extractor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ods_extractor"
|
7
|
+
spec.version = ODSExtractor::VERSION
|
8
|
+
spec.authors = ["Julik Tarkhanov"]
|
9
|
+
spec.email = ["me@julik.nl"]
|
10
|
+
|
11
|
+
spec.summary = "Extracts tabular data from OpenOffice Calc ODS files"
|
12
|
+
spec.description = "Especially useful if your document is very, very large."
|
13
|
+
spec.homepage = "https://github.com/cheddar-me/ods_extractor"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/cheddar-me/ods_extractor"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/cheddar-me/ods_extractor/blob/main/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
# Uncomment to register a new dependency of your gem
|
35
|
+
spec.add_dependency "zip_tricks"
|
36
|
+
spec.add_dependency "nokogiri"
|
37
|
+
|
38
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
40
|
+
spec.add_development_dependency "standard", "~> 1.3"
|
41
|
+
|
42
|
+
# For more information and examples about making a new gem, checkout our
|
43
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ods_extractor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julik Tarkhanov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: zip_tricks
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: standard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
description: Especially useful if your document is very, very large.
|
84
|
+
email:
|
85
|
+
- me@julik.nl
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".rspec"
|
91
|
+
- ".standard.yml"
|
92
|
+
- CHANGELOG.md
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/ods_extractor.rb
|
101
|
+
- lib/ods_extractor/csv_output.rb
|
102
|
+
- lib/ods_extractor/row_output.rb
|
103
|
+
- lib/ods_extractor/sax_handler.rb
|
104
|
+
- lib/ods_extractor/sheet_filter_handler.rb
|
105
|
+
- lib/ods_extractor/version.rb
|
106
|
+
- ods_extractor.gemspec
|
107
|
+
homepage: https://github.com/cheddar-me/ods_extractor
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata:
|
111
|
+
source_code_uri: https://github.com/cheddar-me/ods_extractor
|
112
|
+
changelog_uri: https://github.com/cheddar-me/ods_extractor/blob/main/CHANGELOG.md
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 2.6.0
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.2.32
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Extracts tabular data from OpenOffice Calc ODS files
|
132
|
+
test_files: []
|