# Clipboard operations

Through a set of dedicated methods, HyperFormula supports clipboard operations, such as copying, cutting, and pasting. This lets you integrate the functionality of interacting with the clipboard.

The copied or cut data is stored as a memory reference, not directly in the system clipboard.

# Copy

To copy the contents of a cell or range, use the copy() method. Pass arguments of type SimpleCellRange.

const hfInstance = HyperFormula.buildFromArray([
  ['1', '2'],
]);

// copy [ [ 2 ] ]
const clipboardContent = hfInstance.copy({
  start: { sheet: 0, col: 1, row: 0 }, 
  end: { sheet: 0, col: 1, row: 0 },
});

# Cut

To cut the contents of a cell or range, use the cut() method. Pass arguments of type SimpleCellRange.

TIP

Any CRUD operation called after the cut() method aborts the cut operation.

const hfInstance = HyperFormula.buildFromArray([
  ['1', '2'],
]);

// returns the values that were cut: [ [ 1 ] ]
const clipboardContent = hfInstance.cut({
  start: { sheet: 0, col: 0, row: 0 },
  end: { sheet: 0, col: 0, row: 0 },
});

# Paste

To paste the contents of a cell or range, use the paste() method.

paste() requires only one parameter: the top left corner of the target range.

const hfInstance = HyperFormula.buildFromArray([
  ['1', '2'],
]);

// [ [ 2 ] ] was copied
const clipboardContent = hfInstance.copy({
  start: { sheet: 0, col: 1, row: 0 },
  end: { sheet: 0, col: 1, row: 0 },
});

// returns a list of modified cells: their absolute addresses and new values
const changes = hfInstance.paste({ sheet: 0, col: 1, row: 0 });

If the clipboard is empty, the paste() method doesn't do anything.

# Copy and paste

When called after copy(), the paste() method:

  • Pastes the copied data into the target range.
  • Triggers a recalculation of all affected formulas.

TIP

If a formula =A1 is copied from cell B1 into B2, the B2 formula becomes =A2.

# Cut and paste

When called after cut(), the paste() method:

  • Moves the cut data into the target range, by calling the moveCells() method.
  • Removes the cut data from the source range.
  • Triggers a recalculation of all affected formulas.

TIP

If a formula =A1 is cut from cell B1 into B2, the B2 formula becomes =A1.

# Pasting named expressions

If a copied or cut formula contains a named expression defined for a local scope, and the formula is pasted to a sheet that is out of scope for that expression, the expression's scope changes to global.

If the copied or cut named expression's scope is the same as the target's, the expression's local scope remains the same.

# Clear the clipboard

To clear the clipboard, use the clearClipboard() method.

To check if the clipboard holds any data, use the isClipboardEmpty() method.

# Data storage

The copied or cut data is stored as a memory reference, not directly in the system clipboard.

Depending on what was cut, the data is stored as:

  • An array of arrays
  • A number
  • A string
  • A boolean
  • An empty value

# Demo