ruby - How to add custom properties on docx files in ruby
问题描述
I am trying to figure out how to properly add custom properties to .docx
files programmatically in ruby. I also have an office-js add in which reads these custom properties - but more on that later.
Here are the steps I am currently taking to add custom properties to a .docx
file in ruby:
- Opening the document using the Docx gem and reading the contents of the archive using
Docx:Document.open(filepath).zip
- Adding in a
docProps/custom.xml
file with the customproperty
elements which I want eg:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="matter.id">
<vt:lpwstr>518</vt:lpwstr>
</property>
- Adding the following
Override
element to the[Content_Types].xml
file:
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
- Adding the following
Relationship
element to the_rels/.rels
file:
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/>
Note: The Id attribute value is worked out by the number of <Relationship>
elements present.
After these steps: After running this code over a blank / newly created document I can open the file in Word on my computer and my office-js add in can successfully read the custom properties which I have set in my ruby application.
The issue: When I upload & open the document in Word Online my office-js add in fails to recognised the custom properties which have been set.
My office-js add in reads and write the custom properties using Microsoft's office-js apis and when setting the properties this way my add in is able to recognise them in both Word and Word Online so I am pretty sure I'm missing something when trying to set the properties manually in my ruby function.
I've tried to compare the archives of both a .docx
file which has had the custom properties set by my ruby function and the same file with the custom properties set using my add in and I just can't see any noticeable differences which would be causing Word Online to fail to recognise the custom properties set using my ruby code.
Unfortunately there doesn't seem to be any ruby gems which would allow me to edit the custom properties on a docx file like with the office-js apis and that is why I have resorted to doing it manually.
解决方案
推荐阅读
- java - 如何告诉 Jax-Rs 只扫描特定文件夹中的资源
- javascript - 比较 unix 时间戳不返回预期结果 - JAVASCRIPT
- python - Discord 机器人没有响应命令
- reactjs - 将简单的 HTML 脚本转换为 React
- dgl - 当输入是 DGL 中的列表时,DGLGraph.pull 会出错
- javascript - 运行 window.onload 和 setTimeout 不起作用
- python - 如何使 PyGObject 应用程序适合 macOS 上的扩展坞
- swift - 是否有一种通用的方法来处理异步和快速抛出的组合
- domain-driven-design - 如何处理resolvejs中聚合根之间的关系
- java - JSF 状态持有者保护程序内存泄漏