Plugin to fill cells with color and turn your worksheet into an image

Hello!

The cell colorizer plugin fills cells with color to display images in spreadsheets. One of our engineers created it just for fun and explained the development process. We hope that this example will inspire you to create your own plugins.

Create plugins for ONLYOFFICE

How it works

 

  1. You upload an image (jpg, png, bmp).
  2. The plugin fills cells in a spreadsheet with its pixels color.
  3. You get the image in your spreadsheets (it looks weird but somewhat enjoyable).

You can find the plugin code in this repo.

Plugin structure

This plugin (as well as any plugin for ONLYOFFICE editors) contains these parts:

  1. HTML file with plugin interface.
  2. js file with plugin code.
  3. JSON file with plugin config.

Take these template files if you want to create your own plugin.

Changing config

Let’s work with config.json for a start.

It’s convenient to have a plugin interface on the left panel. To do so, let’s add these parameters:

“isVisual” means that the plugin will have its own interface, “isInsideMode” means that it will appear on the left panel, not in a modal window.

The cell colorizer plugin works for spreadsheets only, so let’s remove all the values from “EditorsSupport” parameter, except for “cell”. This way the plugin will appear only in the spreadsheet editor.

Changing HTML

Let’s add an interface for uploading images and a tag for displaying an uploaded image.

In order not to fill too many cells (we want to colorize the visible area only), we need to scale the image. This is a task for canvas that will also help us get the right colors for pixels.

Creating code

Let’s create variables for easy access to canvas and image:

After a user uploads image, we need to display it in our interface and then start drawing on canvas and filling cells.

Let’s create an event for displaying image:

After the image has been displayed in the plugin, we draw it on canvas.

The image can be really big, thousands of pixels in width. We don’t want to fill that many cells. Let’s downsize the canvas.

For example, if we want to fill only 70 cells for the image width and any number of cells for its height, we need to set canvas width to 70. Canvas height should have the value image_height * 70 / image.width.

After that, you can paint over the entire canvas with the image and take the array of canvas pixels to fill the cells.

Asc.scope.st – is an object that allows passing data as a string to the window.Asc.plugin.callCommand method. Details can be found here.

ctx.getImageData(0, 0, canvas.width, canvas.height).data – here we get an array of pixel colors. Data format is [r1,g1,b1,a1,r2,g2,bg2,a2…].

Let’s add the method to fill the cell with the colors we got from canvas:

window.Asc.plugin.callCommand is the method to work with editors via API. Details here.

Let’s add variables and constants that we are going to work with:

After that, we add two nested loops to move between spreadsheet rows and columns:

It makes sense a simplified conversion formula from rgba to rgb since the color of the cells is always white:

After we fill the current cell with color, we move to the next one:

After we add the plugin to the editors, it will look like this:

Weird, right? Let’s fix the style.

We need to hide the canvas and display the image instead. Also, let’s add change input for the file:

If you want, you can add cell width changing to the cycle, so that the cells become square.

This way the image won’t be too stretched:

cell_colorizer plugin for ONLYOFFICE

That’s all!

Here are some resources that we help you write your own plugins:
 

  • General information on plugins in our blog
  • Instructions on plugins structure and installation are here.
  • Information on plugins code and methods is here.

If you have problems when creating your own plugins, create issues in this repo.

 

Add a comment