Class: Xlsxrb::Ooxml::WorksheetParser
- Inherits:
-
Object
- Object
- Xlsxrb::Ooxml::WorksheetParser
- Defined in:
- lib/xlsxrb/ooxml/worksheet_parser.rb,
sig/generated/xlsxrb/ooxml/worksheet_parser.rbs
Overview
Streaming parser for xl/worksheets/sheetN.xml. Uses a fast string-scanning approach for row/cell extraction, falling back to REXML SAX only for column definitions and unmapped data.
Defined Under Namespace
Classes: ColumnsListener
Constant Summary collapse
- EMPTY_ARRAY =
[].freeze
- EMPTY_HASH =
{}.freeze
- XML_ENTITIES =
{ "&" => "&", "<" => "<", ">" => ">", """ => '"', "'" => "'" }.freeze
Class Method Summary collapse
-
.each_event(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) ⇒ void
Yields Event objects for columns, rows, cells, and hyperlinks.
-
.each_row(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) ⇒ void
Streaming parse: yields one raw row hash at a time.
- .fast_scan_cells_direct(xml, from, to, shared_strings, row_source, &block) ⇒ void
-
.parse(xml_string, shared_strings: []) ⇒ Object
Parses all rows from a worksheet XML string.
-
.parse_columns(xml_string, part_name: "xl/worksheets/sheet1.xml") ⇒ Object
Parses column definitions (
) from a worksheet.
Class Method Details
.each_event(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) ⇒ void
This method returns an undefined value.
Yields Event objects for columns, rows, cells, and hyperlinks.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/xlsxrb/ooxml/worksheet_parser.rb', line 57 def self.each_event(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) return enum_for(:each_event, xml_string, shared_strings: shared_strings, part_name: part_name) unless block return if xml_string.nil? || xml_string.empty? # 1. Parse and yield column events if xml_string.include?("<cols") listener = ColumnsListener.new XmlParser.parse(xml_string, listener) listener.columns.each do |col| block.call(Event.new( type: :column, args: [col[:min], col[:max], col[:width], col[:hidden], col[:custom_width], col[:outline_level]], source: { part: part_name } )) end end # 2. Parse and yield row/cell events fast_scan_events(xml_string, shared_strings, part_name, &block) # 3. Parse and yield hyperlink events return unless xml_string.include?("hyperlink") xml = xml_string.b hpos_term = xml.index("hyperlinks") return unless hpos_term hpos = xml.rindex("<", hpos_term) return unless hpos h_end_term = xml.index("/hyperlinks", hpos) h_end = h_end_term ? xml.index(">", h_end_term) : xml.size h_end ||= xml.size pos = hpos while pos < h_end hl_start_term = xml.index("hyperlink", pos) break unless hl_start_term && hl_start_term < h_end hl_start = xml.rindex("<", hl_start_term) break unless hl_start && hl_start < h_end hl_tag_end = xml.index(">", hl_start + 1) break unless hl_tag_end hl_tag = xml.byteslice(hl_start, hl_tag_end - hl_start) ref = tag_attr(hl_tag, ' ref="') rid = tag_attr(hl_tag, ' r:id="') || tag_attr(hl_tag, ' id="') display = tag_attr(hl_tag, ' display="') tooltip = tag_attr(hl_tag, ' tooltip="') location = tag_attr(hl_tag, ' location="') if ref block.call(Event.new( type: :hyperlink, args: [ref, rid, display, tooltip, location], source: { part: part_name, cell: ref } )) end pos = hl_tag_end + 1 end end |
.each_row(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) ⇒ void
This method returns an undefined value.
Streaming parse: yields one raw row hash at a time.
28 29 30 31 32 33 |
# File 'lib/xlsxrb/ooxml/worksheet_parser.rb', line 28 def self.each_row(xml_string, shared_strings: [], part_name: "xl/worksheets/sheet1.xml", &block) return enum_for(:each_row, xml_string, shared_strings: shared_strings, part_name: part_name) unless block return if xml_string.nil? || xml_string.empty? fast_scan_rows_direct(xml_string, shared_strings, part_name, &block) end |
.fast_scan_cells_direct(xml, from, to, shared_strings, row_source, &block) ⇒ void
This method returns an undefined value.
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/xlsxrb/ooxml/worksheet_parser.rb', line 544 def self.fast_scan_cells_direct(xml, from, to, shared_strings, row_source, &block) pos = from cell_count = 0 while pos < to c_start = xml.index("<c", pos) break unless c_start && c_start < to nb = xml.getbyte(c_start + 2) unless [32, 62, 9, 10, 13, 47].include?(nb) pos = c_start + 2 next end c_tag_end = xml.index(">", c_start + 2) break unless c_tag_end # Zero-allocation attribute scan directly within the tag type = nil style_index = nil col_idx = nil row_idx = nil ai = c_start + 2 while ai < c_tag_end b = xml.getbyte(ai) if [32, 9, 10, 13].include?(b) ai += 1 next end if b == 114 && xml.getbyte(ai + 1) == 61 && xml.getbyte(ai + 2) == 34 # r=" ai += 3 col_idx = 0 while ai < c_tag_end cb = xml.getbyte(ai) if cb.between?(65, 90) col_idx = (col_idx * 26) + (cb - 64) ai += 1 elsif cb.between?(97, 122) col_idx = (col_idx * 26) + (cb - 96) ai += 1 else break end end col_idx -= 1 row_idx = 0 while ai < c_tag_end cb = xml.getbyte(ai) break unless cb.between?(48, 57) row_idx = (row_idx * 10) + (cb - 48) ai += 1 end row_idx -= 1 ai += 1 if xml.getbyte(ai) == 34 elsif b == 116 && xml.getbyte(ai + 1) == 61 && xml.getbyte(ai + 2) == 34 # t=" ai += 3 type_b = xml.getbyte(ai) if type_b == 115 && xml.getbyte(ai + 1) == 34 # "s" type = "s" ai += 2 elsif type_b == 98 && xml.getbyte(ai + 1) == 34 # "b" type = "b" ai += 2 elsif type_b == 101 && xml.getbyte(ai + 1) == 34 # "e" type = "e" ai += 2 else t_end = xml.index('"', ai) type = xml.byteslice(ai, t_end - ai) if t_end ai = t_end ? t_end + 1 : c_tag_end end elsif b == 115 && xml.getbyte(ai + 1) == 61 && xml.getbyte(ai + 2) == 34 # s=" ai += 3 style_index = 0 while ai < c_tag_end cb = xml.getbyte(ai) break unless cb.between?(48, 57) style_index = (style_index * 10) + (cb - 48) ai += 1 end ai += 1 if xml.getbyte(ai) == 34 else ai += 1 end end # Self-closing <c ... /> if xml.getbyte(c_tag_end - 1) == 47 cell_obj = Elements::Cell.new( row_index: row_idx || row_source[:row], column_index: col_idx || cell_count, value: nil, style_index: style_index, errors: Elements::EMPTY_ERRORS ) cell_count += 1 block.call(cell_obj) pos = c_tag_end + 1 next end c_end = xml.index("</c>", c_tag_end + 1) break unless c_end # Parse cell content sequentially (bounded to c_end - avoid unbounded scans) value = nil formula = nil inline_str = nil cpos = c_tag_end + 1 while cpos < c_end tag_pos = xml.index("<", cpos) break unless tag_pos && tag_pos < c_end tag_char = xml.getbyte(tag_pos + 1) case tag_char when 118 # 'v' if xml.getbyte(tag_pos + 2) == 62 # <v> v_val_start = tag_pos + 3 v_end = xml.index("</v>", v_val_start) if v_end if type == "s" # Zero-allocation integer parse for SST index s_idx = 0 v_i = v_val_start while v_i < v_end s_idx = (s_idx * 10) + (xml.getbyte(v_i) - 48) v_i += 1 end value = shared_strings[s_idx] || "" elsif type == "b" value = xml.getbyte(v_val_start) == 49 # '1' else raw_value = xml.byteslice(v_val_start, v_end - v_val_start) value = resolve_fast_value(raw_value, type, shared_strings) end cpos = v_end + 4 else cpos = tag_pos + 3 end elsif xml.getbyte(tag_pos + 2) == 47 && xml.getbyte(tag_pos + 3) == 62 # <v/> cpos = tag_pos + 4 else cpos = tag_pos + 2 end when 102 # 'f' f_tag_end = xml.index(">", tag_pos + 2) if f_tag_end && f_tag_end < c_end if xml.getbyte(f_tag_end - 1) == 47 # self-closing <f ... /> cpos = f_tag_end + 1 else f_end = xml.index("</f>", f_tag_end + 1) if f_end && f_end <= c_end formula = xml.byteslice(f_tag_end + 1, f_end - f_tag_end - 1).force_encoding("UTF-8") formula = decode_xml_entities(formula) if formula.include?("&") cpos = f_end + 4 else cpos = f_tag_end + 1 end end else cpos = tag_pos + 2 end when 105 # 'i' - <is> if xml.byteslice(tag_pos, 4) == "<is>" is_end = xml.index("</is>", tag_pos + 4) if is_end && is_end <= c_end inline_str = extract_inline_text(xml, tag_pos + 4, is_end) cpos = is_end + 5 else cpos = tag_pos + 4 end else cpos = tag_pos + 2 end else # Skip unknown tag close = xml.index(">", tag_pos + 1) cpos = close ? close + 1 : c_end end end val_to_use = inline_str || value cell_obj = Elements::Cell.new( row_index: row_idx || row_source[:row], column_index: col_idx || cell_count, value: val_to_use, formula: formula, style_index: style_index, errors: Elements::EMPTY_ERRORS ) cell_count += 1 block.call(cell_obj) pos = c_end + 4 end end |
.parse(xml_string, shared_strings: []) ⇒ Object
Parses all rows from a worksheet XML string. Returns an Array of raw row hashes:
{ index:, cells: [{ ref:, type:, style_index:, value:, formula: }], attrs:, unmapped: }
19 20 21 22 23 24 25 |
# File 'lib/xlsxrb/ooxml/worksheet_parser.rb', line 19 def self.parse(xml_string, shared_strings: []) return [] if xml_string.nil? || xml_string.empty? rows = [] each_row(xml_string, shared_strings: shared_strings) { |row| rows << row } rows end |
.parse_columns(xml_string, part_name: "xl/worksheets/sheet1.xml") ⇒ Object
Parses column definitions (
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xlsxrb/ooxml/worksheet_parser.rb', line 36 def self.parse_columns(xml_string, part_name: "xl/worksheets/sheet1.xml") return [] if xml_string.nil? || xml_string.empty? columns = [] each_event(xml_string, part_name: part_name) do |event| next unless event.type == :column min, max, width, hidden, custom_width, outline_level = event.args columns << { min: min, max: max, width: width, hidden: hidden, custom_width: custom_width, outline_level: outline_level } end columns end |