Method: MyMatrix#to_t

Defined in:
lib/mymatrix.rb

#to_t(outFile = nil, opts = {}) ⇒ Object

テキスト出力する。outFileにxlsが指定された場合は、xls出力する。



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/mymatrix.rb', line 414

def to_t(outFile=nil, opts={})
  if(!outFile)
    outFile = @file
  end
  
  #拡張子の判別
  ext = File.extname(outFile).downcase
  case ext
  when '.csv'
    opts[:separator] ||= ','
    opts[:escape] ||= true
  when '.xls'
     to_xls(outFile)
     return
  when '.xlsx'
    p 'use Tab-Separated-Value text format.'
    outFile = outFile + '.txt'
  else
   #do nothing
  end
  #デフォルトオプションの設定
  opts[:enc] ||= 's'
  opts[:escape] ||= false
  opts[:separator] ||= SEPARATOR
  
  to_text(outFile) do |row|
    orow = []
    if(opts[:escape])
      row.each do |cell|
        orow << myescape(cell)
      end
    else
       row.each do |cell|
          orow << cell.to_s.gsub(/[#{opts[:separator]}\r\n]/, '')
       end
    end
    
    begin
      str = localEncode(orow.join(opts[:separator]), opts[:enc])
    rescue Encoding::UndefinedConversionError
       orow.each do |ele|
         begin
           localEncode(ele, opts[:enc])
         rescue
           raise "encode error.#{ele}\n(#{orow})"
         end
       end

      @log.debug(row.join(opts[:separator]))
    end
    str
  end
end