<commit_before>module ODFReport

class Spreadsheet
  include Nested

  @@id_target = {}
  @@name_id = {}
  @sheet_id = {}

  def initialize(opts)

    @name             = opts[:name]
    @collection       = opts[:collection]

    @series           = opts[:series] || nil
    @title            = opts[:title] || nil
    @type             = opts[:type] || nil

  end

  def replace!(doc, filename, row = nil)

    case @type

    when 'pie', 'doughnut' # For Pie Charts

      @series = [@series] unless @series.is_a? Array
      @collection.each { |k, v| @collection[k] = [v] } unless @collection.values.first.is_a? Array

    when 'waterfall' # For Waterfall Charts

      etl_waterfall unless @series == ['Fill', 'Base', 'Rise+', 'Rise-', 'Fall+', 'Fall-']

    when 'bar', 'column' # For Bar/Column Charts

      if @series.length < @collection.values.first.length # If number of series names doesn't match the collection values

        @series << rand(65..91).chr until @series.length == @collection.values.first.length

      elsif @series.length < @collection.values.first.length

        @series.pop until @series.length == @collection.values.first.length

      end

    end

    rows = @collection.length + 1
    cols = @series.length + 1

    if /worksheets/ === filename

      doc.xpath("//xmlns:row").remove
      doc.xpath("//xmlns:dimension").first['ref'] = "A1:#{(cols + 64).chr}#{rows}"

      rows.times do |row|
        node = "<row r=\"#{row + 1}\" spans=\"1:4\">"
        doc.xpath("//xmlns:sheetData").first.add_child(node)
      end

      si = 0

      rows.times do |row|

        cols.times do |col|

          if row == 0 or col == 0

            s_node = "<c r=\"#{(col + 65).chr}#{row + 1}\" t=\"s\"><v>#{si}</v></c>"
            doc.xpath("//xmlns:row")[row].add_child(s_node)
            si += 1

          else

            val = @collection.values[row - 1][col - 1]
            c_node = "<c r=\"#{(col + 65).chr}#{row + 1}\"><v>#{val}</v></c>"
            doc.xpath("//xmlns:row")[row].add_child(c_node)

          end

        end

      end

    elsif /tables/ === filename

      doc.xpath("//xmlns:table").first['ref'] = "A1:#{(cols + 64).chr}#{rows}"
      doc.xpath("//xmlns:tableColumns").first['count'] = @series.length + 1

      node = doc.xpath("//xmlns:tableColumn").first
      doc.xpath("//xmlns:tableColumn").remove
      doc.xpath("//xmlns:tableColumns").first.add_child(node)
<commit_msg>think i fixed the excel error<commit_after>module ODFReport

class Spreadsheet
  include Nested

  @@id_target = {}
  @@name_id = {}
  @sheet_id = {}

  def initialize(opts)

    @name             = opts[:name]
    @collection       = opts[:collection]

    @series           = opts[:series] || nil
    @title            = opts[:title] || nil
    @type             = opts[:type] || nil

  end

  def replace!(doc, filename, row = nil)

    case @type

    when 'pie', 'doughnut' # For Pie Charts

      @series = [@series] unless @series.is_a? Array
      @collection.each { |k, v| @collection[k] = [v] } unless @collection.values.first.is_a? Array

    when 'waterfall' # For Waterfall Charts

      etl_waterfall unless @series == ['Fill', 'Base', 'Rise+', 'Rise-', 'Fall+', 'Fall-']

    when 'bar', 'column', 'line' # For Bar/Column/Line Charts

      if @series.length < @collection.values.first.length # If number of series names doesn't match the collection values

        @series << rand(65..91).chr until @series.length == @collection.values.first.length

      elsif @series.length < @collection.values.first.length

        @series.pop until @series.length == @collection.values.first.length

      end

    end

    rows = @collection.length + 1
    cols = @series.length + 1

    if /worksheets/ === filename

      doc.xpath("//xmlns:row").remove
      doc.xpath("//xmlns:dimension").first['ref'] = "A1:#{(cols + 64).chr}#{rows}"

      rows.times do |row|
        node = "<row r=\"#{row + 1}\" spans=\"1:4\">"
        doc.xpath("//xmlns:sheetData").first.add_child(node)
      end

      si = 0

      rows.times do |row|

        cols.times do |col|

          if row == 0 or col == 0

            s_node = "<c r=\"#{(col + 65).chr}#{row + 1}\" t=\"s\"><v>#{si}</v></c>"
            doc.xpath("//xmlns:row")[row].add_child(s_node)
            si += 1

          else

            val = @collection.values[row - 1][col - 1]
            val = 0 if @collection.values[row - 1][col - 1].nil?
            c_node = "<c r=\"#{(col + 65).chr}#{row + 1}\"><v>#{val}</v></c>"
            doc.xpath("//xmlns:row")[row].add_child(c_node)

          end

        end

      end

    elsif /tables/ === filename

      doc.xpath("//xmlns:table").first['ref'] = "A1:#{(cols + 64).chr}#{rows}"
      doc.xpath("//xmlns:tableColumns").first['count'] = @series.length + 1

      node = doc.xpath("//xmlns:tableColumn").first
      doc.xpath("//xmlns:tableColumn").remove
      doc.xpath("//xmlns:tableColumns").first.add_child(node)

{'fraction_non_alphanumeric': 0.18329979879275654, 'fraction_numerical': 0.01187122736418511, 'mean_word_length': 2.2682445759368837, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '790916', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1639, 'n_words': 354}