AngularJS directive that converts pasted plain text tabular data to an array of arrays/objects on the scope.
<textarea
id="excel-paste"
ng-paste-from="users"
ng-paste-from-columns="['name', 'email']"
ng-paste-from-on-validate="onValidate"
ng-paste-from-on-error="onError"></textarea>
NOTE: It is important that the element that has the ng-paste-from directive supports newlines, otherwise they get excluded and the parse becomes invalid.
Major version 3 depends on Angular ~1.3.11.
Major versions 1 and 2 depend on Angular ~1.2.6.
- Type: variable binding
The variable on the scope that should be assigned the parsed result.
The format of the parsed result depends on ng-paste-from-columns
,
look at that attribute for more information.
- Example:
ng-paste-from-columns="['name', 'email']"
- Parsed result: an array of row objects.
- Parsed result example:
[{"name": "a", "email": "a@a.a"},{"name": "b", "email": "b@b.b"}]
The arrays length becomes the number of columns expected by the parser.
- Example:
ng-paste-from-columns="2"
- Parsed result: an array of row arrays (each array item is a column)
- Parsed result example:
[["a", "a@a.a],["b", "b@b.b"]]
It's the number of columns expected by the parser.
- Type:
RegExp
,String
- Default value:
/\r\n|\n\r|\n|\r/g
Used to split the pasted data into rows.
- Type:
RegExp
,String
- Default value:
"\t"
Used to split the rows into columns.
- Type:
Boolean
- Default value:
true
Only allow pasting into the source element.
- Type:
Function
- Signature:
beforeParse(data)
- Return: the data, or else nothing will be parsed.
Callback to manipulate the data before it is parsed.
- Type:
Function
- Signature:
onValidate(row, rowData)
- Arguments:
- row: (Object or Array) See the
ng-paste-from-columns
option for details on this value - rowData: (Object)
- index: (Number, Int) Index of the row in the split source text, before trimming empty lines
- source: (String) Row source text
- expectedLength: (Number, Int) Expected number of columns in the row
- actualLength: (Number, Int) Actual number of columns in the row
- row: (Object or Array) See the
Callback to validate a row object or array.
See the ng-paste-from-columns
attribute for more details on when a row will
be an object or an array.
- Type:
Function
- Signature:
onError(error, rowData)
- Arguments:
- error: (String) Name of the error, see the Errors section of this README
- rowData: (Object)
- index: (Number, Int) Index of the row in the split source text, before trimming empty lines
- source: (String) Row source text
- expectedLength: (Number, Int) Expected number of columns in the row
- actualLength: (Number, Int) Actual number of columns in the row
Callback to handle errors.
Error | Description |
---|---|
NGPASTEFROM_INVALID_COLUMN_LENGTH | The processed row does not have the correct number of columns |
NGPASTEFROM_FAILED_VALIDATION | The processed row failed validation |
- Install NodeJS (nodejs.org)
- Install Gulp globally:
npm install -g gulp
- Fork the repo and clone it. (How to do it with GitHub.)
- Go into the project folder:
cd ng-paste-from
- Install the project dependencies:
npm install
- Build the project files:
gulp
- Build them whenever they change:
gulp watch
To make development of ng-paste-from easier you can check out the master
branch in one directory and the gh-pages
branch in another, then symlink the dist files from master
into the lib
directory in gh-pages
, and change the includes in index.html
to use those versions.
Chrome, Firefox, Safari and IE9+
Licensed under MIT
Copyright (C) 2014 DigiExam