Word 插件/OfficeJS - 检测光标是否在图表元素上 [英] Word Addin/OfficeJS - Detect if Cursor is on Chart Element

查看:41
本文介绍了Word 插件/OfficeJS - 检测光标是否在图表元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检查文档光标是否位于 MS Word API 中的 Chart 元素内.现在我有一个插入文本的应用程序,但是当我尝试将所述文本插入到图表的标题中时,它会删除图表并将其替换为我正在插入的内容控件.

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?

推荐答案

应该这样做.就像@CindyMeister 说的,你可以检查 ooxml 并检查它,range.getOoxml() 只会在光标位于某个已插入文档的 xml 对象上时返回一些内容.

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

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

这篇关于Word 插件/OfficeJS - 检测光标是否在图表元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆