Word JS API: Range.compareLocationWith not working

huangapple go评论61阅读模式
英文:

Word JS API: Range.compareLocationWith not working

问题

以下是您要翻译的内容的中文部分:

我正在尝试确定用户是否点击了另一个段落。
我的方法:前一个选择的段落被存储为本地的 Range 对象,以便稍后进行比较,当发生另一个点击时。

这是用于获取所选段落的代码:

export const getSelectedParagraphRange = async () => {
  return await Word.run(async (context) => {
    const range = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    return range
  })
}

这是用于比较范围的代码:

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(async (context) => {
    const newRange = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}

如果我将 newRange 与自身进行比较,它可以正常工作(我会得到一个值为 "Equal" 的比较对象)。
但是,如果我将参数范围与自身进行比较,它不起作用(函数只是“停止”,没有抛出错误等)。

所以我猜测应该直接从函数内部获取应该进行比较的范围对象?如果是这样,那么我想知道如何比较以前选择的段落与新的段落?
非常感谢任何帮助。

更新:

由于学习了提到的《构建 Office 插件》书籍,我成功使段落比较工作。getSelectedParagraphRange 函数缺少通过调用 range.track() 来跟踪范围对象。此外,在将新段落设置为当前段落时,需要取消跟踪已跟踪的范围。
(我简单地不了解隔离上下文范围背后的原则)。

以下是可用的代码:

export const getSelectedParagraphRange = async (formerRange?: Word.Range) => {
  return await Word.run(async (context) => {
    if (formerRange) {
      formerRange.untrack()
      await context.sync()
    }

    const range = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()
    range.track()
    await context.sync()

    return range
  })
}

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(range, async (context) => {
    const newRange = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}
英文:

I'm trying to determine if the user has clicked in another paragraph.
My approach: The former selected paragraph gets stored locally as a Range object to be compared with later, when another click happens.

This is the code to get the selected paragraph:

export const getSelectedParagraphRange = async () => {
  return await Word.run(async (context) => {
    const range = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    return range
  })
}

This is the code to compare the ranges:

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(async (context) => {
    const newRange = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}

If I compare the newRange with itself it works (I get a compare object with value "Equal").
But if I compare the parameter range with itself it doesn't work (the function just "stops" without throwing an error etc).

So my guess is that the range objects which should be compared have to be taken directly from inside the function? If so, then I'm wondering how I can compare a former selected paragraph with a new one?
Any help is greatly appreciated.

UPDATE:

Thanks to studying the mentioned Building Office Add-ins book I managed to get the paragraph comparison work. The getSelectedParagraphRange function lacked the tracking of the range object by calling range.track(). Additionally the tracked ranges have to be untracked when setting a new paragraph as the current one.
(I simply wasn't aware of the principles behind the isolated context scopes).

Here's the working code:

export const getSelectedParagraphRange = async (formerRange?: Word.Range) => {
  return await Word.run(async (context) => {
    if (formerRange) {
      formerRange.untrack()
      await context.sync()
    }

    const range = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()
    range.track()
    await context.sync()

    return range
  })
}

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(range, async (context) => {
    const newRange = context.document
      .getSelection()
      .paragraphs.getFirst()
      .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}

答案1

得分: 0

You need to pass the range object to the overload of Word.run that takes an object as the first parameter.

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(range, async (context) => {
    const newRange = context.document
     .getSelection()
     .paragraphs.getFirst()
     .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}

See Word.run. I also recommend the book Building Office Add-ins for advanced scenarios like this one.

英文:

You need to pass the range object to the overload of Word.run that takes an object as the first parameter.

export const isParagraphStillActive = async (range: Word.Range) => {
  return await Word.run(range, async (context) => {
    const newRange = context.document
     .getSelection()
     .paragraphs.getFirst()
     .getRange()

    const compare = range.compareLocationWith(newRange)
    await context.sync()

    return compare.value === 'Equal'
  })
}

See Word.run. I also recommend the book Building Office Add-ins for advanced scenarios like this one.

huangapple
  • 本文由 发表于 2023年5月10日 22:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定