尋找與取代

查詢替換可以在表格中查詢文字並將其替換。匹配選項控制查詢的嚴格程度。

在下面的完整外掛範例中可以開啟查詢替換面板試用該功能。

外掛模式

安裝

Shell
pnpm add @univerjs/find-replace @univerjs/sheets-find-replace

使用

TypeScript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverFindReplacePlugin } from '@univerjs/find-replace'import FindReplaceZhTW from '@univerjs/find-replace/locale/zh-TW'import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace'import '@univerjs/find-replace/lib/index.css'import '@univerjs/sheets-find-replace/facade'const univer = new Univer({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      FindReplaceZhTW,     ),  },})univer.registerPlugin(UniverFindReplacePlugin)univer.registerPlugin(UniverSheetsFindReplacePlugin)

行動裝置支援

先配置行動版編輯器。在本頁外掛模式的初始化程式碼中,使用同一套件匯出的行動版外掛替換下列桌面版註冊,替換必須在開啟文件前完成。保留模型外掛、樣式、語言套件和 Facade 匯入,不要同時註冊兩個版本。

套件桌面版註冊行動版註冊
@univerjs/find-replaceUniverFindReplacePluginUniverFindReplaceMobileUIPlugin
@univerjs/sheets-find-replaceUniverSheetsFindReplacePluginUniverSheetsFindReplaceMobileUIPlugin

預設模式

安裝

Shell
pnpm add @univerjs/preset-sheets-find-replace

使用

TypeScript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsFindReplacePreset } from '@univerjs/preset-sheets-find-replace'import UniverPresetSheetsFindReplaceZhTW from '@univerjs/preset-sheets-find-replace/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-find-replace/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsFindReplaceZhTW,     ),  },  presets: [    UniverSheetsCorePreset(),    UniverSheetsFindReplacePreset(),   ],})

Facade API

完整的 Facade API 型別定義見 FacadeAPI。

引入

外掛模式提示

只有外掛模式需要手動引入 Facade 包。預設模式已內建對應的 Facade 包,無需單獨匯入。

TypeScript
import '@univerjs/sheets-find-replace/facade'

建立文字查詢器

呼叫 univerAPI.createTextFinderAsync() 建立文字查詢器,返回 FTextFinder 例項。

下面列出 FTextFinder 上的一些方法。

方法描述
findAll返回當前表格中的所有匹配項,當前匹配位置停在最後一個。
findNext返回當前表格中的下一個匹配單元格,並將當前匹配位置移動到該單元格。
findPrevious返回當前表格中的上一個匹配單元格,並將當前匹配位置移動到該單元格。
getCurrentMatch返回當前表格中當前匹配的單元格。
matchCaseAsync設定是否區分大小寫。為 true 時區分大小寫,否則忽略大小寫。
matchEntireCellAsync設定是否匹配整個單元格。為 true 時要求整個單元格的值一致,否則單元格中有一部分匹配即可。
matchFormulaTextAsync設定是否匹配公式文字。為 true 時查詢公式文字,否則查詢值。
replaceAllWithAsync用給定文字替換所有匹配項。
replaceWithAsync用給定文字替換當前匹配項。
ensureCompleteAsync確保查詢已完成。切換當前表格後,尤其需要透過該方法確認。
TypeScript
// 假設當前表格是空表const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 設定一些值到 A1:D10 範圍內const fRange = fWorksheet.getRange('A1:D10')fRange.setValues([  [1, 2, 3, 4],  [2, 3, 4, 5],  [3, 4, 5, 6],  [4, 5, 6, 7],  [5, 6, 7, 8],  [6, 7, 8, 9],  [7, 8, 9, 10],  [8, 9, 10, 11],  [9, 10, 11, 12],  [10, 11, 12, 13],])// 建立一個查詢文字 '5' 的查詢器const textFinder = await univerAPI.createTextFinderAsync('5')// 找到所有包含文字 '5' 的單元格const matchCells = textFinder.findAll()matchCells.forEach((cell) => {  cell.getA1Notation() // D2, C3, B4, A5})

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.