# Clipboard operations
HyperFormula supports clipboard operations such as copy, cut, and paste. These methods allow you to integrate the functionality of interacting with the clipboard.
TIP
The methods provided below store cut or copied data in a reference inside the memory, not directly in the system clipboard.
# Copy
You can copy cell content by using the copy
method.
This method requires you to pass the arguments as follows:
- The source left corner of a block of cells to be copied
- Width of the block
- Height of the block
const hfInstance = HyperFormula.buildFromArray([
['1', '2'],
]);
// it copies [ [ 2 ] ]
const clipboardContent = hfInstance.copy({ sheet: 0, col: 1, row: 0 }, 1, 1);
Depending on what was copied, the data is stored as:
- an array of arrays
- a number
- a string
- a boolean
- an empty value
If a copied source formula contains named expressions which were defined for a local scope and it is pasted to a sheet which is out of scope for these expressions, their scope will switch to global. If a copied named expression's scope is the same as the target's, the local scope will remain the same.
# Cut
You can cut cell content by using the cut
method. This
method requires you to pass the arguments as follows:
- The source left corner of a block of cells to be copied
- Width of the block
- Height of the block
Any CRUD operation called after this method will abort the cut operation.
const hfInstance = HyperFormula.buildFromArray([
['1', '2'],
]);
// return the values that were cut: [ [ 1 ] ]
const clipboardContent = hfInstance.cut({ sheet: 0, col: 0, row: 0 }, 1, 1);
# Paste
You can paste cell content by using the paste
method.
This method requires only one parameter - the target left corner
into into which the content will be pasted.
If the paste
method is called after copy
, it will paste
copied values and formulas into a block of cells. If it is called
after cut
, it will perform the moveCells
operation into the
block of cells. The paste
method does nothing if the clipboard
is empty.
The paste
method triggers recalculation of the formulas
affected by this operation. When it is called after cut
, it
will remove the content that was cut. This may have an impact
on many related cells in the workbook.
const hfInstance = HyperFormula.buildFromArray([
['1', '2'],
]);
// do a copy; [ [ 2 ] ] was copied
hfInstance.copy({ sheet: 0, col: 0, row: 0 }, 1, 1);
// do a paste; should return a list of cells whose values changed
// after the operation, their absolute addresses, and new values
const changes = hfInstance.paste({ sheet: 0, col: 1, row: 0 });
Cut and paste behaves similarly to the move
operation, so if the formula
'=A1' is in cell B1 it will stay '=A1' after being placed into B2.
Copy and paste behaves a bit differently. If '=A1' is copied from cell B1 into B2 it will become '=A2'.
# Clear the clipboard
You can clear the clipboard on demand by using the clearClipboard
method. There is also a method for checking if there is any content
inside the clipboard: isClipboardEmpty
.