Skip to content

Commit

Permalink
Merge pull request #5 from PowellDean/sheetnames
Browse files Browse the repository at this point in the history
Add the ability for worksheet names to be stored.
  • Loading branch information
d1ceward authored Aug 1, 2024
2 parents 7fddb25 + 3b91d0d commit 8d8d5ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/xlsx-parser/book.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,48 @@ 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(
"string(//*[name()='Relationship' and contains(@Id,'#{sheet["id"]}')]/@Target)"
)
Sheet.new(self, sheetfile)
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
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
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
end

# Close previously opened given file/filename
def close
@zip.close
Expand Down
3 changes: 2 additions & 1 deletion src/xlsx-parser/sheet.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module XlsxParser
class Sheet
getter node : XML::Reader
getter name : String

# All possible output types
alias Type = Bool | Float64 | Int32 | String | Time

def initialize(@book : Book, @file : String)
def initialize(@book : Book, @file : String, @name : String)
@node = XML::Reader.new(@book.zip["xl/#{@file}"].open(&.gets_to_end))
end

Expand Down

0 comments on commit 8d8d5ac

Please sign in to comment.