Excel 스프레드 시트에서보고하고 있으며 "O"열의 값에 대해 테스트를 실행해야합니다.
이렇게하려면 먼저 Hash
"O"로 키가 존재하는지 확인하는 메서드를 포함 하도록 클래스를 확장 했지만 이제 해당 키의 값을 테스트해야합니다.
require 'creek'
class Hash
def has_rkey?(search)
search = Regexp.new(search.to_s) unless search.is_a?(Regexp)
!!keys.detect{ |key| key =~ search }
end
end
creek = Creek::Book.new "timesheet.xlsm"
sheet = creek.sheets[0]
data = {}
sheet.rows.each do |row|
x = {}
row.each do |key, val|
cell = {key => val}
x.merge!(cell)
end
if x.has_rkey?('O')
#Some way of returning the value of the key containing "O"
end
data.merge!(x)
end
말은 할 수 있습니다 x
포함하는 해시했다 {"N1" => 14, "O1" => 15}
입니다 내가 문자 "O"를 포함하는 키 값을 반환하려면,15
시험:
hash = {"N1" => 14, "O1" => 15, "O2" => 16}
values = hash.select { |k,v| k.include? 'O' }.values #=> [15, 16]
이 코드는 Hash#select
조건을 지정하고 String#include?
각 키를 확인 Hash#values
하고 값을 배열로 반환하는 데 사용합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다