详细使用说明Usage Guide

安装、作为组件或库使用、公式语法、组件门面 API、IO 读写、事件——最后附一个可现场编辑并立刻运行的 playground。Install, use as a component or a library, formula syntax, the facade API, IO, events — and a live playground you can edit and run on the spot.

上手GET STARTED

两种用法:元素 或 库Two ways: element or library

🧩 作为 Web ComponentAs a Web Component

<!-- 注册 <cmx-megasheet> -->
<script type="module">
  import './dist/index.js'
  const el = document.getElementById('sheet')
  el.setTheme('light')
  el.loadWorkbook((wb) => {
    wb.clearSheets()
    const s = new CmxMegaSheet.Worksheet('Sheet1', {
      rowCount: 100, colCount: 26, styleSheet: wb.styleSheet })
    s.getCell(0,0).value('销售')
    s.getCell(1,0).value(100)
    s.getCell(2,0).formula('=A2*1.1')
    wb.appendSheet(s)
  })
</script>

📚 作为库(无 DOM)As a library (DOM-free)

import {
    Workbook, Worksheet,
    FormulaEngine, workbookToJSON
  } from '@cmx/megasheet'

  const wb = new Workbook({ sheetCount: 0 })
  const ws = new Worksheet('Sheet1', {
    rowCount: 20, colCount: 10, styleSheet: wb.styleSheet })
  wb.appendSheet(ws)
  ws.setValue(0,0, 42)
  ws.setFormula(1,0, 'SUM(A1:A1)*2')
  new FormulaEngine(wb).recalcAll()  // A2 → 84
  const json = workbookToJSON(wb) // 中性快照

⚙️ 构建与验证(仅开发期,运行时零依赖)Build & verify (dev-only; zero runtime deps)

npm install      # 装 devDeps(typescript / vitest)
npm run build    # tsc → dist(含 .d.ts)
npm test         # vitest(945 单测)
node demo/check-demo.mjs  # headless Chrome 逐页自检 + 零 console error
组件门面FACADE API

<cmx-megasheet> 公共方法<cmx-megasheet> public methods

约 150 个方法,按域分组(工具栏留宿主,组件边界 = 公式栏 + 画布 + 页签栏 + 主题)。~150 methods grouped by domain (toolbar left to host; boundary = formula bar + canvas + tabs + theme).

数据 / 装载Data / load
loadWorkbook · setReportModel · setCellValues · getWorkbook(逃生舱escape hatch) · recalc
样式Style
applySelectionStyle · applySelectionBorder · applySelectionFill(图案/渐变pattern/gradient) · setCellsLocked
编辑Edit
undo/redo/undoSteps · merge/unmergeSelection · insert/deleteRows/Columns · copyFormat/pasteFormat
剪贴板Clipboard
copySelection · pasteSpecial(值/公式/格式/转置/运算/跳空values/formulas/formats/transpose/op/skip-blanks)
数据工具Data tools
sortRange · setAutoFilter · find/replaceAll · textToColumns · removeDuplicates · consolidate
窗格Panes
freezePanes · freezeTrailing(尾冻结trailing) · splitPanes · showCell · setZoom
类型/验证Types/validation
setDataValidation · setCheckbox · setHyperlink · addConditionalRule · setRichText
浮动对象Floating
insertChart(11 类11 types) · insertImage · insertShape · addComment · setSparkline
IOIO
exportXlsx/importXlsx · exportCsv/importCsv · exportHtml · exportPdf · getWorkbookJson/setWorkbookJson
主题/视图Theme/view
setTheme(light/dark/auto) · setEditable · showFormulaBar/showHeaders/showGridlines/showTabs

🔣 公式语法Formula syntax

直接求值不落格:el.evaluateFormula("SUM(A1:A5)")Evaluate without writing a cell: el.evaluateFormula("SUM(A1:A5)").

# 相对 / 绝对
=A1 + $B$2
# 跨表 · 整列 · 整行
=Sheet1!A1     =SUM(A:A)    =SUM(1:1)
# 数组字面量 · 命名区域
=SUM({1,2;3,4})           # → 10
=SUM(SALES)               # defineName('SALES','A1:A5')
# 查找 · 多条件聚合
=VLOOKUP(40, A1:B5, 2, FALSE)
=SUMIFS(A1:A5, B1:B5, "x")

事件(CustomEvent)Events (CustomEvent)

el.addEventListener('cmx-cell-selected', e => {
  console.log(e.detail.addr)  // 'B2'
})
cmx-cell-selected · cmx-cell-edited · cmx-sheet-changed · cmx-col-resized · cmx-row-resized · cmx-edit-rejected

报表取数 QM/QC/JE/FS/REF 的真值由后端算好经 setReportValueMap 下发,引擎只做聚合层重算。Report fetches QM/QC/JE/FS/REF get their truth from the backend via setReportValueMap; the engine only re-aggregates.

试一试PLAYGROUND

现场编辑,立刻运行Edit and run, live

改左边代码点「运行」,右边真 <cmx-megasheet> 立即重铺。M = CmxMegaSheetel = 表格元素。Edit the code and hit Run; the real <cmx-megasheet> on the right re-renders. M = CmxMegaSheet, el = the element.

playground.js
<cmx-megasheet>