历史记录
修改单元格内容、公式或样式后,可以打开历史记录查看之前的版本、对比变化,并在具备相应权限时恢复某个版本。历史记录读取的是服务端保存的版本。撤销和重做仅作用于当前编辑会话,不需要历史服务。
先准备协同和历史服务
先完成协同集成和许可证配置,再验证两个会话可以编辑同一份 Sheets 文档,且刷新后改动仍然保留。
参照 Office 协同扩展模块和 History 示例接入历史存储和权限。核心协同不会自动开启历史记录,后端还必须提供历史列表、版本内容和恢复操作。
将下面的配置添加到现有的产品、许可证和协同配置中。如果预设或现有配置已经注册了这些插件,请修改原有配置,不要重复注册。
插件模式
安装
pnpm add @univerjs-pro/edit-history @univerjs-pro/edit-history-ui @univerjs-pro/sheets-history @univerjs-pro/sheets-history-uinpm install @univerjs-pro/edit-history @univerjs-pro/edit-history-ui @univerjs-pro/sheets-history @univerjs-pro/sheets-history-uiyarn add @univerjs-pro/edit-history @univerjs-pro/edit-history-ui @univerjs-pro/sheets-history @univerjs-pro/sheets-history-uibun add @univerjs-pro/edit-history @univerjs-pro/edit-history-ui @univerjs-pro/sheets-history @univerjs-pro/sheets-history-ui使用
import { UniverEditHistoryPlugin } from '@univerjs-pro/edit-history'import EditHistoryLocale from '@univerjs-pro/edit-history-ui/locale/zh-CN'import { UniverSheetsHistoryPlugin } from '@univerjs-pro/sheets-history'import { UniverSheetsHistoryUIPlugin } from '@univerjs-pro/sheets-history-ui'import HistoryLocale from '@univerjs-pro/sheets-history-ui/locale/zh-CN'import { LocaleType, mergeLocales } from '@univerjs/core'import '@univerjs-pro/edit-history-ui/lib/index.css'import '@univerjs-pro/sheets-history-ui/lib/index.css'const historyServerUrl = '/api/history'univerAPI.loadLocales(LocaleType.ZH_CN, mergeLocales(EditHistoryLocale, HistoryLocale))univer.registerPlugin(UniverEditHistoryPlugin, { historyServerUrl })univer.registerPlugin(UniverSheetsHistoryPlugin)univer.registerPlugin(UniverSheetsHistoryUIPlugin, { univerContainerId: 'app', historyServerUrl,})插件配置
UniverEditHistoryPlugin
historyServerUrlstring默认值:'/universer-api/history'历史记录服务的基础地址。
UniverSheetsHistoryUIPlugin
historyServerUrlstring默认值:'/universer-api/history'历史记录服务的基础地址。univerContainerIdstring编辑器容器的 DOM ID。
/api/history 仅为示例地址,应替换为应用实际使用的历史接口,并确保该接口能够处理客户端发送的请求。两个 historyServerUrl 必须保持一致,univerContainerId 应指向编辑器容器的 DOM ID,语言包应与编辑器当前语言一致。
Sheets 历史面板还依赖协同 UI 插件 UniverCollaborationClientUIPlugin,需要在已有的协同配置中注册。
移动端适配
先配置移动端编辑器。在本页插件模式的初始化代码中,使用同一个包导出的移动端插件替换下列桌面端注册,替换必须在打开文档前完成。保留模型插件、样式、语言包和 Facade 导入,不要同时注册两个版本。
| 来源包 | 桌面端注册 | 移动端注册 |
|---|---|---|
@univerjs-pro/sheets-history-ui | UniverSheetsHistoryUIPlugin | UniverSheetsHistoryMobileUIPlugin |
预设模式
安装
pnpm add @univerjs/preset-sheets-collaborationnpm install @univerjs/preset-sheets-collaborationyarn add @univerjs/preset-sheets-collaborationbun add @univerjs/preset-sheets-collaboration使用
UniverSheetsCollaborationPreset 已包含历史界面及其依赖的插件。保留现有的编辑器和许可证配置;如果该预设已经注册,请修改原有配置,不要再次注册预设或历史插件。
import { UniverSheetsCollaborationPreset } from '@univerjs/preset-sheets-collaboration'import CollaborationLocale from '@univerjs/preset-sheets-collaboration/locales/zh-CN'import '@univerjs/preset-sheets-collaboration/lib/index.css'const collaborationPreset = UniverSheetsCollaborationPreset({ universerEndpoint: 'https://your-server.example', univerContainerId: 'app',})预设配置
UniverSheetsCollaborationPreset
universerEndpointstring默认值:window.location.origin协同服务的基础地址。univerContainerIdstring默认值:'app'编辑器容器的 DOM ID。
将 collaborationPreset 加入现有 createUniver() 调用的 presets 数组,并将 CollaborationLocale 合并到对应语言的资源中。universerEndpoint 填写服务端的基础 URL,univerContainerId 填写编辑器容器 ID。预设按 {universerEndpoint}/universer-api/history 拼接历史地址;如需自定义 historyServerUrl 或 viewerPlugins,应使用插件模式。上文所述的后端服务仍然必需。
查看和恢复版本
从编辑器菜单打开历史记录,选择一个版本即可查看其内容和改动。恢复操作会改写当前共享文档,因此仅应允许具备恢复权限的用户执行,并且该权限必须由服务端校验。
接入后,修改一个单元格内容、公式或样式,等待协同确认,然后查看历史列表和版本内容。重新打开应用并重启服务,确认数据仍然存在。再恢复一个旧版本,确认另一个浏览器会话也能收到恢复后的状态。
面板为空时,先确认该文档是否已被记录到历史中,再检查文档 ID 和服务地址。如果版本可以查看但恢复失败,请检查恢复权限和协同连接。旧版本中可能包含后来被删除的数据,因此读取历史同样需要权限校验。
比较快照
如需构建审阅界面,或将改动交给 AI Agent,可以调用 compareUnitData(),也可以使用 prepareUnitComparison() 计算一次并反复查询。先注册本品类的 History 插件并导入 Facade 入口,再将同一文档的两份完整快照分别作为 beforeSnapshot 和 afterSnapshot 传入。该比较为只读操作,不需要历史服务,也不会恢复或合并内容。
import { UnitComparisonDetailLevel, UnitComparisonFidelity } from '@univerjs-pro/edit-history'import { UniverInstanceType } from '@univerjs/core'import '@univerjs-pro/edit-history/facade'const comparison = univerAPI.prepareUnitComparison({ comparisonId: 'review-1', unitId: beforeSnapshot.id, type: UniverInstanceType.UNIVER_SHEET, fidelity: UnitComparisonFidelity.SNAPSHOT, leftData: beforeSnapshot, rightData: afterSnapshot,})const overview = comparison.query({ detail: UnitComparisonDetailLevel.SUMMARY })const scope = overview.scopes[0]const page = comparison.query({ scope, offset: 0, limit: 100 })console.log(page.items, page.page.hasMore, page.diagnostics)通过 scopes 可以查看可用的视图,再将其中一个传给 query() 筛选结果,无需重新执行比较。每页默认返回 100 项,最多 1,000 项,可配合 page.hasMore 和 offset 获取剩余内容。summary 始终描述整个比较,不随条目筛选而变化。在将结果视为完整比较之前,请先检查 diagnostics.readiness 和 diagnostics.codes。不使用编辑器、在 Node.js 或 CLI 中使用的方式见无界面比较 API。
你觉得这篇文档如何?