評論

支援 Headless

使用者可以為文件新增評論和回覆,使團隊成員的討論集中在一處。

外掛模式

安裝

Shell
pnpm add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-ui

使用

TypeScript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment'import { UniverSheetsThreadCommentUIPlugin } from '@univerjs/sheets-thread-comment-ui'import SheetsThreadCommentZhTW from '@univerjs/sheets-thread-comment/locale/zh-TW'import SheetsThreadCommentUIZhTW from '@univerjs/sheets-thread-comment-ui/locale/zh-TW'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui'import ThreadCommentUIZhTW from '@univerjs/thread-comment-ui/locale/zh-TW'import '@univerjs/sheets-thread-comment-ui/lib/index.css'import '@univerjs/thread-comment-ui/lib/index.css'import '@univerjs/sheets-thread-comment/facade'const univer = new Univer({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      ThreadCommentUIZhTW,       SheetsThreadCommentZhTW,       SheetsThreadCommentUIZhTW,     ),  },})univer.registerPlugin(UniverThreadCommentPlugin)univer.registerPlugin(UniverThreadCommentUIPlugin)univer.registerPlugin(UniverSheetsThreadCommentPlugin)univer.registerPlugin(UniverSheetsThreadCommentUIPlugin)

配合協同編輯使用

如果同時使用協同編輯,還需要註冊資料來源外掛:

Shell
pnpm add @univerjs-pro/thread-comment-datasource
TypeScript
import { UniverThreadCommentDataSourcePlugin } from '@univerjs-pro/thread-comment-datasource'univer.registerPlugin(UniverThreadCommentDataSourcePlugin)

行動裝置支援

請先完成行動版編輯器的配置。在本頁外掛模式的初始化程式碼中,將下面列出的桌面版註冊替換為同一套件匯出的行動版外掛,且必須在開啟文件前完成替換。模型外掛、樣式、語言套件和 Facade 匯入均需保留,不要同時註冊兩個版本。

套件桌面版註冊行動版註冊
@univerjs/sheets-thread-comment-uiUniverSheetsThreadCommentUIPluginUniverSheetsThreadCommentMobileUIPlugin
@univerjs/thread-comment-uiUniverThreadCommentUIPluginUniverThreadCommentMobileUIPlugin

預設模式

安裝

Shell
pnpm add @univerjs/preset-sheets-thread-comment

使用

TypeScript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsThreadCommentPreset } from '@univerjs/preset-sheets-thread-comment'import UniverPresetSheetsThreadCommentZhTW from '@univerjs/preset-sheets-thread-comment/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-thread-comment/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsThreadCommentZhTW,     ),  },  presets: [    UniverSheetsCorePreset(),    UniverSheetsThreadCommentPreset(),   ],})

Facade API

完整的 Facade API 型別定義參見 FacadeAPI。

引入

外掛模式提示

只有外掛模式需要手動引入 Facade 包,預設模式已內建,無需額外匯入。

TypeScript
import '@univerjs/sheets-thread-comment/facade'

建立單元格評論

univerAPI.newTheadComment() 返回一個 FTheadCommentBuilder,用於建置新評論。在該建置器上鍊式呼叫方法,即可設定評論的各項屬性。

以下為 FTheadCommentBuilder 的部分成員方法:

方法描述
setContent設定評論內容
setPersonId設定評論的人員 id
setDateTime設定評論的時間
setId設定評論 ID
setThreadId設定評論的主題 id
TypeScript
// 建立一個新的評論const richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment()  .setContent(richText)console.log(commentBuilder.content.toPlainText())// 新增評論到 A1 單元格const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const cell = fWorksheet.getRange('A1')const result = await cell.addCommentAsync(commentBuilder)

獲取單元格評論

這些方法都會返回 FThreadComment,隨後可以更新、刪除或解決該評論。

  • FWorkbook.getComments(): 獲取工作簿的所有評論
  • FfWorksheet.getComments(): 獲取工作表的所有評論
  • FRange.getComment(): 獲取範圍左上角單元格的評論
  • FRange.getComments(): 取得範圍內的所有評論

以下為 FThreadComment 的部分成員方法:

方法描述
getIsRoot是否為根評論
getCommentData獲取評論資料
getReplies獲取評論的回覆列表
getRange獲取評論的範圍
getRichText獲取評論的富文字內容
deleteAsync刪除評論及其回覆
updateAsync更新評論內容
resolveAsync解決評論
replyAsync回覆評論
TypeScript
// 獲取工作簿的所有評論const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.getComments()// 獲取活動工作表的所有評論const fWorksheet = fWorkbook.getActiveSheet()fWorksheet.getComments()const fRange = fWorksheet.getRange('A1:B2')// 獲取 A1 單元格的評論fRange.getComment()// 獲取 A1:B2 範圍的所有評論fRange.getComments()

清除單元格評論

  • FWorkbook.clearComments(): 清除工作簿的所有評論
  • FWorksheet.clearComments(): 清除工作表的所有評論
  • FRange.clearCommentAsync(): 清除範圍左上角單元格的評論
  • FRange.clearCommentsAsync(): 清除範圍內的所有評論
TypeScript
// 清除工作簿的所有評論const fWorkbook = univerAPI.getActiveWorkbook()await fWorkbook.clearComments()// 清除活動工作表的所有評論const fWorksheet = fWorkbook.getActiveSheet()await fWorksheet.clearComments()const fRange = fWorksheet.getRange('A1:B2')// 清除 A1 單元格的評論await fRange.clearCommentAsync()// 清除 A1:B2 範圍的所有評論await fRange.clearCommentsAsync()

獲取評論回覆列表

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 獲取工作表的所有評論const comments = fWorksheet.getComments()comments.forEach((comment) => {  // 如果該評論是根評論,則獲取其回覆列表  if (comment.getIsRoot()) {    const replies = comment.getReplies()    replies.forEach((reply) => {      console.log(reply.getCommentData())    })  }})

更新/刪除/解決/回覆評論

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 建立一個新的評論並新增到 A1 單元格const richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment()  .setContent(richText)  .setId('mock-comment-id')const cell = fWorksheet.getRange('A1')await cell.addCommentAsync(commentBuilder)// 3 秒後更新評論並回復setTimeout(async () => {  // 更新評論  const comment = fWorksheet.getCommentById('mock-comment-id')  const newRichText = univerAPI.newRichText().insertText('Hello Univer AI')  await comment.updateAsync(newRichText)  // 回覆評論  const replyText = univerAPI.newRichText().insertText('Hello Univer AI! GO! GO! GO!')  const reply = univerAPI.newTheadComment().setContent(replyText)  await comment.replyAsync(reply)}, 3000)// 6 秒後解決評論並刪除setTimeout(async () => {  const comment = fWorksheet.getCommentById('mock-comment-id')  await comment.resolveAsync()  await comment.deleteAsync()}, 6000)

事件監聽

完整的事件型別定義參見 Events。

評論模組會發出評論新增、更新、刪除以及解決狀態變更的事件,可透過 univerAPI.addEvent() 訂閱。

評論新增事件

univerAPI.Event.CommentAdded 在評論新增後觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.CommentAdded, (params) => {  const { comment, workbook, worksheet, row, col } = params})// 移除事件監聽器,使用 `disposable.dispose()`

univerAPI.Event.BeforeCommentAdd 在評論新增前觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentAdd, (params) => {  const { comment, workbook, worksheet, row, col } = params  // 取消評論新增操作  params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`

評論更新事件

univerAPI.Event.CommentUpdated 在評論更新後觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.CommentUpdated, (params) => {  const { comment, workbook, worksheet, row, col } = params})// 移除事件監聽器,使用 `disposable.dispose()`

univerAPI.Event.BeforeCommentUpdate 在評論更新前觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentUpdate, (params) => {  const { comment, workbook, worksheet, row, col, newContent } = params  // 取消評論更新操作  params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`

評論刪除事件

univerAPI.Event.CommentDeleted 在評論刪除後觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.CommentDeleted, (params) => {  const { commentId, workbook, worksheet } = params})// 移除事件監聽器,使用 `disposable.dispose()`

univerAPI.Event.BeforeCommentDelete 在評論刪除前觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentDelete, (params) => {  const { comment, workbook, worksheet, row, col } = params  // 取消評論刪除操作  params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`

評論解決狀態事件

univerAPI.Event.CommentResolved 在評論解決狀態變更後觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.CommentResolved, (params) => {  const { comment, row, col, resolved, workbook, worksheet } = params})// 移除事件監聽器,使用 `disposable.dispose()`

univerAPI.Event.BeforeCommentResolve 在評論解決狀態變更前觸發。

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentResolve, (params) => {  const { comment, row, col, resolved, workbook, worksheet } = params  // 取消評論解決操作  params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`

每個事件都帶有以下通用參數:

  • workbook: 當前工作簿例項
  • worksheet: 當前工作表例項
  • row: 評論所在的行索引
  • col: 評論所在的列索引
  • comment: 評論物件(刪除事件中僅包含 commentId)

有些事件還帶有自身特有的參數:

  • BeforeCommentUpdate 還帶有 newContent,即新的評論內容(RichTextValue 型別)
  • CommentResolved 和 BeforeCommentResolve 還帶有 resolved,即評論的解決狀態

所有以 Before 為字首的回撥都可以設定 params.cancel = true 來阻止對應操作。

通用評論操作

呼叫這些方法之前,需要先匯入通用評論 Facade 和品類 Facade。品類包中的方法負責建立穩定的錨點,回覆、修改、解決和刪除由通用 API 處理。getComments() 只讀取本地狀態;listCommentsAsync() 會透過已配置的資料來源同步它已經知道的執行緒,但無法發現伺服器端上的全部執行緒。

TypeScript
import '@univerjs/thread-comment/facade'const [thread] = univerAPI.getComments({ resolved: false })if (thread) {  await univerAPI.replyCommentAsync({    unitId: thread.unitId,    subUnitId: thread.subUnitId,    threadId: thread.threadId,    content: 'Reviewed.',  })}

共享討論

共享討論需要透過 Server SDK 整合 Comment Service 和 Endpoint,然後在協同和許可證配置完成後,註冊 @univerjs-pro/thread-comment-datasource 中的 UniverThreadCommentDataSourcePlugin。內建資料來源會訪問 /universer-api/comment/unit/{unitId}/... 和 /universer-api/user/list,這兩個路由需要由應用提供。

圖片與圖表評論

先配置宿主側的圖片或圖表外掛,然後在開啟 Unit 之前,將下面對應的整合外掛及其 Facade 入口一併註冊。外掛模式和預設模式都需要顯式註冊這些整合,它們不包含額外的樣式和語言套件。

Shell
pnpm add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-comment
TypeScript
import { UniverSheetsDrawingThreadCommentPlugin } from '@univerjs-pro/sheets-drawing-thread-comment'import { UniverSheetsChartThreadCommentPlugin } from '@univerjs-pro/sheets-chart-thread-comment'import '@univerjs-pro/sheets-drawing-thread-comment/facade'import '@univerjs-pro/sheets-chart-thread-comment/facade'univer.registerPlugin(UniverSheetsDrawingThreadCommentPlugin)univer.registerPlugin(UniverSheetsChartThreadCommentPlugin)

Unit 開啟之後,即可為已有的圖片或圖表新增評論。getComments() 返回本地已載入的執行緒;listCommentsAsync() 會先同步已知執行緒,然後再返回。

TypeScript
const host = univerAPI.getActiveWorkbook()?.getActiveSheet()const image = host?.getImages()[0]const chart = host?.getCharts()[0]await image?.createCommentAsync('Review this image.')await chart?.createCommentAsync('Verify the chart source.')

@univerjs-pro/sheets-drawing-thread-comment · @univerjs-pro/sheets-chart-thread-comment

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.