# 定位方法

**URL:** https://community.onlyoffice.com/t/topic/20346
**Category:** Docs
**Tags:** how-to-question
**Created:** [24 July 2026 09:57 UTC](https://community.onlyoffice.com/t/topic/20346 "2026-07-24T09:57:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![xiahongyuan](https://community.onlyoffice.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xiahongyuan](https://community.onlyoffice.com/u/xiahongyuan)
#### Post date: [24 July 2026 09:57 UTC](https://community.onlyoffice.com/t/topic/20346/1 "2026-07-24T09:57:14Z")

</div>

我想先跳转到指定页 在当前页然后使用Search方法进行文本的选中 应该怎么实现呀 谢谢

---

<div class="post-metadata">

### Author: ![Nurullo](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/nurullo/32/11006_2.png) [@Nurullo](https://community.onlyoffice.com/u/Nurullo)
#### Post date: [27 July 2026 13:18 UTC](https://community.onlyoffice.com/t/topic/20346/2 "2026-07-27T13:18:58Z")

</div>

您好，

先跟您确认一下需求——您是想通过代码（宏/插件）先跳转到指定页，然后在该页选中某段文本，对吗？

如果是的话，可以按以下顺序实现：

js

```auto
let doc = Api.GetDocument();
doc.GoToPage(2); // 跳转到指定页
let ranges = doc.Search("目标文本");
if (ranges.length > 0) ranges[0].Select(); // 选中文本

```

相关方法：

- [ApiDocument | ONLYOFFICE](https://api.onlyoffice.com/docs/office-api/usage-api/document-api/ApiDocument) （GoToPage, Search）
- [ApiRange | ONLYOFFICE](https://api.onlyoffice.com/docs/office-api/usage-api/document-api/ApiRange) （Select）

请确认是否符合您的使用场景。

---

<div class="post-metadata">

### Author: ![xiahongyuan](https://community.onlyoffice.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xiahongyuan](https://community.onlyoffice.com/u/xiahongyuan)
#### Post date: [29 July 2026 05:41 UTC](https://community.onlyoffice.com/t/topic/20346/3 "2026-07-29T05:41:45Z")

</div>

doc.Search(“目标文本”)中的doc是整篇文档查找的嘛 如何在指定章节查找指定文本呢

---

<div class="post-metadata">

### Author: ![Nurullo](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/nurullo/32/11006_2.png) [@Nurullo](https://community.onlyoffice.com/u/Nurullo)
#### Post date: [29 July 2026 13:28 UTC](https://community.onlyoffice.com/t/topic/20346/4 "2026-07-29T13:28:18Z")

</div>

是的，`doc.Search()` 是对整篇文档进行查找的。

方便说明一下您所指的"章节"具体是什么吗？例如是按页、按 Word 的节（Section），还是按标题划分的内容？

---

<div class="post-metadata">

### Author: ![xiahongyuan](https://community.onlyoffice.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xiahongyuan](https://community.onlyoffice.com/u/xiahongyuan)
#### Post date: [30 July 2026 06:05 UTC](https://community.onlyoffice.com/t/topic/20346/5 "2026-07-30T06:05:46Z")

</div>

按页、按 Word 的节（Section），还是按标题划分的内容 查找的方法分别应该怎么实现呢

---

<div class="post-metadata">

### Author: ![Nurullo](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/nurullo/32/11006_2.png) [@Nurullo](https://community.onlyoffice.com/u/Nurullo)
#### Post date: [4 August 2026 13:57 UTC](https://community.onlyoffice.com/t/topic/20346/6 "2026-08-04T13:57:43Z")

</div>

您好，

目前没有直接在指定页、节或标题范围内查找的方法——`Search()` 总是对整篇文档进行查找。这三种情况的思路是一样的：先查找整篇文档，再对返回的结果进行过滤。

js

```auto
(function() {
    let doc = Api.GetDocument();
    let ranges = doc.Search("目标文本");

    // 1. 按页过滤（页码从 0 开始：第 1 页 → 0，第 2 页 → 1，……）
    let onPage = ranges.filter(r => r.GetStartPage() === 1);

    // 2. 按节过滤（通过 GetPrevious 计算节的索引）
    function sectionIndex(sec) {
        let i = 0, s = sec.GetPrevious();
        while (s) { i++; s = s.GetPrevious(); }
        return i;
    }
    let inSection = ranges.filter(r =>
        sectionIndex(r.GetParagraph(0).GetSection()) === 1
    );

    // 3. 在两个标题之间查找
    let headings = doc.GetAllHeadingParagraphs();
    let startPos = headings[0].GetPosInParent();
    let endPos = headings[1].GetPosInParent();
    let inChapter = ranges.filter(r => {
        let p = r.GetParagraph(0).GetPosInParent();
        return p > startPos && p < endPos;
    });

    if (onPage.length > 0) onPage[0].Select();
})();

```

相关文档：

- [ApiDocument | ONLYOFFICE](https://api.onlyoffice.com/docs/office-api/usage-api/document-api/ApiDocument)
- [ApiRange | ONLYOFFICE](https://api.onlyoffice.com/docs/office-api/usage-api/document-api/ApiRange)
- [ApiParagraph | ONLYOFFICE](https://api.onlyoffice.com/docs/office-api/usage-api/document-api/ApiParagraph)
