Skip to main content Skip to footer

Prevent Specific Characters from being Pasted

To determine what cannot be pasted from the clipboard by users, we can override the ClipboardPasting event. This event triggers before a paste occurs. For example, we could restrict alphabetic characters as well as a list of non-alphabet symbols. See the following sample code: 

let spread = args.spread;
let sheet = spread.getActiveSheet();

function containsAlphabetAndSpecialCharacter(str) {
    // Regular expressions to check for alphabet and special character
    const hasAlphabet = /[a-zA-Z]/;
    const hasSpecialCharacter = /[!@#$%^&*(),.?":{}|<>]/;

    // Check if the string contains both an alphabet and a special character
    return hasAlphabet.test(str) || hasSpecialCharacter.test(str);
}

spread.bind(GC.Spread.Sheets.Events.ClipboardPasting, function (sender, args) {
    const pastingData = args.pasteData.text;

    if (containsAlphabetAndSpecialCharacter(pastingData)) {
        args.cancel = true;
    }
});

Tye Glenz