欄位
欄位定義 Base 表的結構。每個欄位都有型別、配置和預設值,並具備排序、篩選、分組、卡片展示等能力。
Bases 介面設定
UniverBasesUIPlugin
attachmentobject上傳附件並回傳附件資訊。acceptstring[]限制附件選擇器可選的檔案範圍。maxSizenumber附件大小上限,單位為位元組。upload(file: File) => Promise<IBaseAttachment>上傳附件並回傳附件資訊。
personOptionsobject[]人員選擇器提供的候選使用者。idstring標識該選項的穩定 ID。namestring選擇器中顯示的名稱。avatarstring選項頭像的 URL。
groupOptionsobject[]群組選擇器提供的候選群組。idstring標識該選項的穩定 ID。namestring選擇器中顯示的名稱。avatarstring選項頭像的 URL。
上傳附件
在註冊 Bases UI 外掛的位置配置上傳處理:
TypeScript
univer.registerPlugin(UniverBasesUIPlugin, { attachment: { accept: ['image/*', '.pdf'], maxSize: 10 * 1024 * 1024, upload: uploadAttachment, },})uploadAttachment 是業務應用提供的上傳函式,簽名為 (file: File) => Promise<IBaseAttachment>,返回的附件結構見下文範例。maxSize 的單位為位元組。未提供上傳函式時,外掛會先嚐試圖片 IO,再回退到 Base64,這會使快照變大。生產環境應接入真實檔案儲存,並在後端校驗檔案型別、大小和下載權限。
人員與群組
候選項透過 UniverBasesUIPlugin 的 personOptions 和 groupOptions 傳入,每項包含 id、name 和可選的 avatar。這些配置只控制選擇器的顯示內容,登入和訪問權限仍由業務應用負責。
欄位類型
| 用途 | BaseFieldType |
|---|---|
| 文字與聯絡方式 | Text, Link, Phone, Email |
| 數值與進度 | Number, Currency, Progress, Rating |
| 選項與成員 | Checkbox, SingleSelect, MultiSelect, Person, Group |
| 日期與附件 | Date, Attachment |
| 計算與關聯 | Formula, RecordLink |
| 自動產生與稽核 | Numbering, RecordId, CreatedBy, UpdatedBy, CreatedAt, UpdatedAt |
單選和多選使用選項 ID,人員和分組使用業務應用中的成員 ID。自動編號和審計欄位由系統維護,不能作為普通文字欄位寫入。公式的用法見公式欄位,跨表關聯和父子記錄的用法見記錄、關聯與層級。
新增欄位
TypeScript
const table = base.getTableById('tasks')if (!table) throw new Error('Tasks table not found')const title = table.addField('Title', univerAPI.Enum.BaseFieldType.Text)const priority = table.addField( 'Priority', univerAPI.Enum.BaseFieldType.SingleSelect, { field: { config: { options: [ { id: 'high', name: 'High', color: '#ef4444' }, { id: 'low', name: 'Low', color: '#22c55e' }, ], }, }, },)更新欄位
TypeScript
priority.setName('Impact')priority.update({ description: 'Business priority' })priority.setConfig({ options: [ { id: 'p0', name: 'P0', color: '#dc2626' }, { id: 'p1', name: 'P1', color: '#f97316' }, ],})變更欄位類型
TypeScript
priority.changeType(univerAPI.Enum.BaseFieldType.Text, {})更改欄位型別可能會規範化現有值。在將結構變更應用於生產表之前,應先驗證應用中的資料。
附件欄位
附件需要穩定的 ID、名稱和可訪問的來源。以下範例假設業務系統已上傳該檔案:
TypeScript
const attachments = table.addField('Files', univerAPI.Enum.BaseFieldType.Attachment)const record = table.getRecords()[0]if (!record) throw new Error('Create a record first')record.setAttachments(attachments.getId(), [{ id: 'brief-1', name: 'brief.pdf', mimeType: 'application/pdf', source: 'https://example.com/files/brief.pdf',}])setAttachments() 會替換整個單元格的附件列表;getAttachments() 用於讀取列表,deleteAttachments() 用於刪除指定項。範例中的 URL 應替換為使用者實際可訪問的檔案地址。
你覺得這篇文檔如何?