IronLionZion
IronLionZion

Reputation: 123

Listen to paragraph events in OfficeJS (Office Add-ins Platform)

I am using the JavaScript API for Office Add-ins and trying to attach a function to an event that fires whenever a new paragraph is added (and eventually, also on paragraph delete/modify).

I can iterate through and print all the paragraphs in the document like so:

Word.run( context => {
  var paragraphs = context.document.body.paragraphs;
  context.load(paragraphs, 'text');     
  return context.sync().then( () => {
    for (var i = 0; i < paragraphs.items.length; i++) {
      console.log(paragraphs.items[i].text);
    }
  });
});

What I want to do is listen to new paragraphs and do an action based on that. I found EventType.NodeInserted but I'm not sure how to use it for all paragraphs of the document. Ideally I'd like to have something like the following:

Office.context.document.addHandlerAsync(Office.EventType.NodeInserted, 
  function (paragraphEvent) {
    // do something useful with new paragraph
});

I've looked through all the documentation but I'm new to the API so I have a hard time understanding how to bind events to any new paragraph (in the whole document, not in a specific section).

Upvotes: 0

Views: 303

Answers (1)

Juan Balmori
Juan Balmori

Reputation: 5036

there is no event to detect paragraph changes in the document. The eventType.NodeInserted is used in when a new node is added to a Custom XML Part.

your only option at this point is to poll the document and infer changes, or if you are interested in a specific section of the document you can create a binding and subscribe to the binding.dataChanged events and infer deltas.

Hope this helps... Juan.

Upvotes: 1

Related Questions