如何检测 Word js api 中的旧文档格式
Posted
技术标签:
【中文标题】如何检测 Word js api 中的旧文档格式【英文标题】:How to detect old doc format in Word js api 【发布时间】:2017-11-10 01:42:30 【问题描述】:我为 word 创建了一个任务窗格插件,它使用 Document.getFileAsync 方法以压缩格式 (docx) 获取文档内容。 这适用于 .docx 文件,但如果使用旧的 .doc 文件,则毫无疑问会失败。
我收到以下错误:
code: 5001
message: "An internal error has occurred."
name: "Internal Error"
有没有办法在调用 getFileAsync 之前检测无效格式的文档? 我尝试使用以下代码读取文档属性格式值:
return Word.run(function (context)
var properties = context.document.properties;
context.load(properties, "format");
return context.sync()
.then(function ()
return properties.format;
);
);
但是对于 docx 和 doc 文件,返回的值始终是一个空字符串。
我希望能够检测旧文件格式,以便向用户显示适当的错误消息。
【问题讨论】:
【参考方案1】:getFileAsync()
方法仅适用于.docx
文件。只是为了检测正确的文件,您可以简单地检查文件的扩展名:fname.substr((~-fname.lastIndexOf('.') >>> 0) + 2)
其中fname
是此处的文件名。并相应地提示您的消息。
【讨论】:
以上是关于如何检测 Word js api 中的旧文档格式的主要内容,如果未能解决你的问题,请参考以下文章