Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup & reference functions #520

Merged
merged 23 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added 9 text functions EXACT, LOWER, UPPER, MID, T, SUBSTITUTE, REPLACE, UNICODE, UNICHAR. (#159)
- Added 11 information functions HLOOKUP, ROW, COLUMN (#158)

## [0.2.0] - 2020-09-22

### Added
- Added 9 text functions LEN, TRIM, PROPER, CLEAN, REPT, RIGHT, LEFT, SEARCH, FIND. (#221)
- Added helper methods for keeping track of cell/range dependencies: `getCellPrecedents` and `getCellDependents`. (#441)
- Added helper methods for keeping track of cell/range dependencies: getCellPrecedents and getCellDependents. (#441)
voodoo11 marked this conversation as resolved.
Show resolved Hide resolved
- Added 22 financial functions FV, PMT, PPMT, IPMT, CUMIPMT, CUMPRINC, DB, DDB, DOLLARDE, DOLLARFR, EFFECT, ISPMT, NOMINAL, NPER, RATE, PV, RRI, SLN, SYD, TBILLEQ, TBILLPRICE, TBILLYIELD. (#494)
- Added FORMULATEXT function. (PR #422)
- Added 8 information functions ISERR, ISNA, ISREF, NA, SHEET, SHEETS, ISBINARY, ISFORMULA. (#481)
Expand Down
2 changes: 1 addition & 1 deletion src/BuildEngineFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {LazilyTransformingAstService} from './LazilyTransformingAstService'
import {CellContentParser} from './CellContentParser'
import {Exporter} from './CellValue'
import {buildColumnSearchStrategy, ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {buildColumnSearchStrategy, ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {Config, ConfigParams} from './Config'
import {DateTimeHelper} from './DateTimeHelper'
import {CrudOperations} from './CrudOperations'
Expand Down
2 changes: 1 addition & 1 deletion src/CrudOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress} from './
import {CellContent, CellContentParser, isMatrix, RawCellContent} from './CellContentParser'
import {ClipboardCell, ClipboardOperations} from './ClipboardOperations'
import {AddColumnsCommand, AddRowsCommand, Operations, RemoveColumnsCommand, RemoveRowsCommand} from './Operations'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {Config} from './Config'
import {ContentChanges} from './ContentChanges'
import {DependencyGraph, SheetMapping} from './DependencyGraph'
Expand Down
9 changes: 9 additions & 0 deletions src/DependencyGraph/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@ export class DependencyGraph {
}
}

public computeListOfValuesInRange(range: AbsoluteCellRange): InternalScalarValue[] {
const values: InternalScalarValue[] = []
for (const cellFromRange of range.addresses(this)) {
const value = this.getScalarValue(cellFromRange)
values.push(value)
}
return values
}

private rangeDependencyQuery = (vertex: RangeVertex) => {
const allDeps: [(SimpleCellAddress | AbsoluteCellRange), Vertex][] = []
const {smallerRangeVertex, restRange} = this.rangeMapping.findSmallerRange(vertex.range) //checking whether this range was splitted by bruteForce or not
Expand Down
2 changes: 1 addition & 1 deletion src/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {AbsoluteCellRange} from './AbsoluteCellRange'
import {absolutizeDependencies} from './absolutizeDependencies'
import {CellError, EmptyValue, ErrorType, SimpleCellAddress} from './Cell'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {Config} from './Config'
import {ContentChanges} from './ContentChanges'
import {DateTimeHelper} from './DateTimeHelper'
Expand Down
2 changes: 1 addition & 1 deletion src/GraphBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {absolutizeDependencies} from './absolutizeDependencies'
import {CellError, simpleCellAddress, SimpleCellAddress} from './Cell'
import {CellContent, CellContentParser} from './CellContentParser'
import {CellDependency} from './CellDependency'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {Config} from './Config'
import {
DependencyGraph,
Expand Down
2 changes: 1 addition & 1 deletion src/GraphBuilderMatrixHeuristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {AbsoluteCellRange} from './AbsoluteCellRange'
import {simpleCellAddress, SimpleCellAddress} from './Cell'
import {CellContent, CellContentParser, RawCellContent} from './CellContentParser'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {DependencyGraph, MatrixVertex} from './DependencyGraph'
import {Matrix, MatrixSize} from './Matrix'
import {Sheets} from './Sheet'
Expand Down
2 changes: 1 addition & 1 deletion src/HyperFormula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {AbsoluteCellRange} from './AbsoluteCellRange'
import {CellType, CellValueType, getCellType, getCellValueType, SimpleCellAddress} from './Cell'
import {CellContent, CellContentParser, RawCellContent} from './CellContentParser'
import {CellValue, ExportedChange, Exporter, NoErrorCellValue} from './CellValue'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {Config, ConfigParams} from './Config'
import {ColumnRowIndex, CrudOperations} from './CrudOperations'
import {DateTime, numberToSimpleTime} from './DateTimeHelper'
Expand Down
25 changes: 25 additions & 0 deletions src/Lookup/AdvancedFind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {DependencyGraph} from '../DependencyGraph'
import {InternalScalarValue} from '../Cell'
import {AbsoluteCellRange} from '../AbsoluteCellRange'

export abstract class AdvancedFind {
protected constructor(
protected dependencyGraph: DependencyGraph
) {
}

public advancedFind(keyMatcher: (arg: InternalScalarValue) => boolean, range: AbsoluteCellRange): number {
const values = this.dependencyGraph.computeListOfValuesInRange(range)
for (let i = 0; i < values.length; i++) {
if (keyMatcher(values[i])) {
return i + range.start.col
}
}
return -1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {Config} from '../Config'
import {DependencyGraph} from '../DependencyGraph'
import {rangeLowerBound} from '../interpreter/binarySearch'
import {Matrix} from '../Matrix'
import {ColumnSearchStrategy} from './ColumnSearchStrategy'
import {ColumnSearchStrategy} from './SearchStrategy'
import {ColumnsSpan} from '../Span'
import {AdvancedFind} from './AdvancedFind'

export class ColumnBinarySearch implements ColumnSearchStrategy {
export class ColumnBinarySearch extends AdvancedFind implements ColumnSearchStrategy {
constructor(
private dependencyGraph: DependencyGraph,
protected dependencyGraph: DependencyGraph,
private config: Config,
) {}
) {
super(dependencyGraph)
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public add(value: InternalScalarValue | Matrix, address: SimpleCellAddress): void {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -38,30 +42,11 @@ export class ColumnBinarySearch implements ColumnSearchStrategy {

public find(key: InternalNoErrorCellValue, range: AbsoluteCellRange, sorted: boolean): number {
if (range.height() < this.config.vlookupThreshold || !sorted) {
const values = this.computeListOfValuesInRange(range)
const values = this.dependencyGraph.computeListOfValuesInRange(range)
const index = values.indexOf(key)
return index < 0 ? index : index + range.start.row
} else {
return rangeLowerBound(range, key, this.dependencyGraph)
}
}

public advancedFind(keyMatcher: (arg: InternalScalarValue) => boolean, range: AbsoluteCellRange): number {
const values = this.computeListOfValuesInRange(range)
for(let i=0; i<values.length; i++) {
if(keyMatcher(values[i])) {
return i + range.start.row
}
}
return -1
}

private computeListOfValuesInRange(range: AbsoluteCellRange): InternalScalarValue[] {
const values: InternalScalarValue[] = []
for (const cellFromRange of range.addresses(this.dependencyGraph)) {
const value = this.dependencyGraph.getScalarValue(cellFromRange)
values.push(value)
return rangeLowerBound(range, key, this.dependencyGraph, 'row')
}
return values
}
}
4 changes: 2 additions & 2 deletions src/ColumnSearch/ColumnIndex.ts → src/Lookup/ColumnIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {Matrix} from '../Matrix'
import {ColumnsSpan, RowsSpan} from '../Span'
import {Statistics, StatType} from '../statistics'
import {ColumnBinarySearch} from './ColumnBinarySearch'
import {ColumnSearchStrategy} from './ColumnSearchStrategy'
import {ColumnSearchStrategy} from './SearchStrategy'
import {AddRowsTransformer} from '../dependencyTransformers/AddRowsTransformer'
import {RemoveRowsTransformer} from '../dependencyTransformers/RemoveRowsTransformer'
import {FormulaTransformer} from '../dependencyTransformers/Transformer'
Expand Down Expand Up @@ -112,7 +112,7 @@ export class ColumnIndex implements ColumnSearchStrategy {
return rowNumber <= range.end.row ? rowNumber : this.binarySearchStrategy.find(key, range, sorted)
}

public advancedFind(keyMatcher: (arg: InterpreterValue) => boolean, range: AbsoluteCellRange): number {
public advancedFind(keyMatcher: (arg: InternalScalarValue) => boolean, range: AbsoluteCellRange): number {
return this.binarySearchStrategy.advancedFind(keyMatcher, range)
}

Expand Down
31 changes: 31 additions & 0 deletions src/Lookup/RowSearchStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {SearchStrategy} from './SearchStrategy'
import {InternalNoErrorCellValue} from '../Cell'
import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {rangeLowerBound} from '../interpreter/binarySearch'
import {Config} from '../Config'
import {DependencyGraph} from '../DependencyGraph'
import {AdvancedFind} from './AdvancedFind'

export class RowSearchStrategy extends AdvancedFind implements SearchStrategy {
constructor(
private config: Config,
protected dependencyGraph: DependencyGraph,
) {
super(dependencyGraph)
}

public find(key: InternalNoErrorCellValue, range: AbsoluteCellRange, sorted: boolean): number {
if (range.width() < this.config.vlookupThreshold || !sorted) {
const values = this.dependencyGraph.computeListOfValuesInRange(range)
const index = values.indexOf(key)
return index < 0 ? index : index + range.start.col
} else {
return rangeLowerBound(range, key, this.dependencyGraph, 'col')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import {ColumnBinarySearch} from './ColumnBinarySearch'
import {ColumnIndex} from './ColumnIndex'
import {ColumnsSpan} from '../Span'

export interface ColumnSearchStrategy {
export interface SearchStrategy {
find(key: InternalNoErrorCellValue, range: AbsoluteCellRange, sorted: boolean): number,

advancedFind(keyMatcher: (arg: InternalScalarValue) => boolean, range: AbsoluteCellRange): number,
}

export interface ColumnSearchStrategy extends SearchStrategy {
add(value: InterpreterValue | Matrix, address: SimpleCellAddress): void,

remove(value: InterpreterValue | Matrix | null, address: SimpleCellAddress): void,
Expand All @@ -31,10 +37,6 @@ export interface ColumnSearchStrategy {

removeValues(range: IterableIterator<[InternalScalarValue, SimpleCellAddress]>): void,

find(key: InternalNoErrorCellValue, range: AbsoluteCellRange, sorted: boolean): number,

advancedFind(keyMatcher: (arg: InterpreterValue) => boolean, range: AbsoluteCellRange): number,

destroy(): void,
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {EmptyValue, invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddre
import {CellContent, CellContentParser, RawCellContent} from './CellContentParser'
import {ColumnsSpan, RowsSpan} from './Span'
import {ContentChanges} from './ContentChanges'
import {ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {absolutizeDependencies} from './absolutizeDependencies'
import {LazilyTransformingAstService} from './LazilyTransformingAstService'
import {buildMatrixVertex} from './GraphBuilder'
Expand Down
2 changes: 1 addition & 1 deletion src/error-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ErrorMessage {
public static NotOctal = 'String does not represent an octal number.'
public static NotHex = 'String does not represent a hexadecimal number.'
public static EndStartPeriod = 'End period needs to be at least start period.'
public static CellRef = 'Cell reference expected.'
public static CellRefExpected = 'Cell reference expected.'
public static BadRef = 'Address is not correct.'
public static NumberRange = 'Number-only range expected.'
public static ValueNotFound = 'Value not found.'
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/languages/csCZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dictionary: RawTranslationPackage = {
CHOOSE: 'ZVOLIT',
CLEAN: 'VYČISTIT',
CODE: 'KÓD',
COLUMN: 'SLOUPEC',
COLUMNS: 'SLOUPCE',
CONCATENATE: 'CONCATENATE',
CORREL: 'CORREL',
Expand Down Expand Up @@ -94,6 +95,7 @@ const dictionary: RawTranslationPackage = {
HEX2BIN: 'HEX2BIN',
HEX2DEC: 'HEX2DEC',
HEX2OCT: 'HEX2OCT',
HLOOKUP: 'VVYHLEDAT',
HOUR: 'HODINA',
IF: 'KDYŽ',
IFERROR: 'IFERROR',
Expand Down Expand Up @@ -161,6 +163,7 @@ const dictionary: RawTranslationPackage = {
ROUND: 'ZAOKROUHLIT',
ROUNDDOWN: 'ROUNDDOWN',
ROUNDUP: 'ROUNDUP',
ROW: 'ŘÁDEK',
ROWS: 'ŘÁDKY',
RRI: 'RRI',
SEARCH: 'HLEDAT',
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/languages/daDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dictionary: RawTranslationPackage = {
CHOOSE: 'VÆLG',
CLEAN: 'RENS',
CODE: 'KODE',
COLUMN: 'KOLONNE',
COLUMNS: 'KOLONNER',
CONCATENATE: 'SAMMENKÆDNING',
CORREL: 'KORRELATION',
Expand Down Expand Up @@ -94,6 +95,7 @@ const dictionary: RawTranslationPackage = {
HEX2BIN: 'HEX.TIL.BIN',
HEX2DEC: 'HEX.TIL.DEC',
HEX2OCT: 'HEX.TIL.OKT',
HLOOKUP: 'VOPSLAG',
HOUR: 'TIME',
IF: 'HVIS',
IFERROR: 'HVIS.FEJL',
Expand Down Expand Up @@ -161,6 +163,7 @@ const dictionary: RawTranslationPackage = {
ROUND: 'AFRUND',
ROUNDDOWN: 'RUND.NED',
ROUNDUP: 'RUND.OP',
ROW: 'RÆKKE',
ROWS: 'RÆKKER',
RRI: 'RRI',
SEARCH: 'SØG',
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/languages/deDE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dictionary: RawTranslationPackage = {
CHOOSE: 'WAHL',
CLEAN: 'SÄUBERN',
CODE: 'CODE',
COLUMN: 'SPALTE',
COLUMNS: 'SPALTEN',
CONCATENATE: 'VERKETTEN',
CORREL: 'KORREL',
Expand Down Expand Up @@ -94,6 +95,7 @@ const dictionary: RawTranslationPackage = {
HEX2BIN: 'HEXINBIN',
HEX2DEC: 'HEXINDEZ',
HEX2OCT: 'HEXINOKT',
HLOOKUP: 'WVERWEIS',
HOUR: 'STUNDE',
IF: 'WENN',
IFERROR: 'WENNFEHLER',
Expand Down Expand Up @@ -161,6 +163,7 @@ const dictionary: RawTranslationPackage = {
ROUND: 'RUNDEN',
ROUNDDOWN: 'ABRUNDEN',
ROUNDUP: 'AUFRUNDEN',
ROW: 'ZEILE',
ROWS: 'ZEILEN',
RRI: 'ZSATZINVEST',
SEARCH: 'SUCHEN',
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/languages/enGB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dictionary: RawTranslationPackage = {
CHOOSE: 'CHOOSE',
CLEAN: 'CLEAN',
CODE: 'CODE',
COLUMN: 'COLUMN',
COLUMNS: 'COLUMNS',
CONCATENATE: 'CONCATENATE',
CORREL: 'CORREL',
Expand Down Expand Up @@ -94,6 +95,7 @@ const dictionary: RawTranslationPackage = {
HEX2BIN: 'HEX2BIN',
HEX2DEC: 'HEX2DEC',
HEX2OCT: 'HEX2OCT',
HLOOKUP: 'HLOOKUP',
HOUR: 'HOUR',
IF: 'IF',
IFERROR: 'IFERROR',
Expand Down Expand Up @@ -161,6 +163,7 @@ const dictionary: RawTranslationPackage = {
ROUND: 'ROUND',
ROUNDDOWN: 'ROUNDDOWN',
ROUNDUP: 'ROUNDUP',
ROW: 'ROW',
ROWS: 'ROWS',
RRI : 'RRI',
SEARCH: 'SEARCH',
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/languages/esES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const dictionary: RawTranslationPackage = {
CHOOSE: 'ELEGIR',
CLEAN: 'LIMPIAR',
CODE: 'CODIGO',
COLUMN: 'COLUMNA',
COLUMNS: 'COLUMNAS',
CONCATENATE: 'CONCATENAR',
CORREL: 'COEF.DE.CORREL',
Expand Down Expand Up @@ -94,6 +95,7 @@ export const dictionary: RawTranslationPackage = {
HEX2BIN: 'HEX.A.BIN',
HEX2DEC: 'HEX.A.DEC',
HEX2OCT: 'HEX.A.OCT',
HLOOKUP: 'BUSCARH',
HOUR: 'HORA',
IF: 'SI',
IFERROR: 'SI.ERROR',
Expand Down Expand Up @@ -161,6 +163,7 @@ export const dictionary: RawTranslationPackage = {
ROUND: 'REDONDEAR',
ROUNDDOWN: 'REDONDEAR.MENOS',
ROUNDUP: 'REDONDEAR.MAS',
ROW: 'FILA',
ROWS: 'FILAS',
RRI: 'RRI',
SEARCH: 'HALLAR',
Expand Down
Loading