问题描述
我正在尝试使用 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]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)