Images
@univerjs/preset-sheets-drawing
Images can be used to enrich spreadsheet content, enhance visual expression, and help illustrate or highlight key information.
Currently, the supported image types are: floating images and cell images.
Preset Mode
Installation
npm install @univerjs/preset-sheets-drawing
Usage
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing'
import UniverPresetSheetsDrawingEnUS from '@univerjs/preset-sheets-drawing/locales/en-US'
import { createUniver, LocaleType, merge } from '@univerjs/presets'
import '@univerjs/preset-sheets-core/lib/index.css'
import '@univerjs/preset-sheets-drawing/lib/index.css'
const { univerAPI } = createUniver({
locale: LocaleType.En_US,
locales: {
[LocaleType.En_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsDrawingEnUS,
),
},
presets: [
UniverSheetsCorePreset(),
UniverSheetsDrawingPreset(),
],
})
If you are using the collaboration feature, make sure to pass the collaboration: true
option in UniverSheetsDrawingPreset
:
UniverSheetsDrawingPreset({
collaboration: true,
})
Plugin Mode
Installation
npm install @univerjs/docs-drawing @univerjs/drawing @univerjs/drawing-ui @univerjs/sheets-drawing @univerjs/sheets-drawing-ui
Usage
import { LocaleType, merge, Univer } from '@univerjs/core'
import { UniverDocsDrawingPlugin } from '@univerjs/docs-drawing'
import { IImageIoService, UniverDrawingPlugin } from '@univerjs/drawing'
import { UniverDrawingUIPlugin } from '@univerjs/drawing-ui'
import DrawingUIEnUS from '@univerjs/drawing-ui/locale/en-US'
import { UniverSheetsDrawingPlugin } from '@univerjs/sheets-drawing'
import { UniverSheetsDrawingUIPlugin } from '@univerjs/sheets-drawing-ui'
import SheetsDrawingUIEnUS from '@univerjs/sheets-drawing-ui/locale/en-US'
import '@univerjs/drawing-ui/lib/index.css'
import '@univerjs/sheets-drawing-ui/lib/index.css'
import '@univerjs/sheets-drawing-ui/facade'
const univer = new Univer({
locale: LocaleType.En_US,
locales: {
[LocaleType.En_US]: merge(
{},
DrawingUIEnUS,
SheetsDrawingUIEnUS,
),
},
})
univer.registerPlugin(UniverSheetsFilterPlugin)
univer.registerPlugin(UniverSheetsFilterUIPlugin)
univer.registerPlugin(UniverDrawingPlugin)
univer.registerPlugin(UniverDrawingUIPlugin)
univer.registerPlugin(UniverSheetsDrawingPlugin)
univer.registerPlugin(UniverSheetsDrawingUIPlugin)
If you are using the collaboration feature, you need to add the override
configuration:
univer.registerPlugin(UniverDrawingPlugin, {
override: [[IImageIoService, null]],
})
Facade API
Complete Facade API type definitions can be found in the FacadeAPI.。
Add Floating DOM
FWorksheet.addFloatDomToPosition(layer)
method can add a floating DOM element to a specified position.
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
// You should register components at an appropriate time (e.g., when Univer is loaded)
// This is a React component. For Vue3 components, the third parameter should be `{ framework: 'vue3' }`
univerAPI.registerComponent(
'myFloatDom',
({ data }) => (
<div style={{ width: '100%', height: '100%', background: '#fff', border: '1px solid #ccc', boxSizing: 'border-box' }}>
popup content:
{' '}
{data?.label}
</div>
),
)
// Add a floating DOM
// If disposable is null, floating DOM addition failed
const disposable = fWorksheet.addFloatDomToPosition({
componentKey: 'myFloatDom',
initPosition: {
startX: 100,
endX: 300,
startY: 100,
endY: 200,
},
// Component data
data: {
label: 'hahah',
},
})
// 2 秒后移除浮动 DOM
setTimeout(() => {
disposable?.dispose()
}, 2000)
FWorksheet.addFloatDomToRange(range, layer, domLayout)
method can add a floating DOM element to a specified range.
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
// Register a range loading component
function RangeLoading() {
const divStyle = {
width: '100%',
height: '100%',
backgroundColor: '#fff',
border: '1px solid #ccc',
boxSizing: 'border-box' as const,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center' as const,
transformOrigin: 'top left',
}
return (
<div style={divStyle}>
Loading...
</div>
)
}
univerAPI.registerComponent('RangeLoading', RangeLoading)
// Add the range loading component covering the range A1:C3
const fRange = fWorksheet.getRange('A1:C3')
const disposable = fWorksheet.addFloatDomToRange(fRange, { componentKey: 'RangeLoading' }, {}, 'myRangeLoading')
// Remove the floating DOM after 2 seconds
setTimeout(() => {
disposable?.dispose()
}, 2000)
// another example-------------------
// Register a float button component
function FloatButton() {
const divStyle = {
width: '100px',
height: '30px',
backgroundColor: '#fff',
border: '1px solid #ccc',
boxSizing: 'border-box' as const,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center' as const,
cursor: 'pointer',
}
const clickHandler = () => {
console.warn('click')
}
return (
<div style={divStyle} onClick={clickHandler}>
FloatButton
</div>
)
}
univerAPI.registerComponent('FloatButton', FloatButton)
// Add the float button to the range A5:C7, position is start from A5 cell, and width is 100px, height is 30px, margin is 100% of range width and height
const fRange2 = fWorksheet.getRange('A5:C7')
const disposable2 = fWorksheet.addFloatDomToRange(
fRange2,
{
componentKey: 'FloatButton',
},
{
width: 100,
height: 30,
marginX: '100%', // margin percent to range width, or pixel
marginY: '100%',
},
'myFloatButton',
)
FWorksheet.addFloatDomToColumnHeader(column, layer, domPos)
method can add a floating DOM element to a specified column header.
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
// Register a float button component
function FloatButton() {
const divStyle = {
width: '100px',
height: '30px',
backgroundColor: '#fff',
border: '1px solid #ccc',
boxSizing: 'border-box' as const,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center' as const,
cursor: 'pointer',
}
const clickHandler = () => {
console.warn('click')
}
return (
<div style={divStyle} onClick={clickHandler}>
FloatButton
</div>
)
}
univerAPI.registerComponent('FloatButton', FloatButton)
// Add the float button to the column D header, position is right align, width is 100px, height is 30px, margin is 0
const disposable = fWorksheet.addFloatDomToColumnHeader(
3,
{
componentKey: 'FloatButton',
allowTransform: false,
},
{
width: 100,
height: 30,
marginX: 0,
marginY: 0,
horizonOffsetAlign: 'right',
},
'myFloatButton',
)
// Remove the float button after 2 seconds
setTimeout(() => {
disposable?.dispose()
}, 2000)
Insert Floating Images
FWorksheet.newOverGridImage()
creates a floating image builder, returns an instance of FOverGridImageBuilder
, and generates an ISheetImage
object for inserting floating images through chain calls.
Here are some member methods on FOverGridImageBuilder
:
Method | Description |
---|---|
buildAsync | Build the image and return ISheetImage object |
setSource | Set the source of the image |
setColumn | Set the horizontal position of the image |
setRow | Set the vertical position of the image |
setColumnOffset | Set the horizontal offset of the image |
setRowOffset | Set the vertical offset of the image |
setWidth | Set the width of the image |
setHeight | Set the height of the image |
setAnchorType | Set the anchor type of the image, whether the position and size change with the cell |
setCropTop | Set the cropping region of the image by defining the top edges, thereby displaying the specific part of the image you want |
setCropLeft | Set the cropping region of the image by defining the left edges, thereby displaying the specific part of the image you want |
setCropBottom | Set the cropping region of the image by defining the bottom edges, thereby displaying the specific part of the image you want |
setCropRight | Set the cropping region of the image by defining the right edges, thereby displaying the specific part of the image you want |
setRotate | Set the rotation angle of the image |
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, width is 500px, height is 300px
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
const image = await fWorksheet.newOverGridImage()
.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', univerAPI.Enum.ImageSourceType.URL)
.setColumn(5)
.setRow(5)
.setWidth(500)
.setHeight(300)
.buildAsync()
fWorksheet.insertImages([image])
Also can insert image by FWorksheet.insertImage(url, column, row, offsetX, offsetY)
method.
// Insert an image to the sheet, position is F6, offset is 10px
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
const result = await fWorksheet.insertImage('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', 5, 5, 10, 10)
Get Floating Images
FWorksheet.getImages()
method can get all floating images in the worksheet, and return an array of FOverGridImage[]
instances.
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
const images = fWorksheet.getImages()
images.forEach((image) => {
image.getId()
})
You can also get a floating image by its ID using the FWorksheet.getImageById(id)
method.
Update Floating Images
FWorksheet.updateImages(sheetImages)
method can update the position and size of floating images. etc.
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, width is 500px, height is 300px
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
const image = await fWorksheet.newOverGridImage()
.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', univerAPI.Enum.ImageSourceType.URL)
.setColumn(5)
.setRow(5)
.setWidth(500)
.setHeight(300)
.buildAsync()
fWorksheet.insertImages([image])
// update the image width to 100px and height to 50px after 4 seconds
setTimeout(async () => {
const imageBuilder = fWorksheet.getImageById(image.drawingId).toBuilder()
const newImage = await imageBuilder.setWidth(100).setHeight(50).buildAsync()
fWorksheet.updateImages([newImage])
}, 4000)
Delete Floating Images
FWorksheet.deleteImages(sheetImages)
method can delete floating images.
const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet()
const image = fWorksheet.getImages()[0]
// Delete the first image of the sheet
fWorksheet.deleteImages([image])
Insert Cell Images
FRange.insertCellImageAsync(file)
method can insert cell images.
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
// Insert an image into the cell A10
const fRange = fWorksheet.getRange('A10')
const result = await fRange.insertCellImageAsync('https://avatars.githubusercontent.com/u/61444807?s=48&v=4')