3

I thought this would be a simple task after I learn how to traverse the structure of a OneNote page.

A simple page will have an outline (that is the quivalent of a div html tag) and this outline will contain paragraphs (the p tag). Now the interesting part is coming, OneNote can indent paragraphs, this way the indented paragraph gets added to the collection of subparagraphs its predecessor has. This is easier said than done

The APIs are here.

Basically paragraph.paragraphs.items is the collection of paragraphs that are children of a paragraph. Each paragraph object has a property called parentParagraph which I can't figure out how to use.

So the idea would be to take an existing paragraph and move it as

ParagraphL1
     ParagraphL2
     ParagraphL3

should turn into

   ParagraphL1
     ParagraphL2
          ParagraphL3

For the above to happen I guess paragraph2.items[0]=paragraph3. I am not sure what is happening with the reference that paragraph1 has for paragraph3 as being one of its children

The other thing that I can't figure out is how to update the text of a paragraph with a new text. It seems that with the current APIs there no other way to do it other than adding a sibling of the existing one and deleting the existing one

Update

I have tried to move 'test' under Level11 (see the bottom of the picture for structure and IDs, see the console output, last three lines

So adding the test paragraph to Level11.items succeeded but it did not have the effect I expected; as you can see in the Watch window Level11 indicates it has child with test's ID but test does not indicate Level11 as its parent (see the IDs at the bottom and in the watch window

out of sync pointers

Summary

In one sentence: I need to be able to indent/outdent any arbitrary paragraph in a OneNote document using JavaScript APIs. Start from this code if you wish.

halfer
  • 19,824
  • 17
  • 99
  • 186
MiniMe
  • 1,057
  • 4
  • 22
  • 47

1 Answers1

0

I'm not an expert on the OneNote APIs, but it seems that the paragraph.richtext.text property is read only. So, I think your idea to add sibling is the right one. For similar reasons, I think that to indent, you need to insert a child paragraph (that duplicates the original paragraph) one level down and then delete the original paragraph.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
  • quite convoluted way to do it, They could have made the parentParagraph writable – MiniMe Jun 12 '20 at 17:52
  • I have tried the reverse, that was to make 'test' a sibling of Level11 (see the picture in the update) and see the comments there – MiniMe Jun 12 '20 at 20:41
  • Are you using the `Paragraph.insert*AsSibling` methods or are you trying to directly push objects onto the paragraphs.items array? I think you have to use the insert methods. – Rick Kirkham Jun 12 '20 at 21:13
  • I considered that too but that works for a paragraph that has no subsequent children. What I want is to indent or un-indent a paragraph and all its children. The method you are suggesting works when you add text or html before or after a paragraph, – MiniMe Jun 12 '20 at 21:30
  • Was the bounty attributed to you ? This is so unfair, I lost 200 points and I have no pertinent answer to this – MiniMe Jun 23 '20 at 10:30
  • No. I have not received the bounty. – Rick Kirkham Jun 23 '20 at 20:08
  • where the heck did it go then? – MiniMe Jun 23 '20 at 21:00
  • I don't know how bounties work in Stack. I'm trying to find some help for your question internally at Microsoft. – Rick Kirkham Jun 23 '20 at 23:05
  • oh you are Microsoft? Thanks God someone is looking at this in the end. I opened an issue here as well. https://github.com/OfficeDev/office-js/issues/1209 I am planning to develop an application around note and I need all the help I can get. I have tried various ways -REST API and then JavaScript but nothing helps. I will be forever indebted to you if you could put me in touch with someone I could ask questions..there seems to be a huge void in this space (OneNote JavaScript API examples) – MiniMe Jun 24 '20 at 00:45
  • The above link is wrong, this is the github issue I was referring to https://github.com/OfficeDev/office-js/issues/1217 – MiniMe Jun 25 '20 at 11:46