Skip to content

Instantly share code, notes, and snippets.

@kei-p
Created November 30, 2016 06:07
Show Gist options
  • Save kei-p/74aeff4ec49c4754788881f78f7bb773 to your computer and use it in GitHub Desktop.
Save kei-p/74aeff4ec49c4754788881f78f7bb773 to your computer and use it in GitHub Desktop.
markdown で作ったテストシートを xlsx に変換

トピック1

カテゴリ1

  • テスト内容 1

    • 確認事項 1
  • テスト内容 2

    • 確認事項 2

カテゴリ2

  • テスト内容 3

    • 確認事項 3
  • テスト内容 4

    • 確認事項 4
  • テスト内容 5

    • 確認事項 5-1
    • 確認事項 5-2
  • テスト内容 6

    • 確認事項 6-1
    • 確認事項 6-2
    • 確認事項 6-3
source "https://rubygems.org"
gem 'redcarpet'
gem 'rubyXL'
require 'bundler/setup'
Bundler.require
file = File.open('./demo.md').read
renderer = Redcarpet::Render::HTML.new
markdown = Redcarpet::Markdown.new(renderer)
html = markdown.render(file)
REGEXP_PATTERNS = {
category: %r{\A<h2>(?<text>.*)</h2>\Z},
feature: %r{\A<h3>(?<text>.*)</h3>\Z},
case: %r{\A<li><p>(?<text>.*)</p>\Z},
check: %r{\A<li>(?<text>.*)</li>\Z}
}
check_list = []
check_item = {}
html.each_line do |line|
REGEXP_PATTERNS.each do |k, v|
if m = line.match(v)
text = m[:text]
check_item[k] = text
check_list.push(check_item.clone) if k == :check
end
end
end
puts check_list
workbook = RubyXL::Parser.parse('template.xlsx')
worksheet = workbook[0]
worksheet.sheet_name = 'テストシート'
# ヘッダーの行数分ずらす
base_row = 1
check_list.each.with_index do |item, index|
row = index + base_row
%i(category feature case check).each.with_index do |k, i|
worksheet.add_cell(row, i, item[:category])
end
end
workbook.write('demo.xlsx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment