beforePaste hook does not prevent pasting

Tags: #<Tag:0x00007efc651f3ef8>

Hi.
Here’s a piece of my table:
image

And here’s what I try to copy from excel:
image

I use beforePaste hook to format incoming data and to check if cells has “NumericEditor” type with getCellEditor() method. If at least one is not, I want to prevent pasting.

It works fine as long as all the cells are numeric. Here’s the result:
image

Otherwise, it’s still get pasted. The first column in my example has “DateEditor” type. And when I try to paste there, it’s not prevented even if the hook function looks like this:

beforePaste: function(data, coords) { return false;}

image

What could be the issue and how can I fix that?

Hi @ivan.tselobanov

would you be able to share your lastest progress via fiddle? You can use this demo as a base https://jsfiddle.net/ztr0kL75/

Got it: https://jsfiddle.net/61tzj9qn/33/

Forgot to mention: my current version is 6.2.2
So hot1.getCellEditor(row, col).name always returns “n” here instead of different names for different types as in 6.2.2
Maybe there’s any other way to get cell’s type by coordiunates?

you can use getCellMeta(row_index, column_index).type

Yep. It works. https://jsfiddle.net/61tzj9qn/52/
But with my table it doesn’t work anyway even after upgrading to 8.0.0.

Can’t provide full example since it’s to complicated to paste in sandbox.
Anyway, thanks :slight_smile:

Why it does not work, Ivan?
Do you get some console errors, nothing happens or you get false results?

Console is clear, no errors. The paste just gets done on non-numeric cells even if hook returns false.
Here’s a screen of the table’s constructor (with data names changed):


Maybe there can be some conflicts between hooks?

I wouldn’t say any of those settings can change the behavior. In the screenshot all the below are set to ‘true’?

beforeChange,
afterChange,
beforePaste

No, thats functions passed into constructor, they’re just has the same names as hooks.

Would you be able to pass only the settings for Handsontable and use the data generator (Handsontable.helper.createSpreadsheetData(1000, 1000))? At the first glance, all seems to be set as desired.

I’ve tried it, still no effect though. (Also removed all the other settings):
image

OK, I guess that I know that is wrong. Please confirm if I’m right. It seems that the value is not pasted cause it is blocked by beforePaste but the value that is already in the cell is valid so it triggers the validator for this cell to mark the cell red.

Well, I guess that’s right. Not sure though, because haven’t found any validadions declared in my code yet. I’ll reply with the result later this week.

Hi @ivan.tselobanov

most of the cell types attach their validators. We have 4 predefined validators https://github.com/handsontable/handsontable/tree/master/src/validators and each cell besides ‘text’ and ‘handsontable’ are using one of them.

I see. Is there any way to disable validation inside beforePaste hook then. Or just to disable it entirely to check if thats the issue?

to disable validation you can use the beforeValidate hook.

Here’s an example https://jsfiddle.net/gvn736w9/ I return 0 as all my cells are numeric and 0 is a valid result for checking if a value is a number.

Tried to paste in date column after adding beforeValidate hook. Still no effect.

Date validator needs a date format to pass. Here’s an example https://jsfiddle.net/2afxsc0r/
I pass

beforeValidate: function() {
      return '01/01/1900'
}

Nope, no effect. Wrong values are still get pasted

Could you share your latest progress via JSFiddle demo?