Disabling paste operations in specific columns in SpreadJS
In some scenarios, you may want to restrict users from pasting content into certain columns in a SpreadJS worksheet. This can be useful for maintaining data integrity, especially in columns that are populated by formulas or reserved for system use.
In this article, we demonstrate how to use the ClipboardPasting event in SpreadJS to disable pasting in specific columns. In our sample, pasting is disabled in columns B and D (index 1 and 3), regardless of whether the paste is done via keyboard shortcuts or context menu.
Users can still manually type or edit cells in these columns, but the paste operation is restricted.
const spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); const sheet = spread.getActiveSheet(); // blocking paste operations in columns B and D const restrictedColumns = [1, 3]; // adding sample cells sheet.getCell(0, 0).text("Copy this cell"); // Column A - allowed sheet.getCell(0, 1).text("Try pasting here"); // Column B - restricted sheet.getCell(0, 3).text("Try pasting here too"); // Column D - restricted // binding ClipboardPasting event to block paste in restricted columns spread.bind(GC.Spread.Sheets.Events.ClipboardPasting, function (e, args) { let pasteRange = args.cellRange; // checking if any column in the paste range is restricted for (let col = pasteRange.col; col < pasteRange.col + pasteRange.colCount; col++) { if (restrictedColumns.includes(col)) { args.cancel = true; alert("Pasting is disabled in the selected column."); break; } } });