ruby中excel簡單操作以及文件讀取操作方法


# -*-coding:utf-8 -*-
#author:kanlijun
require 'win32ole'
require 'fileutils'
class ResultAnalyse
@@i=0
@@f=0
def initialize path
excel =WIN32OLE.new('excel.application')
@workbook =excel.WorkBooks.open(path)
@worksheet=@workbook.Worksheets(1)
end

#獲取excel第一行第一列的標題文本
def get_title
@worksheet.Range("a1").Value
end

def get_receive
@worksheet.Range("i3").Value
end

#獲取sheet中用例行數
def get_rows
@worksheet.usedrange.rows.count
end

#刪除excel中上一次寫入的數據
def del_excel_result
num = get_rows
for i in 3..num
@worksheet.Range("g#{i}").Value=''
@worksheet.Range("g#{i}").Interior.ColorIndex = 0 #無色

end
end

#獲取flag等於1的文件名稱以及行數
def get_runinterface
num= get_rows
arr=[]
rows=[]
for i in 3..num
t=@worksheet.Range("d#{i}").Value
if t=='1'
rows<<i
arr<<@worksheet.Range("c#{i}").Value+'.rb'
end
end
return arr,rows
end

#將執行結果寫入excel
def judge_pass_fail file_path,rows
n=rows
if File.exist?("#{file_path}")
f=File.open("#{file_path}",mode='r')
case_str =f.readlines[2..-1]
case_str.each do|item|
@@i+=1
if item.include?('passed')
@worksheet.Range("g#{n}").Value='Pass'
@worksheet.Range("g#{n}").Interior.ColorIndex = 4 #綠色
elsif item.include? 'failed'
@@f +=1
@worksheet.Range("g#{n}").Value='Fail'
@worksheet.Range("g#{n}").Interior.ColorIndex = 3 #紅色
else
@worksheet.Range("g#{n}").Value='wrong'
@worksheet.Range("g#{n}").Interior.ColorIndex = 40 #黃色
end
n +=1
end
else
puts 'not exist file'
end
[@@i,@@f]
end

#關閉excel
def excel_close
@workbook.close(1)
end

#獲取case用例文件txt
def get_interface_result(file_path,rows)
if File.directory? file_path
i=0
Dir.foreach(file_path) do |file|
if file !="." and file !=".."
# p file_path+"/#{file}"
@one_line=judge_pass_fail(file_path+"/#{file}",rows[i])
i+=1
end
end
return @one_line
else
puts "file_path is not exist!"
end
end

#刪除上一次用例執行結果txt
def delete_spec list
if File.directory? list
FileUtils.rm_rf(list)
end
end

def auto_write

end

end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM