spreadsheet_encrypt 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f830286b7845e5dbdba5c935696d89c069bf0f27323a76a659dad678dfbdbfe2
4
+ data.tar.gz: ec8207dc7a7080f60642a95957a9431b112d87b065c736a04868bcf55eb5e7a6
5
+ SHA512:
6
+ metadata.gz: 51895e92f784eb5e91b43748ea73c6b73ddfde5748f105ae58d8d4e8bc25e21a77aaee9020d3b154cb8a27d3da37dd8b31aa4a6772824878c79d08b3e33f5d09
7
+ data.tar.gz: 48ae9a56a4413ca549b3bb2db30206a72619b25fa46abfe54ae558cd6d0163f0a6fd9219a1d5c3692f3a051d056d93a21d5fe679186f517aa6673d96439497c2
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## [0.1.0] - 2026-09-04
2
+
3
+ ### Added
4
+
5
+ - Encrypt `.xlsx` with an Office open password via ooxml_crypt
6
+ - Convert `.xls` to `.xlsx` then encrypt
7
+ - Library API `SpreadsheetEncrypt.encrypt` (positional and keyword args)
8
+ - CLI `spreadsheet_encrypt`
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 汤宇浩
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,69 @@
1
+ # SpreadsheetEncrypt
2
+
3
+ Encrypt Excel spreadsheet files (`.xls` / `.xlsx`) with an **open password** (Office Open XML encryption). Opening the output in Excel / WPS requires the password.
4
+
5
+ - `.xlsx` — encrypted directly
6
+ - `.xls` — converted to `.xlsx` (cell values / basic multi-sheet), then encrypted
7
+
8
+ ## Installation
9
+
10
+ Add to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "spreadsheet_encrypt"
14
+ ```
15
+
16
+ Or install locally from source:
17
+
18
+ ```bash
19
+ bundle exec rake install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Library
25
+
26
+ ```ruby
27
+ require "spreadsheet_encrypt"
28
+
29
+ # Positional arguments: input path, password, output path
30
+ SpreadsheetEncrypt.encrypt(
31
+ "/path/to/input.xls",
32
+ "secret-password",
33
+ "/path/to/output.xlsx"
34
+ )
35
+
36
+ # Keyword arguments
37
+ SpreadsheetEncrypt.encrypt(
38
+ input: "/path/to/book.xlsx",
39
+ password: "secret",
40
+ output: "/path/to/book.encrypted.xlsx"
41
+ )
42
+ ```
43
+
44
+ Returns the actual output path written (String). If the input is `.xls` and the output path still ends with `.xls`, the extension is automatically changed to `.xlsx`.
45
+
46
+ ### CLI
47
+
48
+ ```bash
49
+ bundle exec spreadsheet_encrypt input.xlsx -p secret -o out.xlsx
50
+ bundle exec spreadsheet_encrypt input.xls -p secret -o out.xlsx
51
+ ```
52
+
53
+ ## Limitations
54
+
55
+ - Encryption targets **file open password** (not sheet/workbook edit protection).
56
+ - Compatible with documents encrypted in the Office 2010+ OOXML style (via [ooxml_crypt](https://github.com/teamsimplepay/ooxml_crypt)).
57
+ - `.xls` conversion preserves cell values and multiple sheets; macros, charts, and complex formatting are **not** guaranteed.
58
+
59
+ ## Development
60
+
61
+ ```bash
62
+ bin/setup
63
+ bundle exec rspec
64
+ bundle exec rubocop
65
+ ```
66
+
67
+ ## License
68
+
69
+ MIT. See [LICENSE.txt](LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "optparse"
5
+ require "spreadsheet_encrypt"
6
+
7
+ options = {}
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: spreadsheet_encrypt INPUT -p PASSWORD -o OUTPUT"
10
+
11
+ opts.on("-p", "--password PASSWORD", "Open password for the output file") do |password|
12
+ options[:password] = password
13
+ end
14
+
15
+ opts.on("-o", "--output PATH", "Output file path (.xlsx)") do |path|
16
+ options[:output] = path
17
+ end
18
+
19
+ opts.on("-h", "--help", "Show help") do
20
+ puts opts
21
+ exit
22
+ end
23
+
24
+ opts.on("-v", "--version", "Show version") do
25
+ puts SpreadsheetEncrypt::VERSION
26
+ exit
27
+ end
28
+ end
29
+
30
+ parser.parse!
31
+
32
+ input = ARGV[0]
33
+ if input.nil? || options[:password].nil? || options[:output].nil?
34
+ warn parser.help
35
+ exit 1
36
+ end
37
+
38
+ begin
39
+ path = SpreadsheetEncrypt.encrypt(input, options[:password], options[:output])
40
+ puts path
41
+ rescue SpreadsheetEncrypt::Error => e
42
+ warn "Error: #{e.message}"
43
+ exit 1
44
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "ooxml_crypt"
5
+ require_relative "errors"
6
+ require_relative "xls_converter"
7
+
8
+ module SpreadsheetEncrypt
9
+ # Encrypts spreadsheet files with an Office open password.
10
+ class Encryptor
11
+ SUPPORTED_EXTENSIONS = %w[.xls .xlsx].freeze
12
+
13
+ def self.encrypt(input_path, password, output_path)
14
+ new(input_path, password, output_path).encrypt
15
+ end
16
+
17
+ def initialize(input_path, password, output_path)
18
+ @input_path = input_path.to_s
19
+ @password = password.to_s
20
+ @output_path = output_path.to_s
21
+ end
22
+
23
+ # @return [String] actual output path written
24
+ def encrypt
25
+ validate!
26
+ ensure_output_directory!
27
+
28
+ case extension
29
+ when ".xlsx"
30
+ encrypt_xlsx(@input_path, @output_path)
31
+ when ".xls"
32
+ encrypt_xls
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def validate!
39
+ raise InvalidPassword, "password must not be empty" if @password.strip.empty?
40
+ raise FileNotFound, "input file not found: #{@input_path}" unless File.file?(@input_path)
41
+
42
+ return if SUPPORTED_EXTENSIONS.include?(extension)
43
+
44
+ raise UnsupportedFormat,
45
+ "unsupported format '#{extension}'; supported: #{SUPPORTED_EXTENSIONS.join(', ')}"
46
+ end
47
+
48
+ def extension
49
+ @extension ||= File.extname(@input_path).downcase
50
+ end
51
+
52
+ def ensure_output_directory!
53
+ dir = File.dirname(@output_path)
54
+ FileUtils.mkdir_p(dir) unless dir.empty? || dir == "."
55
+ end
56
+
57
+ def normalize_output_path
58
+ return @output_path if File.extname(@output_path).downcase == ".xlsx"
59
+
60
+ base = @output_path.sub(/\.xls\z/i, "")
61
+ "#{base}.xlsx"
62
+ end
63
+
64
+ def encrypt_xlsx(source, destination)
65
+ OoxmlCrypt.encrypt_file(source, @password, destination)
66
+ destination
67
+ rescue StandardError => e
68
+ raise EncryptionFailed, "failed to encrypt spreadsheet: #{e.message}"
69
+ end
70
+
71
+ def encrypt_xls
72
+ destination = normalize_output_path
73
+ tempfile = XlsConverter.to_xlsx(@input_path)
74
+ encrypt_xlsx(tempfile.path, destination)
75
+ ensure
76
+ if tempfile
77
+ tempfile.close
78
+ tempfile.unlink
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpreadsheetEncrypt
4
+ class Error < StandardError; end
5
+
6
+ class UnsupportedFormat < Error; end
7
+
8
+ class FileNotFound < Error; end
9
+
10
+ class InvalidPassword < Error; end
11
+
12
+ class EncryptionFailed < Error; end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpreadsheetEncrypt
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "caxlsx"
4
+ require "roo"
5
+ require "roo-xls"
6
+ require "tempfile"
7
+
8
+ module SpreadsheetEncrypt
9
+ # Converts a legacy .xls workbook into a temporary .xlsx file (cell values only).
10
+ class XlsConverter
11
+ def self.to_xlsx(input_path)
12
+ new(input_path).to_xlsx
13
+ end
14
+
15
+ def initialize(input_path)
16
+ @input_path = input_path
17
+ end
18
+
19
+ # @return [Tempfile] caller must close/unlink when done
20
+ def to_xlsx
21
+ book = Roo::Spreadsheet.open(@input_path, extension: :xls)
22
+ tempfile = Tempfile.new(["spreadsheet_encrypt", ".xlsx"])
23
+ tempfile.binmode
24
+
25
+ package = Axlsx::Package.new
26
+ book.each_with_pagename do |name, sheet|
27
+ package.workbook.add_worksheet(name: sanitize_sheet_name(name)) do |ws|
28
+ next if sheet.first_row.nil? || sheet.last_row.nil?
29
+
30
+ (sheet.first_row..sheet.last_row).each do |row_index|
31
+ ws.add_row(Array(sheet.row(row_index)))
32
+ end
33
+ end
34
+ end
35
+
36
+ package.serialize(tempfile.path)
37
+ tempfile.rewind
38
+ tempfile
39
+ end
40
+
41
+ private
42
+
43
+ def sanitize_sheet_name(name)
44
+ cleaned = name.to_s.gsub(%r{[\\/*?:\[\]]}, "_")
45
+ cleaned = "Sheet1" if cleaned.strip.empty?
46
+ cleaned[0, 31]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "spreadsheet_encrypt/version"
4
+ require_relative "spreadsheet_encrypt/errors"
5
+ require_relative "spreadsheet_encrypt/encryptor"
6
+
7
+ module SpreadsheetEncrypt
8
+ # Encrypt a spreadsheet file with an open password.
9
+ #
10
+ # Positional:
11
+ # SpreadsheetEncrypt.encrypt(input_path, password, output_path)
12
+ #
13
+ # Keyword:
14
+ # SpreadsheetEncrypt.encrypt(input:, password:, output:)
15
+ #
16
+ # @return [String] actual output path written
17
+ def self.encrypt(*args, **kwargs)
18
+ input, password, output = resolve_args(args, kwargs)
19
+ Encryptor.encrypt(input, password, output)
20
+ end
21
+
22
+ def self.resolve_args(args, kwargs)
23
+ if kwargs.any?
24
+ missing = %i[input password output] - kwargs.keys
25
+ raise ArgumentError, "missing keywords: #{missing.join(', ')}" if missing.any?
26
+
27
+ return [kwargs[:input], kwargs[:password], kwargs[:output]]
28
+ end
29
+
30
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 3)" unless args.size == 3
31
+
32
+ args
33
+ end
34
+ private_class_method :resolve_args
35
+ end
@@ -0,0 +1,4 @@
1
+ module SpreadsheetEncrypt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadsheet_encrypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 汤宇浩
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2026-09-04 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: base64
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: caxlsx
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: csv
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: ooxml_crypt
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.1'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.1'
68
+ - !ruby/object:Gem::Dependency
69
+ name: roo
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.10'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.10'
82
+ - !ruby/object:Gem::Dependency
83
+ name: roo-xls
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.2'
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.2'
96
+ description: |
97
+ Encrypt existing Excel files with Office Open XML password protection.
98
+ .xlsx files are encrypted directly; .xls files are converted to .xlsx then encrypted.
99
+ email:
100
+ - tangyuhao@rippletek.com
101
+ executables:
102
+ - spreadsheet_encrypt
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - CHANGELOG.md
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - exe/spreadsheet_encrypt
111
+ - lib/spreadsheet_encrypt.rb
112
+ - lib/spreadsheet_encrypt/encryptor.rb
113
+ - lib/spreadsheet_encrypt/errors.rb
114
+ - lib/spreadsheet_encrypt/version.rb
115
+ - lib/spreadsheet_encrypt/xls_converter.rb
116
+ - sig/spreadsheet_encrypt.rbs
117
+ homepage: https://github.com/tom0932/spreadsheet_encrypt
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ allowed_push_host: https://rubygems.org
122
+ homepage_uri: https://github.com/tom0932/spreadsheet_encrypt
123
+ source_code_uri: https://github.com/tom0932/spreadsheet_encrypt
124
+ changelog_uri: https://github.com/tom0932/spreadsheet_encrypt/blob/main/CHANGELOG.md
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 3.2.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.6.2
140
+ specification_version: 4
141
+ summary: Encrypt Excel spreadsheet files (.xls / .xlsx) with an open password.
142
+ test_files: []