Skip to content

[Bug]: Wildcard matching in MATCH and ?, and SUBSTITUTE with empty old_text, differ from Excel #1887

Open

Description

@trackoor

Three bugs, each with its own repro. Expected values are from LibreOffice Calc 26.2.7 on an .xlsx file.

HyperFormula version

3.5.0 (npm), Node 20.20.2, Linux. No framework.

Demo

Each snippet runs with node after npm i hyperformula@3.5.0.

1. MATCH with match_type 0 ignores * and ?

VLOOKUP and COUNTIF handle wildcards, but MATCH(..., 0) treats them as literal characters.

const { HyperFormula } = require('hyperformula')
const hf = HyperFormula.buildFromArray([
  ['apple', '=MATCH("b*", A1:A3, 0)', '=MATCH("?ig", A1:A3, 0)', '=VLOOKUP("b*", A1:A3, 1, 0)'],
  ['banana'], ['fig'],
], { licenseKey: 'gpl-v3' })
console.log(hf.getSheetValues(0)[0].slice(1).map(v => v?.value ?? v))

Actual: [ '#N/A', '#N/A', 'banana' ]
Expected: [ 2, 3, 'banana' ]

LookupPlugin.doMatch calls searchStrategy.find directly instead of going through searchInRange like the other lookups.

2. ? matches zero or one character instead of exactly one

escapeNonWildcards in src/interpreter/ArithmeticHelper.ts turns ? into .? instead of ..

const { HyperFormula } = require('hyperformula')
const hf = HyperFormula.buildFromArray([
  ['apple', 1, '=COUNTIF(A1:A3, "?????")', '=SUMIF(A1:A3, "?????", B1:B3)', '=VLOOKUP("fi?g", A1:B3, 2, 0)'],
  ['banana', 2], ['fig', 4],
], { licenseKey: 'gpl-v3' })
console.log(hf.getSheetValues(0)[0].slice(2).map(v => v?.value ?? v))

Actual: [ 2, 5, 4 ]
Expected: [ 1, 1, '#N/A' ]

3. SUBSTITUTE with empty old_text inserts new_text everywhere

An empty old_text (or a blank cell) should leave the text unchanged. TextPlugin.substitute builds new RegExp("", "g") instead.

const { HyperFormula } = require('hyperformula')
const hf = HyperFormula.buildFromArray([[
  'abc', null, '=SUBSTITUTE(A1, B1, "x")', '=SUBSTITUTE("abc", "", "x")', '=SUBSTITUTE("abc", "", "x", 1)',
]], { licenseKey: 'gpl-v3' })
console.log(hf.getSheetValues(0)[0].slice(2))

Actual: [ 'xaxbxcx', 'xaxbxcx', 'xabc' ]
Expected: [ 'abc', 'abc', 'abc' ]

Found with a differential-testing tool; the report was drafted with AI assistance and reviewed and edited by me.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions