Skip to main content

SSF 数字格式化程序

"数字格式" 中所述,现代电子表格文件格式将 "content" 与 "presentation" 分开。应用将存储基础值 (3.50) 和数字格式 ($0.00),而不是存储像 $3.50 这样的格式化值。解析器应该渲染值。

¥As explained in "Number Formats", modern spreadsheet file formats separate "content" from "presentation". Instead of storing a formatted value like $3.50, applications will store the underlying value (3.50) and the number format ($0.00). Parsers are expected to render values.

SheetJS SSF ("电子表格格式") 库根据 Excel 和其他电子表格软件中定义的数字格式化规则来格式化数字 [^1]

¥The SheetJS SSF ("SpreadSheet Format") library formats numbers according to the number formatting rules defined in Excel and other spreadsheet software[^1]

该库的一个版本随主文件处理库一起提供。它深度集成在 SheetJS CE API 函数中,包括 read[^2]、write[^3] 和 sheet_to_json[^4]。

¥A version of the library ships with the main file processing library. It is deeply integrated in SheetJS CE API functions including read[^2], write[^3], and sheet_to_json[^4].

该库还可在 SheetJS CDN[^5] 上独立使用。

¥The library is also available for standalone use on the SheetJS CDN[^5].

源代码和项目文档托管在位于 https://git.sheetjs.com/sheetjs/sheetjs/src/branch/master/packages/ssf 的 SheetJS git 服务器上

¥Source code and project documentation are hosted on the SheetJS git server at https://git.sheetjs.com/sheetjs/sheetjs/src/branch/master/packages/ssf

在线演示

¥Live Demo

格式化文本是根据指定的数字格式和值计算的。如果不支持特定格式,请 报告问题

¥The formatted text is calculated from the specified number format and value. Please report an issue if a particular format is not supported.

Result
Loading...
Live Editor
function SheetJSSSF() {
  const [fmt, setFmt] = React.useState("#,##0");
  const [val, setVal] = React.useState(7262);
  const [text, setText] = React.useState("");

  React.useEffect(() => {
    if(typeof SSF == "undefined") return setText("ERROR: Reload this page!");
    let v = +val;
    if(!isFinite(v)) return setText(`ERROR: ${val} is not a valid number!`);
    try {
      setText(SSF.format(fmt, v));
    } catch(e) { setText("ERROR: " + (e && e.message || e)); }
  }, [fmt, val]);
  const goodstyle = { backgroundColor: "#C6EFCE", color: "#006100" };
  const badstyle = { backgroundColor: "#FFC7CE", color: "#9C0006" };

  return ( <table>
    <tr><td><b>Number Format</b></td><td>
      <input type="text" value={fmt} onChange={e => setFmt(e.target.value)}/>
    </td></tr>
    <tr><td><b>Number Value</b></td><td>
      <input type="text" value={val} onChange={e => setVal(e.target.value)}/>
    </td></tr>
    <tr><td colspan="2"></td></tr>
    <tr><td><b>Formatted Text</b></td><td>
      <code style={/ERROR/.test(text)?badstyle:goodstyle}>{text}</code>
    </td></tr>
  </table> );
}

[^1]: ECMA-376 中概述了数字格式规则。MS-XLS 规范中定义了粗略的语法。

¥The number formatting rules are sketched in ECMA-376. A rough grammar is defined in the MS-XLS specification.

[^2]: 见 read 于 "读取文件"

¥See read in "Reading Files"

[^3]: 见 write 于 "写入文件"

¥See write in "Writing Files"

[^4]: 见 sheet_to_json 于 "实用工具"

¥See sheet_to_json in "Utilities"

[^5]: 详细信息请参见 https://cdn.sheetjs.com/ssf/

¥See https://cdn.sheetjs.com/ssf/ for more details.