How to access formula information during the clipboard paste event in SpreadJS
When working with SpreadJS, you may want to detect and inspect formulas when a user copies and pastes cells. While the ClipboardPasted event provides information about the paste operation, it does not directly expose the formula data in the event arguments.
This article explains how to retrieve formula information from pasted cells using the ClipboardPasted event in combination with the getFormula method.
To retrieve formulas from pasted cells, you can use the ClipboardPasted event together with the getFormula method. After the paste operation completes, the formula is already applied to the target cells, allowing you to read it directly from the worksheet.
First, ensure that clipboard options allow all content (including formulas) to be pasted:
sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.all;
Then, handle the ClipboardPasted event and iterate through the pasted cell range to retrieve formulas.
spread.bind(GC.Spread.Sheets.Events.ClipboardPasted, (e, args) => {
let sheet = args.sheet;
let cellRange = args.cellRange;
for (let i = cellRange.row; i < cellRange.row + cellRange.rowCount; i++) {
for (let j = cellRange.col; j < cellRange.col + cellRange.colCount; j++) {
let formula = sheet.getFormula(i, j);
if (formula) {
console.log("Cell (" + i + "," + j + "): " + formula);
}
}
}
});
Using getFormula in combination with the pasted cell range allows you to fully inspect and handle formulas during clipboard operations in SpreadJS.
Kristina Ismail