Drawing & Image
| Packages | @univerjs/sheets-drawing |
|---|
This class should not be instantiated directly. Use factory methods on
univerAPIinstead.
Overview
@univerjs/sheets-drawing
| Method | Description |
|---|---|
buildAsync | - |
getId | Get the id of the image |
getSource | Get the source of the image |
getSourceType | Get the source type of the image |
getType | Get the drawing type of the image |
remove | Remove the image from the sheet |
setAnchorType | Set the anchor type of the image, whether the position and size change with the cell |
setBack | Move the image layer to the bottom layer |
setBackward | Move the image layer backward by one level |
setColumn | Set the horizontal position of the image |
setColumnOffset | Set the horizontal offset of the image |
setCrop | Set the cropping region of the image by defining the top, bottom, left, and right 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 |
setCropLeft | Set the cropping region of the image by defining the left 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 |
setCropTop | Set the cropping region of the image by defining the top edges, thereby displaying the specific part of the image you want |
setForward | Move the image layer forward by one level |
setFront | Move the image layer to the top layer |
setHeight | Set the height of the image |
setImage | Set the initial image configuration for the image builder |
setPositionAsync | - |
setRotate | Set the rotation angle of the image |
setRow | Set the vertical position of the image |
setRowOffset | Set the vertical offset of the image |
setSizeAsync | Set the size of the image |
setSource | - |
setSubUnitId | - |
setUnitId | - |
setWidth | Set the width of the image |
toBuilder | Convert the image to a FOverGridImageBuilder |
APIs
Lifecycle & Creation
buildAsync
Signature
async buildAsync(): Promise<ISheetImage>Returns
Promise<ISheetImage>— See signature above.
@univerjs/sheets-drawing
Getters & Queries
getId
Get the id of the image
Signature
getId(): stringReturns
string— The id of the image
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const images = fWorksheet.getImages();
images.forEach((image) => {
console.log(image, image.getId());
});@univerjs/sheets-drawing
getSource
Get the source of the image
Signature
getSource(): stringReturns
string— The source of the image
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const images = fWorksheet.getImages();
images.forEach((image) => {
console.log(image, image.toBuilder().getSource());
});@univerjs/sheets-drawing
getSourceType
Get the source type of the image
Signature
getSourceType(): ImageSourceTypeReturns
ImageSourceType— The source type of the image
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const images = fWorksheet.getImages();
images.forEach((image) => {
console.log(image, image.toBuilder().getSourceType());
});@univerjs/sheets-drawing
getType
Get the drawing type of the image
Signature
getType(): DrawingTypeEnumReturns
DrawingTypeEnum— The drawing type of the image
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const images = fWorksheet.getImages();
images.forEach((image) => {
console.log(image, image.getType());
});@univerjs/sheets-drawing
Setters & Modifiers
setAnchorType
Set the anchor type of the image, whether the position and size change with the cell
Signature
setAnchorType(anchorType: SheetDrawingAnchorType): FOverGridImageBuilderParameters
anchorTypeSheetDrawingAnchorType— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// image1 position is start from A6 cell, anchor type is Position.
// Only the position of the drawing follows the cell changes. When rows or columns are inserted or deleted, the position of the drawing changes, but the size remains the same.
const image1 = await fWorksheet.newOverGridImage()
.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', univerAPI.Enum.ImageSourceType.URL)
.setColumn(0)
.setRow(5)
.setAnchorType(univerAPI.Enum.SheetDrawingAnchorType.Position)
.buildAsync();
// image2 position is start from C6 cell, anchor type is Both.
// The size and position of the drawing follow the cell changes. When rows or columns are inserted or deleted, the size and position of the drawing change accordingly.
const image2 = await fWorksheet.newOverGridImage()
.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', univerAPI.Enum.ImageSourceType.URL)
.setColumn(2)
.setRow(5)
.setAnchorType(univerAPI.Enum.SheetDrawingAnchorType.Both)
.buildAsync();
// image3 position is start from E6 cell, anchor type is None.
// The size and position of the drawing do not follow the cell changes. When rows or columns are inserted or deleted, the position and size of the drawing remain unchanged.
const image3 = await fWorksheet.newOverGridImage()
.setSource('https://avatars.githubusercontent.com/u/61444807?s=48&v=4', univerAPI.Enum.ImageSourceType.URL)
.setColumn(4)
.setRow(5)
.setAnchorType(univerAPI.Enum.SheetDrawingAnchorType.None)
.buildAsync();
// insert images into the sheet
fWorksheet.insertImages([image1, image2, image3]);
// after 2 seconds, set the row height of the 5th row to 100px and insert a row before the 5th row.
// then observe the position and size changes of the images.
setTimeout(() => {
fWorksheet.setRowHeight(5, 100).insertRowBefore(5);
}, 2000);@univerjs/sheets-drawing
setBack
Move the image layer to the bottom layer
Signature
setBack(): booleanReturns
boolean— true if the image is moved to the bottom layer successfully, otherwise false
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setBack();
console.log(result);@univerjs/sheets-drawing
setBackward
Move the image layer backward by one level
Signature
setBackward(): booleanReturns
boolean— true if the image is moved backward successfully, otherwise false
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setBackward();
console.log(result);@univerjs/sheets-drawing
setColumn
Set the horizontal position of the image
Signature
setColumn(column: number): FOverGridImageBuilderParameters
columnnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell.
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)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setColumnOffset
Set the horizontal offset of the image
Signature
setColumnOffset(offset: number): FOverGridImageBuilderParameters
offsetnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell and horizontal offset is 10px.
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)
.setColumnOffset(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setCrop
Set the cropping region of the image by defining the top, bottom, left, and right edges, thereby displaying the specific part of the image you want.
Signature
setCrop(top?: number, left?: number, bottom?: number, right?: number): booleanParameters
topnumber(optional) — No descriptionleftnumber(optional) — No descriptionbottomnumber(optional) — No descriptionrightnumber(optional) — No description
Returns
boolean— true if the crop is set successfully, otherwise false
Examples
// set the crop of the image, top 10px, left 10px, bottom 10px, right 10px.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setCrop(10, 10, 10, 10);
console.log(result);@univerjs/sheets-drawing
setCropBottom
Set the cropping region of the image by defining the bottom edges, thereby displaying the specific part of the image you want.
Signature
setCropBottom(bottom: number): FOverGridImageBuilderParameters
bottomnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, bottom crop is 10px.
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)
.setCropBottom(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setCropLeft
Set the cropping region of the image by defining the left edges, thereby displaying the specific part of the image you want.
Signature
setCropLeft(left: number): FOverGridImageBuilderParameters
leftnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, left crop is 10px.
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)
.setCropLeft(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setCropRight
Set the cropping region of the image by defining the right edges, thereby displaying the specific part of the image you want.
Signature
setCropRight(right: number): FOverGridImageBuilderParameters
rightnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, right crop is 10px.
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)
.setCropRight(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setCropTop
Set the cropping region of the image by defining the top edges, thereby displaying the specific part of the image you want.
Signature
setCropTop(top: number): FOverGridImageBuilderParameters
topnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell, top crop is 10px.
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)
.setCropTop(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setForward
Move the image layer forward by one level
Signature
setForward(): booleanReturns
boolean— true if the image is moved forward successfully, otherwise false
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setForward();
console.log(result);@univerjs/sheets-drawing
setFront
Move the image layer to the top layer
Signature
setFront(): booleanReturns
boolean— true if the image is moved to the top layer successfully, otherwise false
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setFront();
console.log(result);@univerjs/sheets-drawing
setHeight
Set the height of the image
Signature
setHeight(height: number): FOverGridImageBuilderParameters
heightnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// 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 120px and height is 50px.
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(120)
.setHeight(50)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setImage
Set the initial image configuration for the image builder.
Signature
setImage(image: ISheetImage): FOverGridImageBuilderParameters
imageISheetImage— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set initial image configuration.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell.
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = await fWorksheet.newOverGridImage()
.setImage({
drawingId: '123456',
drawingType: univerAPI.Enum.DrawingType.DRAWING_IMAGE,
imageSourceType: univerAPI.Enum.ImageSourceType.BASE64,
source: 'https://avatars.githubusercontent.com/u/61444807?s=48&v=4',
unitId: fWorkbook.getId(),
subUnitId: fWorksheet.getSheetId(),
})
.setColumn(5)
.setRow(5)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setPositionAsync
Signature
async setPositionAsync(row: number, column: number, rowOffset?: number, columnOffset?: number): Promise<boolean>Parameters
rownumber— No descriptioncolumnnumber— No descriptionrowOffsetnumber(optional) — No descriptioncolumnOffsetnumber(optional) — No description
Returns
Promise<boolean>— See signature above.
@univerjs/sheets-drawing
setRotate
Set the rotation angle of the image
Signature
setRotate(angle: number): booleanParameters
anglenumber— No description
Returns
boolean— true if the rotation is set successfully, otherwise false
Examples
// set 90 degrees rotation of the image
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setRotate(90);
console.log(result);@univerjs/sheets-drawing
setRow
Set the vertical position of the image
Signature
setRow(row: number): FOverGridImageBuilderParameters
rownumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell.
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)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setRowOffset
Set the vertical offset of the image
Signature
setRowOffset(offset: number): FOverGridImageBuilderParameters
offsetnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// create a new image builder and set image source.
// then build `ISheetImage` and insert it into the sheet, position is start from F6 cell and vertical offset is 10px.
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)
.setRowOffset(10)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
setSizeAsync
Set the size of the image
Signature
async setSizeAsync(width: number, height: number): Promise<boolean>Parameters
widthnumber— No descriptionheightnumber— No description
Returns
Promise<boolean>— true if the size is set successfully, otherwise false
Examples
// set the image width 120px and height 50px
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.setSizeAsync(120, 50);
console.log(result);@univerjs/sheets-drawing
setSource
Signature
setSource(source: string, sourceType?: ImageSourceType): booleanParameters
sourcestring— No descriptionsourceTypeImageSourceType(optional) — No description
Returns
boolean— See signature above.
@univerjs/sheets-drawing
setSubUnitId
Signature
setSubUnitId(subUnitId: string): FOverGridImageBuilderParameters
subUnitIdstring— No description
Returns
FOverGridImageBuilder— See signature above.
@univerjs/sheets-drawing
setUnitId
Signature
setUnitId(unitId: string): FOverGridImageBuilderParameters
unitIdstring— No description
Returns
FOverGridImageBuilder— See signature above.
@univerjs/sheets-drawing
setWidth
Set the width of the image
Signature
setWidth(width: number): FOverGridImageBuilderParameters
widthnumber— No description
Returns
FOverGridImageBuilder— TheFOverGridImageBuilderfor chaining
Examples
// 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 120px and height is 50px.
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(120)
.setHeight(50)
.buildAsync();
fWorksheet.insertImages([image]);@univerjs/sheets-drawing
Actions & Operations
remove
Remove the image from the sheet
Signature
remove(): booleanReturns
boolean— true if the image is removed successfully, otherwise false
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const image = fWorksheet.getImages()[0];
const result = image?.remove();
console.log(result);@univerjs/sheets-drawing
Miscellaneous
toBuilder
Convert the image to a FOverGridImageBuilder
Signature
toBuilder(): FOverGridImageBuilderReturns
FOverGridImageBuilder— The builder FOverGridImageBuilder
Examples
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const images = fWorksheet.getImages();
images.forEach((image) => {
console.log(image, image.toBuilder().getSource());
});@univerjs/sheets-drawing