File: excel.rb

package info (click to toggle)
ruby-spreadsheet 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,980 kB
  • sloc: ruby: 6,939; makefile: 10
file content (89 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
require "spreadsheet"

##
# Spreadsheet::Excel Compatibility Layer.
# Drop-in replacement for Spreadsheet::Excel version <= 0.3.5.1
module Spreadsheet
  module Excel
    class ExcelCompatibleWorkbook < Workbook
      def initialize file_path, *args
        super(*args)
        @file_path = file_path
      end

      def close
        write @file_path
      end
    end

    def self.new file_path
      ExcelCompatibleWorkbook.new file_path
    end

    class Workbook
      def add_worksheet name
        if name.is_a? String
          create_worksheet name: name
        else
          super
        end
      end
    end
  end

  class Worksheet
    unless instance_methods.include? "new_format_column"
      alias_method :new_format_column, :format_column
      def format_column column, width = nil, format = nil
        if width.is_a? Format
          new_format_column column, width, format
        else
          new_format_column column, format, width: width
        end
      end
    end
    def write row, col, data = nil, format = nil
      if data.is_a? Array
        write_row row, col, data, format
      else
        row = row(row)
        row[col] = data
        row.set_format col, format
      end
    end

    def write_column row, col, data = nil, format = nil
      if data.is_a? Array
        data.each do |token|
          if token.is_a? Array
            write_row row, col, token, format
          else
            write row, col, token, format
          end
          row += 1
        end
      else
        write row, col, data, format
      end
    end

    def write_row row, col, data = nil, format = nil
      if data.is_a? Array
        data.each do |token|
          if token.is_a? Array
            write_column row, col, token, format
          else
            write row, col, token, format
          end
          col += 1
        end
      else
        write row, col, data, format
      end
    end

    def write_url row, col, url, string = url, format = nil
      row(row)[col] = Link.new url, string
    end
  end
end