Judson James
Judson James

Reputation: 33

Word Addin/OfficeJS - Detect if Cursor is on Chart Element

I want to be able to check if the document cursor is inside of a Chart element within the MS Word API. Right now I have an application that inserts text, but when I try to insert said text into the title of the Chart, it deletes the chart and replaces it with the Content Control I'm inserting.

Instead of deleting the chart, I want to check if the cursor is inside of the Chart via context. If I'm inside of the Chart in any way, be able to throw a warning message to the user and escape. Is there a way to do this?

Upvotes: 3

Views: 178

Answers (1)

DjH
DjH

Reputation: 1498

This should do it. Like @CindyMeister said you can check the ooxml and inspect it, range.getOoxml() will only return something if the cursor is on some xml object that's been inserted into the document.

example.ts

Word.run(async (context: RequestContext) => {
  const range: Range = context.document.getSelection();
  const ooxml: ClientResult<string> = range.getOoxml();
  context.load(range);
  await context.sync();

  const ooxmlVal = ooxml.value;
  if (ooxmlVal) {

    const lowered = ooxmlVal.toLowerCase();
    const isChart = lowered.includes("excel") && lowered.includes("chart");
    if (isChart) {
      console.log("CURSOR IS ON CHART");
    }
  }
});

Upvotes: 1

Related Questions