Class: TalltyImportExport::ExportForm

Inherits:
Export
  • Object
show all
Defined in:
lib/tallty_import_export/export_form.rb

Instance Attribute Summary

Attributes inherited from Export

#context, #klass

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Export

#export_headers, #export_options, #export_xlsx, #handle_data, #handle_data_type, #handle_format, #handle_select, #process_options, #try_chain, #try_method, #with_scope

Constructor Details

#initialize(*attrs) ⇒ ExportForm

Returns a new instance of ExportForm.



3
4
5
# File 'lib/tallty_import_export/export_form.rb', line 3

def initialize *attrs
  super(*attrs)
end

Class Method Details

.export_template_xlsx(form) ⇒ Object



8
9
10
11
12
13
# File 'lib/tallty_import_export/export_form.rb', line 8

def export_template_xlsx form
  export_xlsx([], {
    headers: form_transfer_to_headers(form),
    header_only: true
  })
end

Instance Method Details

#export_headers_result(**options) ⇒ Object



127
128
129
130
131
# File 'lib/tallty_import_export/export_form.rb', line 127

def export_headers_result **options
  @headers = options.symbolize_keys[:headers]
  @headers = super(**options)
  @headers = TalltyImportExport::Attr::ExportHeader.new({ items: @headers })
end

#export_workbook(workbook, association_records, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
119
120
121
122
123
124
# File 'lib/tallty_import_export/export_form.rb', line 16

def export_workbook workbook, association_records, **options
  # excel导出样式
  alignment = { vertical: :center, horizontal: :center }
  border    = { color: '969696', style: :thin }
  title1 = workbook.styles.add_style(alignment: alignment, border: border, sz: 12, b: true)
  title2 = workbook.styles.add_style(alignment: alignment, border: border, bg_color: "2a5caa",  sz: 12, fg_color: "fffffb")
  title3 = workbook.styles.add_style(alignment: alignment.merge(wrap_text: true), border: border, sz: 10)
  _sheet_name = (respond_to?(:sheet_name) ? self.sheet_name : nil) || options[:sheet_name]

  header_obj = export_headers_result **options

  workbook.add_worksheet(name: _sheet_name) do |sheet|
    index = 0

    if respond_to?(:first_header)
      row_index = Axlsx.col_ref(headers.size - 1)
      sheet.merge_cells("A1:#{row_index}1")
      sheet.add_row [first_header], style: title1, height: 30
      index += 1
    end

    header_obj.header_lines.each do |header_line|
      sheet.add_row(header_line.map(&:name), style: title2, height: 25)
      index += 1
    end

    # 合并相同 header
    header_obj.header_seq_to_axios.values.each do |axios_ary|
      if axios_ary.count > 1
        top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
        bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
        sheet.merge_cells(
          Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
        )
      end
    end

    return if options[:header_only]

    value_seq_to_axios = {}

    formats = []
    association_records.each do |association_record|
      records = @each_method.present? ?
        (try_method(association_record, @each_method) || [nil]) :
        [association_record]

      records.each do |record|
        value_living_alone_col_index_to_value_count = {}

        payload = TalltyImportExport::ExportPayload.new(record, header: header_obj) do |payload, header, **opts|
          _data = header[:source] ?
            handle_data(association_record, header, index, **opts) :
            handle_data(payload, header, index, **opts)
        end


        # p '----------------------------------------------------------------'
        # payload.lines.each { |x| p x.map { |x| x.try(:value) || '________' } }

        payload.lines.each_with_index do |line, row_index|
          row = []
          line.each_with_index do |value, col_index|
            value_living_alone_col_index_to_value_count[col_index] ||= 0

            if (TalltyImportExport::ExportPayload::Value === value)
              row << value.value
              value_living_alone_col_index_to_value_count[col_index] += 1
              unless value_seq_to_axios[value.seq]
                value_seq_to_axios[value.seq] = []
              end
              value_seq_to_axios[value.seq] << [col_index , row_index + index]
            else
              row << nil
            end
            formats.push(
              header_obj.flatten_value[col_index].format&.to_sym || (row.last.is_a?(String) ? :string : nil)
            )
          end
          sheet.add_row(row, style: title3, height: @row_height, types: formats)
          formats = []

        end

        # 合并仅有一个值的一列中所有格子
        value_living_alone_col_index_to_value_count.each_pair do |col_index, count|
          if count == 1
            sheet.merge_cells(
              Axlsx::cell_r(col_index, index) + ':' + Axlsx::cell_r(col_index, index + payload.max_matrix_height - 1)
            )
          end
        end

        index += payload.max_matrix_height
      end
    end

    # 合并相同值
    value_seq_to_axios.values.each do |axios_ary|
      if axios_ary.count > 1
        top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
        bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
        sheet.merge_cells(
          Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
        )
      end
    end
  end
end