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

泄露秘密 提交于 2021-01-27 19:50:31

问题


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?


回答1:


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");
    }
  }
});


来源:https://stackoverflow.com/questions/60176353/word-addin-officejs-detect-if-cursor-is-on-chart-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!