Skip to content

Instantly share code, notes, and snippets.

@espio999
Last active May 27, 2023 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espio999/467f6cf3a25aea8cb0a5f95b9c16f438 to your computer and use it in GitHub Desktop.
Save espio999/467f6cf3a25aea8cb0a5f95b9c16f438 to your computer and use it in GitHub Desktop.
Print OneNote data items as CSV
function makeCSV($hierarchy, $node_depth){
$data = @()
foreach ($item in $hierarchy.ChildNodes){
$data += makeCSVitem $node_depth $item
}
return $data
}
function makeCSVitem([string]$node_depth, $items){
$data = @()
foreach($item in $items){
$data_item = New-Object PSObject | Select-Object depth, name, ID
$data_item.depth = $node_depth
$data_item.name = $item.name
$data_item.ID = $item.ID
$data += $data_item
if ($item.HasChildNodes){
$node_depth = countNodeDepth $node_depth
makeCSV $item $node_depth
}
}
return $data
}
function countNodeDepth([int]$n){
return $n + 1
}
function saveCSV($data){
$output_path = './data.csv'
$data | Export-Csv -Delimiter "`t" -Path $output_path -Encoding Default
}
$OneNote = New-Object -ComObject OneNote.Application
Add-Type -assembly Microsoft.Office.Interop.OneNote
$OneNoteID = ""
[xml]$Hierarchy = ""
$OneNote.GetHierarchy(
$OneNoteID,
[Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages,
[ref]$Hierarchy,
[Microsoft.Office.InterOp.OneNote.XMLSchema]::xsCurrent)
$csv_data = makeCSV($Hierarchy)
saveCSV $csv_data
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($OneNote) | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment