Skip to content

Commit

Permalink
Improve sheets methods and upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
d1ceward committed Aug 1, 2024
1 parent 8d8d5ac commit 81224b1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
5 changes: 3 additions & 2 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Globs:
- 'src/**/*.cr'
- 'spec/**/*.cr'
- '**/*.cr'
- '!lib'
- '!tmp'

Lint/Formatting:
Enabled: false
Expand Down
2 changes: 1 addition & 1 deletion .crystal-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.0
1.13.1
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
lints:
name: Lints
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -22,7 +23,7 @@ jobs:
with:
crystal: ${{ env.CRYSTAL_VERSION }}
- name: Crystal shard cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: lib
key: ${{ runner.os }}-shards-${{ hashFiles('**/shard.lock') }}
Expand All @@ -33,6 +34,7 @@ jobs:
tests:
name: Tests
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -43,7 +45,7 @@ jobs:
with:
crystal: ${{ env.CRYSTAL_VERSION }}
- name: Crystal shard cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: lib
key: ${{ runner.os }}-${{ matrix.crystal }}-shards-${{ hashFiles('**/shard.lock') }}
Expand Down
18 changes: 9 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Ignore crystal dependencies and generated binaries
/lib
/tmp
lib/
tmp/

# Ignore binaries
/bin/*
!/bin/ameba.cr
!/bin/coverage
!/bin/release
bin/*
!bin/ameba.cr
!bin/coverage
!bin/release

# Ignore coverage files
/coverage
/run_tests.cr
/run_tests
coverage/
run_tests.cr
run_tests
47 changes: 47 additions & 0 deletions spec/xlsx-parser/book_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,58 @@ describe XlsxParser::Book do
end
end

describe "#sheet_exists?" do
it "returns true if sheet with given name exists" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
book.sheet_exists?("Sheet1").should be_truthy
book.close
end

it "returns false if sheet with given name does not exist" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
book.sheet_exists?("NonexistentSheet").should be_falsey
book.close
end
end

describe "#sheet" do
it "returns sheet with given name" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
sheet = book.sheet("Sheet1")
sheet.should be_a(XlsxParser::Sheet)
sheet.try(&.name).should eq("Sheet1")
book.close
end

it "returns nil if sheet with given name does not exist" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
sheet = book.sheet("NonexistentSheet")
sheet.should be_nil
book.close
end
end

describe "#close" do
it "closes the previously opened file" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
book.close
book.zip.closed?.should be_truthy
end
end

describe "#style_types" do
it "return an array of Sheet class instance" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
book.style_types
book.close
end
end

describe "#base_time" do
it "returns the base time from workbookPr" do
book = XlsxParser::Book.new("./spec/fixtures/sample.xlsx")
book.base_time.should eq(Time.utc(1899, 12, 30))
book.close
end
end
end
40 changes: 8 additions & 32 deletions src/xlsx-parser/book.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,22 @@ module XlsxParser
rels = XML.parse(@zip["xl/_rels/workbook.xml.rels"].open(&.gets_to_end))

@sheets = sheets_nodes.map do |sheet|
sheetname = sheet["name"]
sheetfile = rels.xpath_string(
sheet_file = rels.xpath_string(
"string(//*[name()='Relationship' and contains(@Id,'#{sheet["id"]}')]/@Target)"
)
Sheet.new(self, sheetfile, sheetname)
end
end

# Return True if the given sheet name appears in the workbook
def sheetname?(search_name : String)
this_sheet = sheetname search_name
if this_sheet.nil?
false
else
true
Sheet.new(self, sheet_file, sheet["name"])
end
end

# Return the Worksheet with the given name, or nil if that sheet name
# can't be found in the workbook
def sheetname(search_name : String)
found_sheet = nil
@sheets.each do | sheet |
if sheet.name == search_name
found_sheet = sheet
break
end
end

found_sheet
# Return true if sheet with given name exists otherwise return false
def sheet_exists?(name : String)
sheets.any? { |sheet| sheet.name == name }
end

# Return an array of all sheet names in the current workbook
def sheetnames
name_list = [] of String
@sheets.each do | sheet |
name_list << sheet.name
end

name_list
# Return sheet with given name
def sheet(name : String) : Sheet?
sheets.find { |sheet| sheet.name == name }
end

# Close previously opened given file/filename
Expand Down

0 comments on commit 81224b1

Please sign in to comment.