【问题标题】:How do you get the current page? [Trying to create a simple OneNote Addin that adds text to the current page]如何获取当前页面? [尝试创建一个简单的 OneNote Addin 将文本添加到当前页面]
【发布时间】:2016-08-02 21:06:35
【问题描述】:

正如标题所说,我正在尝试创建一个 OneNote 插件,它只是将一些文本添加到当前页面(在您的 OneNote 桌面应用程序中)。

我查看了 API,我可以将文本添加到 Pages XML,然后使用 UpdatePageContent() 更新页面...但我看不到任何内容可以为您提供当前正在查看的页面在?

对不起,如果这很明显,或者如果有一种方法可以在不需要 API 的情况下编辑页面...我花了几天时间研究 OneNote 插件示例,但只是设法真正获得了按钮显示在功能区中!

任何建议或指导将不胜感激。

【问题讨论】:

    标签: onenote onenote-api


    【解决方案1】:

    以下是获取活动页面的方法。

    OneNote.run(function (context) {
    
        // Get the active page.
        var page = context.application.getActivePageOrNull();
    
        // Queue a command to load the page. 
        // For best performance, request specific properties.           
        page.load('id,title');
    
        // Run the queued commands, and return a promise to indicate task completion.
        return context.sync()
            .then(function () {
    
                if (!page.isNull) {
                    // Show some properties.
                    console.log("Page title: " + page.title);
                    console.log("Page ID: " + page.id);
                }
            });
    })
    .catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
    

    https://github.com/OfficeDev/office-js-docs/blob/master/reference/onenote/application.md#getactivepageornull

    下面是向活动页面添加内容的示例:

    OneNote.run(function (context) {
    
        // Gets the active page.
        var page = context.application.getActivePage();
    
        // Queue a command to add an outline with given html. 
        var outline = page.addOutline(200, 200,
    "<p>Images and a table below:</p> \
     <img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"> \
     <img src=\"http://imagenes.es.sftcdn.net/es/scrn/6653000/6653659/microsoft-onenote-2013-01-535x535.png\"> \
     <table> \
       <tr> \
         <td>Jill</td> \
         <td>Smith</td> \
         <td>50</td> \
       </tr> \
       <tr> \
         <td>Eve</td> \
         <td>Jackson</td> \
         <td>94</td> \
       </tr> \
     </table>"     
            );
    
        // Run the queued commands, and return a promise to indicate task completion.
        return context.sync()
            .catch(function(error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });
    });
    

    【讨论】:

    • 好的...所以这不是我需要的答案,因为那是针对 OneNote Web 应用程序而不是桌面应用程序,但无论如何我已经找到了我想要的。我需要的是一个隐藏在 OneNote.Application 对象的 Windows 部分中的方法。
    【解决方案2】:

    如果您使用的是桌面版 OneNote,答案是这样的:

    string thisNoteBook = oneNote.Windows.CurrentWindow.CurrentNotebookId;
    string thisSection = oneNote.Windows.CurrentWindow.CurrentSectionId;    
    string thisPage = oneNote.Windows.CurrentWindow.CurrentPageId;
    

    这些将为您提供当前活动页面的 ID,您可以从那里使用

    oneNote.GetPageContent(thisPage, out xmlPage);
    

    oneNote.GetHierarchy(thisNoteBook, HierarchyScope.hsSections, out HieracyXML);
    

    等等


    如果有人阅读本文需要任何进一步的帮助,请给我发消息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多