Class: FXlsx::File

Inherits:
Object
  • Object
show all
Defined in:
lib/f_xlsx/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closedObject

Returns the value of attribute closed.



3
4
5
# File 'lib/f_xlsx/file.rb', line 3

def closed
  @closed
end

#has_fileObject

Returns the value of attribute has_file.



3
4
5
# File 'lib/f_xlsx/file.rb', line 3

def has_file
  @has_file
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/f_xlsx/file.rb', line 3

def id
  @id
end

Class Method Details

.new_fileObject



5
6
7
8
9
10
# File 'lib/f_xlsx/file.rb', line 5

def self.new_file
  XlsxExt.load_lib
  instance = File.new
  instance.id = XlsxExt.newFile()
  instance
end

.open_file(path) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/f_xlsx/file.rb', line 12

def self.open_file(path)
  XlsxExt.load_lib
  instance = File.new
  instance.id = XlsxExt.openFile(path)
  instance.has_file = true
  instance
end

Instance Method Details

#closeObject



174
175
176
177
178
# File 'lib/f_xlsx/file.rb', line 174

def close
  raise "file closed" if self.closed?
  XlsxExt.closeFile(self.id)
  self.closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/f_xlsx/file.rb', line 20

def closed?
  closed
end

#delete_sheet(sheet_name) ⇒ Object



48
49
50
51
52
# File 'lib/f_xlsx/file.rb', line 48

def delete_sheet(sheet_name)
  raise "file closed" if self.closed?
  
  XlsxExt.deleteSheet(self.id, sheet_name)
end

#get_cell_value(sheet_name, row, col) ⇒ Object



89
90
91
92
93
# File 'lib/f_xlsx/file.rb', line 89

def get_cell_value(sheet_name, row, col)
  raise "file closed" if self.closed?
  
  XlsxExt.getCellValue(self.id, sheet_name, row, col)
end

#get_merge_cells(sheet_name) ⇒ Object



156
157
158
159
# File 'lib/f_xlsx/file.rb', line 156

def get_merge_cells(sheet_name)
  raise "file closed" if self.closed?
  XlsxExt.getMergeCells(self.id, sheet_name).values
end

#get_rows(sheet_name) ⇒ Object



95
96
97
98
99
# File 'lib/f_xlsx/file.rb', line 95

def get_rows(sheet_name)
  raise "file closed" if self.closed?
  
  XlsxExt.getRows(self.id, sheet_name).value
end

#get_sheet_listObject



54
55
56
57
58
59
# File 'lib/f_xlsx/file.rb', line 54

def get_sheet_list
  raise "file closed" if self.closed?
  
  ptr =  XlsxExt.getSheetList(self.id)
  ptr.value
end

#get_sheet_name(index) ⇒ Object



61
62
63
64
65
# File 'lib/f_xlsx/file.rb', line 61

def get_sheet_name(index)
  raise "file closed" if self.closed?
  
  XlsxExt.getSheetName(self.id, index)
end

#get_sheet_visible(sheet_name) ⇒ Object



42
43
44
45
46
# File 'lib/f_xlsx/file.rb', line 42

def get_sheet_visible(sheet_name)
  raise "file closed" if self.closed?

  XlsxExt.getSheetVisible(self.id, sheet_name) == 1
end

#merge_cell(sheet_name, start_row, start_col, end_row, end_col) ⇒ Object



144
145
146
147
148
# File 'lib/f_xlsx/file.rb', line 144

def merge_cell(sheet_name, start_row, start_col, end_row, end_col)
  raise "file closed" if self.closed?
  
  XlsxExt.mergeCell(self.id, sheet_name, start_row, start_col, end_row, end_col)
end

#new_sheet(sheet_name) ⇒ Object



24
25
26
27
28
# File 'lib/f_xlsx/file.rb', line 24

def new_sheet(sheet_name)
  raise "file closed" if self.closed?
  
  XlsxExt.newSheet(self.id, sheet_name)
end

#put_row(sheet_name, row_index, row) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/f_xlsx/file.rb', line 101

def put_row(sheet_name, row_index, row)
  raise "file closed" if self.closed?
  
  # 不需要手动释放
  str = CStrArray.new
  str[:s_size] = row.size
  ptr = FFI::MemoryPointer.new(:pointer, row.size)
  pps = row.map{|x| FFI::MemoryPointer.from_string(x.to_s) }
  ptr.write_array_of_pointer(pps)
  str[:arr] = ptr
  XlsxExt.putRow(self.id, sheet_name, row_index,str)
  pps.each(&:free)
  ptr.free
end

#put_rows(sheet_name, rows) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/f_xlsx/file.rb', line 116

def put_rows(sheet_name, rows)
  raise "file closed" if self.closed?
  #不需要手动释放
  str2 = CStrArray2.new
  str2[:s_size] = rows.size
  ptr2 = FFI::MemoryPointer.new(:pointer, rows.size)
  todoRelease = [ptr2]
  
  ptr2_arr = rows.map do |row|
    # 不需要手动释放
    str = CStrArray.new
    str[:s_size] = row.size
  
    ptr = FFI::MemoryPointer.new(:pointer, row.size)
    todoRelease << ptr
    sptrs = row.map{|s| FFI::MemoryPointer.from_string(s.to_s)}
    todoRelease += sptrs
    ptr.write_array_of_pointer(sptrs)
    str[:arr] = ptr
    str.pointer
  end
  todoRelease += ptr2_arr
  ptr2.write_array_of_pointer(ptr2_arr)
  str2[:arr] = ptr2
  XlsxExt.putRows(self.id, sheet_name, str2)
  todoRelease.each(&:free)
end

#saveObject



161
162
163
164
165
166
# File 'lib/f_xlsx/file.rb', line 161

def save
  raise "file closed" if self.closed?
  raise "new file can't save, call save_as" unless has_file
  XlsxExt.save(self.id)
  self.closed = true
end

#save_as(path) ⇒ Object



168
169
170
171
172
# File 'lib/f_xlsx/file.rb', line 168

def save_as(path)
  raise "file closed" if self.closed?
  XlsxExt.saveAs(self.id,path)
  self.closed = true
end

#set_cell_value(sheet_name, row, col, value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/f_xlsx/file.rb', line 68

def set_cell_value(sheet_name, row, col, value)
  raise "file closed" if self.closed?
  
  value = '' if value.nil?
  ptr = nil
  typ = if value.is_a?(Integer)
    ptr = FFI::MemoryPointer.new(:int)
    ptr.write_int(value)
    1
  elsif value.is_a?(Float)
    ptr = FFI::MemoryPointer.new(:float)
    ptr.write_float(value)
    2
  else
    ptr = FFI::MemoryPointer.from_string(value.to_s)
    3
  end
  XlsxExt.setCellValue(self.id, sheet_name, row, col, ptr, typ)
  ptr.free if ptr
end

#set_sheet_name(sheet_name, target_sheet_name) ⇒ Object



30
31
32
33
34
# File 'lib/f_xlsx/file.rb', line 30

def set_sheet_name(sheet_name, target_sheet_name)
  raise "file closed" if self.closed?
  
  XlsxExt.setSheetName(self.id, sheet_name, target_sheet_name)
end

#set_sheet_visible(sheet_name, visible) ⇒ Object



36
37
38
39
40
# File 'lib/f_xlsx/file.rb', line 36

def set_sheet_visible(sheet_name, visible)
  raise "file closed" if self.closed?

  XlsxExt.setSheetVisible(self.id, sheet_name, visible ? 1 : 0)
end

#unmerge_cell(sheet_name, start_row, start_col, end_row, end_col) ⇒ Object



150
151
152
153
154
# File 'lib/f_xlsx/file.rb', line 150

def unmerge_cell(sheet_name, start_row, start_col, end_row, end_col)
  raise "file closed" if self.closed?
  
  XlsxExt.unMergeCell(self.id, sheet_name, start_row, start_col, end_row, end_col)
end