类: FTextFinder
This interface class provides methods to find and replace text in the univer.
继承
any
实现
IFTextFinder
方法
ensureCompleteAsync()
ensureCompleteAsync(): Promise<any>
Ensure the find operation is completed. Especially when the current sheet changed use this method to ensure the find operation is completed.
返回
Promise
<any
>
The find complete result.
示例
// Create a text-finder to find the text '1'.
const textFinder = await univerAPI.createTextFinderAsync('1');
// Find all cells that contain the text '1'.
const matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation());
});
const fWorkbook = univerAPI.getActiveWorkbook();
const sheets = fWorkbook.getSheets();
// Change the current sheet to the second sheet.
sheets[1]?.activate();
// Ensure the find operation is completed of the current sheet.
await textFinder.ensureCompleteAsync();
const matchCells2 = textFinder.findAll();
matchCells2.forEach((cell) => {
console.log(cell.getA1Notation());
});
实现了
IFTextFinder.ensureCompleteAsync
findAll()
findAll(): FRange[]
Get all the matched cells of the current sheet, the current matched cell is the last matched cell.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
返回
FRange
[]
All the matched cells.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
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) => {
console.log(cell.getA1Notation()); // D2, C3, B4, A5
});
实现了
IFTextFinder.findAll
findNext()
findNext(): Nullable<FRange>
Get 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.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
返回
Nullable
<FRange
>
The next matched cell.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
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');
console.log(textFinder.getCurrentMatch().getA1Notation()); // current match cell is A5
// Find the next matched range
const nextMatch = textFinder.findNext();
console.log(nextMatch.getA1Notation()); // D2
console.log(textFinder.getCurrentMatch().getA1Notation()); // current match cell is D2
实现了
IFTextFinder.findNext
findPrevious()
findPrevious(): Nullable<FRange>
Get 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.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
返回
Nullable
<FRange
>
The previous matched cell.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
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');
console.log(textFinder.getCurrentMatch().getA1Notation()); // current match cell is A5
// Find the previous matched range.
const previousMatch = textFinder.findPrevious();
console.log(previousMatch.getA1Notation()); // B4
console.log(textFinder.getCurrentMatch().getA1Notation()); // current match cell is B4
实现了
IFTextFinder.findPrevious
getCurrentMatch()
getCurrentMatch(): Nullable<FRange>
Get the current matched cell of the current sheet.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
返回
Nullable
<FRange
>
The current matched cell.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
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');
// Get the current matched range.
const currentMatch = textFinder.getCurrentMatch();
console.log(currentMatch.getA1Notation()); // A5
实现了
IFTextFinder.getCurrentMatch
matchCaseAsync()
matchCaseAsync(matchCase): Promise<IFTextFinder>
Set the match case option, if true, the find operation will match case, otherwise, the find operation will ignore case.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
参数
参数 | 类型 | 描述 |
---|---|---|
matchCase | boolean | Whether to match case. |
返回
Promise
<IFTextFinder
>
The text-finder instance.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:D1');
fRange.setValues([
['hello univer', 'hello UNIVER', 'HELLO UNIVER', 'HELLO univer'],
]);
// Create a text-finder to find the text 'univer'.
const textFinder = await univerAPI.createTextFinderAsync('univer');
let matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1, B1, C1, D1
});
// Set the match case.
await textFinder.matchCaseAsync(true);
matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1, D1
});
实现了
IFTextFinder.matchCaseAsync
matchEntireCellAsync()
matchEntireCellAsync(matchEntireCell): Promise<IFTextFinder>
Set 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.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
参数
参数 | 类型 | 描述 |
---|---|---|
matchEntireCell | boolean | Whether to match entire cell value. |
返回
Promise
<IFTextFinder
>
The text-finder instance.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:D1');
fRange.setValues([
['hello univer', 'hello univer 1', 'hello univer 2', 'hello univer 3'],
]);
// Create a text-finder to find the text 'hello univer'.
const textFinder = await univerAPI.createTextFinderAsync('hello univer');
let matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1, B1, C1, D1
});
// Set the match entire cell.
await textFinder.matchEntireCellAsync(true);
matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1
});
实现了
IFTextFinder.matchEntireCellAsync
matchFormulaTextAsync()
matchFormulaTextAsync(matchFormulaText): Promise<IFTextFinder>
Set the match formula text option, if true, the find operation will match formula text, otherwise, the find operation will match value.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
参数
参数 | 类型 | 描述 |
---|---|---|
matchFormulaText | boolean | Whether to match formula text. |
返回
Promise
<IFTextFinder
>
The text-finder instance.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:D1');
fRange.setValues([
['sum', '1', '=SUM(2)', '3'],
]);
// Create a text-finder to find the text 'sum'.
const textFinder = await univerAPI.createTextFinderAsync('sum');
let matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1
});
// Set the match entire cell.
await textFinder.matchFormulaTextAsync(true);
matchCells = textFinder.findAll();
matchCells.forEach((cell) => {
console.log(cell.getA1Notation()); // A1, C1
});
实现了
IFTextFinder.matchFormulaTextAsync
replaceAllWithAsync()
replaceAllWithAsync(replaceText): Promise<number>
Replace all the matched text with the given text.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
参数
参数 | 类型 | 描述 |
---|---|---|
replaceText | string | The text to replace. |
返回
Promise
<number
>
The count of replaced text.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:D1');
fRange.setValues([
['hello', 'hello', 'hello', 'hello'],
]);
// Create a text-finder to find the text 'hello'.
const textFinder = await univerAPI.createTextFinderAsync('hello');
// Replace all the matched text with 'hello univer'.
const count = await textFinder.replaceAllWithAsync('hello univer');
console.log(count); // 4
console.log(fRange.getValues()); // [['hello univer', 'hello univer', 'hello univer', 'hello univer']]
实现了
IFTextFinder.replaceAllWithAsync
replaceWithAsync()
replaceWithAsync(replaceText): Promise<boolean>
Replace the current matched text with the given text.
If current sheet changed, use await textFinder.ensureCompleteAsync()
to ensure the find operation is completed.
参数
参数 | 类型 | 描述 |
---|---|---|
replaceText | string | The text to replace. |
返回
Promise
<boolean
>
Whether the replace is successful.
抛出
If the find operation is not completed.
示例
// Assume the current sheet is empty sheet.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('B1:E1');
fRange.setValues([
['hello', 'hello', 'hello', 'hello'],
]);
// Create a text-finder to find the text 'hello'.
const textFinder = await univerAPI.createTextFinderAsync('hello');
// Replace the current matched text with 'hello univer'.
const replaced = await textFinder.replaceWithAsync('hello univer');
console.log(replaced); // true
console.log(fRange.getValues()); // [['hello', 'hello', 'hello', 'hello univer']]
实现了
IFTextFinder.replaceWithAsync