Skip to content

[Bug]: FIND returns start_num - 1 instead of #VALUE! when text is not found and start_num > 1 #1883

Open

Description

@trackoor

Description

FIND(find_text, within_text, start_num) returns a number when find_text does not occur at or after start_num. It should return #VALUE!. With start_num = 1 the error is returned as expected.

const { HyperFormula } = require('hyperformula')

const hf = HyperFormula.buildFromArray([[
  '=FIND("l", "Hello", 5)',
  '=FIND("z", "Hello", 2)',
  '=FIND("z", "Hello", 1)',
  '=SEARCH("z", "Hello", 2)',
]], { licenseKey: 'gpl-v3' })

console.log(hf.getSheetValues(0)[0].map(v => v?.value ?? v))

Actual: [ 4, 1, '#VALUE!', '#VALUE!' ]

Expected (Excel, LibreOffice): [ '#VALUE!', '#VALUE!', '#VALUE!', '#VALUE!' ]

So a formula like =IFERROR(FIND("-", A1, 3), 0) gets a wrong position instead of the fallback. SEARCH returns #VALUE! as expected.

Cause: TextPlugin.find computes shiftedText.indexOf(pattern) + startIndex and only checks index > 0, so a miss (-1) becomes startIndex - 1, which is positive whenever startIndex > 1.

Excel reference: https://support.microsoft.com/en-us/office/find-findb-functions-c7912941-af2a-4bdf-a553-d0d89b0a0628

Video or screenshots

N/A

Demo

Not applicable: the snippet above runs with node after npm i hyperformula.

HyperFormula version

3.5.0 (npm), also develop at 5a6eb09

Your framework

None (Node.js)

Your environment

Node.js 26.13.0, Linux

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