excel - 将 Excel 工作表转换为 DITA XML
问题描述
我正在尝试使用 Ruby 将 Excel 工作表转换为 DITA (XML)。这是代码:
require 'creek'
require 'nokogiri'
def writeFile (file)
# Need path for map
filename = File.join("#{@output}/glossentries/",@filename)
File.open("#{filename}", "w") { |f| f.write file }
puts "Wrote: #{filename}"
end
@topics = Array.new
@keys = Array.new
xlsheet = ARGV[0]
@output = ARGV[1]
Dir::mkdir(@output) unless File.exists?(@output)
Dir::mkdir("#{@output}/glossentries/") unless File.exists?("#{@output}/glossentries/")
doc = Creek::Book.new(xlsheet)
sheet = doc.sheets[0]
sheet.rows.each.with_index do |row, idx|
# index 0 is the header row. If you remove the header row, then remove the if statement.
if idx != 0
@filename = "#{row["A#{idx+1}"].downcase.delete(' ').gsub(/[(,\-)\/']/ , '_')}.dita"
@topics.push(@filename)
@keys.push("#{row["A#{idx+1}"].downcase.delete(' ').gsub(/[(,)\-\/']/ , '_')}")
begin
builder = Nokogiri::XML::Builder.new do |xml|
xml.doc.create_internal_subset(
'glossentry',
"-//OASIS//DTD DITA Glossary//EN",
"glossary.dtd"
)
xml.glossentry('id' => row["A#{idx+1}"].downcase.delete(' ').gsub(/[(,)\-\/']/ , '_')) do
xml.glossterm row["A#{idx+1}"]
xml.glossdef row["B#{idx+1}"]
if row["C#{idx+1}"] # write out longer form of glossary entry if acroynm exists
xml.glossBody do
xml.glossSurfaceForm row["A#{idx+1}"] + " (" + row["C#{idx+1}"] + ")" # put columns a and c together
xml.glossAlt do
xml.glossAcronym row["C#{idx+1}"] # column c only in acronym
end
end
end
end
end
writeFile(builder.to_xml)
end
end
end
### build the DITA Map
builder = Nokogiri::XML::Builder.new do |ditamap|
ditamap.doc.create_internal_subset(
'map',
"-//OASIS//DTD DITA Map//EN",
"map.dtd"
)
ditamap.map('id' =>'glossary_entries')do
ditamap.title 'Glossary Map'
@topics.each.with_index do |indfile, idx|
ditamap.topicref('href' => "glossentries/#{indfile}", 'keys' => @keys[idx], 'toc' => 'no')
end
end
end
# build map file
contents = builder.to_xml
filename = File.join(@output,'glossaryentries.ditamap')
File.open("#{filename}", "w") { |f| f.write contents }
puts "Wrote: #{filename}"
Excel 工作表有 3 列:
- 单词
- 定义
- 首字母缩略词
一旦我执行它(excel2dita <Excel file> <output directory>
),我会收到以下消息:
1: from C:/Users/Pizza/Desktop/excel2glossary/excel2dita.rb:20:in `<main>'
C:/Users/Pizza/Desktop/excel2glossary/excel2dita.rb:20:in `exists?': no implicit conversion of nil into String (TypeError)
我不确定,我在这里缺少什么,为什么它不起作用。
Ruby 版本是:ruby 2.7.2p137(2020-10-01 修订版 5445e04352)[x64-mingw32]
解决方案
推荐阅读
- python - 检查字符串是否为有效的 HTML 标签
- php - 检查所有域 laravel 中 URL 中的 GET 方法
- php - 如何制作可以从数组中为刽子手游戏选择单词的按钮
- python - 用于预测的分类或回归
- mysql - 我必须加入两个选择查询
- apache-spark - pyspark:ORC 不支持从文件类型字符串(1)到阅读器类型数组的类型转换
(1) - swift - 模拟和注入 ObservableObject
- javascript - 将单词列表呈现为
React js 中的标签
- reactjs - Material-UI Pickers – 使空日期(onblur)触发错误
- node.js - 如何根据构建或多个环境在nextjs中设置set process.env