Cut, Copy, Paste

Overview

To copy a range of cells, you should use io.keikai.api.Ranges to select them and call paste(Range) with another Range object for destination like:

1
2
3
//src and desitination belong to different sheets
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());

CellOperationUtil

With the help of io.keikai.api.CellOperationUtil, we can easily perform copying and cutting, and it also provides methods for “paste special” such as pasteValue(), or pasteFormula(). These methods all require 2 Range objects as arguments. One is source and another is destination.

To cut a range of cells, you should use

CellOperationUtil.cut(srcRange, destRange);

To copy a range of cells, you should use

CellOperationUtil.paste(srcRange, destRange);

The usages for pasteFormula(), pasteValue(), pasteTranspose(), and pasteAllExceptBorder() are all the same.

Range

Copy cells with Range API is also simple:

1
2
3
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
Range destination = Ranges.range(getDestinationSheet(), "A1");
src.paste(destination, true);

There is also a pasteSpecial() to do special pasting like pasting value only or pasting formula only.

Example

The following codes copy a selection range to the same position of another sheet when a user clicks a button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CopyCutComposer extends SelectorComposer<Component> {

    @Wire
    private Spreadsheet ss;


    @Listen("onClick = #copyButton")
    public void copyByUtil() {
        Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
        Range dest = Ranges.range(getResultSheet(), ss.getSelection());
        CellOperationUtil.paste(src, dest);
    }
    
    //omitted codes...
}
Get code at GitHub

Comments