OneNote使用UpdateHierarchy创建页面 - 如何查找新页面?

时间:2013-05-24 15:12:56

标签: powershell onenote

我设法在OneNote笔记本中的特定位置创建部分。现在我想用页面实现相同的功能。所以我没有使用不可预测的放置“CreateNewPage”方法,而是使用UpdateHierarchy,它完全正常(出于测试目的,我正在使用下面的AppendChild)。

我遇到的唯一问题是,在使用UpdateHierarchy创建新页面后,我完全放弃了对新创建页面的任何链接。 OneNote分配一个ID并忽略我给出的所有其他标签/名称。设置用于设置标题的One:T成员也会被忽略 - 它总是会创建一个“无标题页面”。

我做错了什么或是否需要先创建CreateNewPage,并使用指定的页面ID,使用UpdateHierarchy重新放置它?

此致 乔尔

function createPage {
param([string]$title, [string]$sectionnode)

[string]$pageref=$null

# Gather the pages within the notebook
[xml]$ref = $null
$_globalOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)

[System.Xml.XmlNamespaceManager] $nsmgr = $ref.NameTable
$nsmgr.AddNamespace('one', "http://schemas.microsoft.com/office/onenote/2010/onenote")

# Creation of a new page
$newPage = $ref.CreateElement('one', 'Page', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newTitle = $ref.CreateElement('one', 'Title', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newOE = $ref.CreateElement('one', 'OE', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newT = $ref.CreateElement('one', 'T', 'http://schemas.microsoft.com/office/onenote/2010/onenote')

$newPage.SetAttribute('name', "Olololo")
$newT.InnerText = '<![CDATA[Testtitle]]>'

$newOE.AppendChild($newT)
$newTitle.AppendChild($newOE)
$newPage.AppendChild($newTitle)

$ref.Section.AppendChild($newPage)
$_globalBJBOneNote["COM"].UpdateHierarchy($ref.OuterXML)
}

1 个答案:

答案 0 :(得分:0)

这就是诀窍。仍然必须使用UpdatePageContent完成标题等的设置,但是新页面放置正确。为了放置元数据(标题,缩进等),可以使用单独的函数,该函数使用createPage函数的返回GUID工作。

function createPage {
param([string]$sectionnode, [string]$pagenode = $sectionnode)

# Gather the sections within the notebook
[xml]$ref = $null
$_globalBJBOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)

[System.Xml.XmlNamespaceManager] $nsmgr = $ref.NameTable
$nsmgr.AddNamespace('one', "http://schemas.microsoft.com/office/onenote/2010/onenote")

# Create new page
[string]$pageID = $null
$_globalBJBOneNote["COM"].createNewPage($ref.Section.ID, [ref]$pageID)

# Reload the hierarchy, now we can get the node of the new notebook
$_globalBJBOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)
$newPageNode = $ref.SelectSingleNode('//one:Page[@ID="' + $pageID + '"]', $nsmgr)
$referencePageNode = $ref.SelectSingleNode('//one:Page[@ID="' + $pagenode + '"]', $nsmgr)

# Reposition
[void]$ref.Section.removeChild($newPageNode)
[void]$ref.Section.InsertAfter($newPageNode, $referencePageNode)
$_globalBJBOneNote["COM"].UpdateHierarchy($ref.OuterXML)

$pageID
}
相关问题