如何在Office 365 JavaScript API中获取文档标题? [英] How can i get the document title in Office 365 JavaScript API?

查看:112
本文介绍了如何在Office 365 JavaScript API中获取文档标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用angularjs编写Office加载项,我需要获取并设置文档标题(在文档顶部看到的标题).

I am writing an office add-in with angularjs, i need to get and set document title (that seen at that the top of the document).

这是我的代码:

function run() {
  Word.run(function (context) {
    var properties = context.document.properties;
    context.load(properties);
    return context.sync()
        .then(function() {
            console.log('thisDocument.properties.title', properties.title);
        })
    })
    .catch(function(error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Utilities.log(error);
    });
}

但是在控制台中没有打印文档标题!

but in console didn't print the title of document!

推荐答案

您要写入控制台的 context.document.properties.title 属性是文件级属性如果选择文件>>信息(在Windows桌面上运行的Excel中),则在属性"下显示的标题.它不是您在文档顶部看到的标题"(文本),也不是文件本身的名称.我怀疑如果您(通过Word UI)检查文档的文件级属性标题,您会发现未填充标题属性- -除非您明确设置它,否则不会填充它.

The context.document.properties.title property that you're writing to the console is the file-level attribute Title that is displayed under Properties if you select File >> Info (in Excel running on Windows Desktop). It is not the "title" (text) that you see at that the top of your document, or the name of the file itself. I'd suspect that if you examine the file-level attribute Title for your document (via the Word UI), you'll see that the Title attribute is not populated -- it won't be populated unless you've explicitly set it.

我对Word API对象模型并不十分熟悉,但这可能会有所帮助.以下代码段获取文档的第一段(如果文档的第一行是标题,将代表文档的标题),然后使用新的文本字符串更新标题的文本(同时保持任何以前的格式,等等.

I'm not super-familiar with the Word API object model, but here's something that might be helpful. The following code snippet gets the first paragraph of the document (which, if the first line of a document is the title, will represent the title of the document), and then updates the text of the title with a new text string (while maintaining any previous formatting, etc.).

Word.run(function (context) {

    // get the first paragraph 
    // (if the first line of the document is the title, this will be the contents of the first line (title))
    var firstParagraph = context.document.body.paragraphs.getFirst();
    firstParagraph.load("text");

    // get the OOXML representation of the first paragraph
    var ooXML = firstParagraph.getOoxml();

    return context.sync()
        .then(function () {
            // replace the text of the first paragraph with a new text string
            firstParagraph.insertOoxml(ooXML.value.replace(firstParagraph.text, "NEW TITLE"), "Replace");

            return context.sync();
        });

}).catch(function (error) {
        console.log("Error: " + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });

注意 :如果您不能假定文档的第一个 paragraph 始终是文档标题(即有时是文档,可能没有标题,或者例如有时标题前面可能有一个或多个空行),则需要在上面的代码段中添加其他逻辑,以确定第一个 paragraph 在继续执行替换第一段内容的逻辑之前,文档的em>确实是标题.

Note: If you cannot assume that the first paragraph of the document will always be the document title (i.e., if sometimes the document may not have a title, or if sometimes the title may be preceded by one or more blank lines, for example), you'll need to add additional logic to the snippet above to determine whether or not the first paragraph of the document is indeed a title, before proceeding with the logic that replaces the contents of the first paragraph.

这篇关于如何在Office 365 JavaScript API中获取文档标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆