Skip to content

parseAttributeValue 将单元格坐标 "E1" 误判为科学计数法并转成 null,导致属性值丢失 #888

Open

Description

@whmdg20090421

背景与实际问题

我在开发一个 Android 离线 WebView 里的 .xlsx 只读预览/编辑器,底层用 fast-xml-parser 解析原始 worksheet XML。

xlsx 里的单元格坐标(如 E1)会出现在工作表超链接的属性上:

<hyperlink ref="E1" location="Sheet2!A1"/>
  • ref="E1" —— 这个超链接贴在哪个单元格(左上角坐标,随所在位置变化,可能是 A1/B5/E1/F1…)
  • location="Sheet2!A1" —— 点击后跳转的目标

也就是说,E1 在这里只是一个普通的单元格地址字符串,既不是整数、也不是科学计数法,它本应被当作字符串原样保留。

问题

开启 parseAttributeValue: true 后,fast-xml-parser 会把属性值自动转成数字。它对 E1 的判定是:看起来像科学计数法 E1(= 1×10¹),尝试转换后得到 null,结果把整个坐标属性吃掉了:

const { XMLParser } = require('fast-xml-parser') // 4.5.7 / 4.x

const p = new XMLParser({
  ignoreAttributes: false,
  attributeNamePrefix: '@_',
  parseAttributeValue: true,
  trimValues: true,
})

p.parse('<hyperlink ref="E1" location="Sheet2!A1"/>')

实际输出:

{ "hyperlink": { "@_ref": null, "@_location": "Sheet2!A1" } }

期望输出(或至少不能丢原值):

{ "hyperlink": { "@_ref": "E1", "@_location": "Sheet2!A1" } }

对比:同一份文件里 ref="F1"、ref="B5" 都能正常保留为字符串,唯独 E1(以及任何形如 字母+E+两位数字 的值)被转成 null。在我这个 xlsx 里,有两张表的返回链接 ref="E1" 因此整条丢失、格子里点不动;而另一张表是 ref="F1" 就正常。

影响面

E1/E2/E10… 这类单元格坐标在 OOXML(表格、图表引用、定义名称等)里极常见。凡是开启 parseAttributeValue 的解析场景,只要出现这类坐标,就会被静默转成 null,造成数据丢失,且很难排查。

临时规避

numberParseOptions: { eNotation: false }

设置后 E1 会被保留为字符串,其它数字属性仍照常转换。

关于 eNotation

这个行为由 numberParseOptions.eNotation(默认 true)控制。默认开启时,很多纯粹是"字符串"的标识值只要长得像科学计数法,就会被当成数字处理——对 XML 这种以字符串属性为主的数据格式,风险较高,且转换失败后直接变 null(而非保留原字符串),进一步放大了问题。

环境

  • fast-xml-parser: 4.5.0(本地实测 4.5.7 同样复现)
  • Node / 浏览器均复现

(说明:我这边只用到解析能力,不确定在此之外是否有其它依赖;如已存在同类 issue,烦请合并,谢谢。)

Activity

  1. github-actions commented on Oct 9, 2026

    @github-actions

    We're glad you find this project helpful. We'll try to address this issue ASAP. You can join Discord channel(https://discord.gg/X4Qp9u6Vz) for anonucements and pre release discussions. Don't forget to star this repo.

  2. amitguptagwl commented on Oct 10, 2026

    @amitguptagwl
    Member

    Thanks for reporting this. This helped to find a bug in strnum package which is fixed and released now. However, since you're using old version of this library. I would suggest to switch to the new version or use flexible-xml-parser if possible.

    I'll see meanwhile what I can do for old version.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions