Find & Replace

GitHubEdit on GitHub
Preset Info
@univerjs/preset-sheets-find-replace
Server Required
No

The Find & Replace feature allows users to quickly search for specific content in spreadsheets and replace it, supporting various matching options to help users efficiently process data.

Preset Mode

Installation

npm install @univerjs/preset-sheets-find-replace

Usage

import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import { UniverSheetsFindReplacePreset } from '@univerjs/preset-sheets-find-replace'
import UniverPresetSheetsFindReplaceEnUS from '@univerjs/preset-sheets-find-replace/locales/en-US'
import { createUniver, LocaleType, merge } 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.En_US,
  locales: {
    [LocaleType.En_US]: merge(
      {},
      UniverPresetSheetsCoreEnUS,
      UniverPresetSheetsFindReplaceEnUS, 
    ),
  },
  presets: [
    UniverSheetsCorePreset(),
    UniverSheetsFindReplacePreset(), 
  ],
})

Plugin Mode

Installation

npm install @univerjs/find-replace @univerjs/sheets-find-replace

Usage

import { LocaleType, merge, Univer } from '@univerjs/core'
import { UniverFindReplacePlugin } from '@univerjs/find-replace'
import FindReplaceEnUS from '@univerjs/find-replace/locale/en-US'
import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace'
import SheetsFindReplaceEnUS from '@univerjs/sheets-find-replace/locale/en-US'

import '@univerjs/find-replace/lib/index.css'

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

const univer = new Univer({
  locale: LocaleType.En_US,
  locales: {
    [LocaleType.En_US]: merge(
      {},
      FindReplaceEnUS, 
      SheetsFindReplaceEnUS, 
    ),
  },
})

univer.registerPlugin(UniverFindReplacePlugin) 
univer.registerPlugin(UniverSheetsFindReplacePlugin) 

Facade API

Complete Facade API type definitions can be found in the FacadeAPI.

创建文本查找器

univerAPI.createTextFinderAsync() creates a text finder and returns an FTextFinder instance.

Here are some member methods on FTextFinder:

MethodDescription
findAllGet all the matched cells of the current sheet, the current matched cell is the last matched cell
findNextGet the next matched cell of the current sheet, if exists return the next matched cell and move the current matched cell to the next matched cell
findPreviousGet the previous matched cell of the current sheet, if exists return the previous matched cell and move the current matched cell to the previous matched cell
getCurrentMatchGet the current matched cell of the current sheet
matchCaseAsyncSet the match case option, if true, the find operation will match case, otherwise, the find operation will ignore case
matchEntireCellAsyncSet the match entire cell option, if true, the find operation will match entire cell value, otherwise, the find operation will match part of the cell value
matchFormulaTextAsyncSet the match formula text option, if true, the find operation will match formula text, otherwise, the find operation will match value
replaceAllWithAsyncReplace all the matched text with the given text
replaceWithAsyncReplace the current matched text with the given text
ensureCompleteAsyncEnsure the find operation is completed. Especially when the current sheet changed use this method to ensure the find operation is completed
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Set some values to the range 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],
])

// Create a text-finder to find the text '5'.
const textFinder = await univerAPI.createTextFinderAsync('5')

// Find all cells that contain the text '5'.
const matchCells = textFinder.findAll()
matchCells.forEach((cell) => {
  cell.getA1Notation() // D2, C3, B4, A5
})

How is this guide?