Excel Copy Paste

Excel Copy Paste

by Laxmikant Dange

HTML

<div ng-app="TestApp">
  <div ng-controller="TestController">
    <table border="1px">
      <tr>
        <th>Date</th>
        <th>Number</th>
        <th>Text</th>
      </tr>
      <tr ng-repeat="rec in tableData">
        <td>{{rec[0]}}</td>
        <td>{{rec[1]}}</td>
        <td>{{rec[2]}}</td>
      </tr>
      <tr>
        <td>
          <input type="text" ng-paste="textPasted($event);">
        </td>
        <td></td>
        <td></td>
      </tr>
    </table>
  </div>
</div>

JavaScript

angular
  .module('TestApp', [])
  .controller("TestController", function($scope, $clipboard) {
  	$scope.tableData=[];
    $scope.textPasted = function(evt) {
      var txt = $clipboard.getText(evt)
      txt = txt.trim().split('\n').map((r) => r.trim().split('\t'));
      $scope.tableData=$scope.tableData.concat(txt);
      console.log(txt)
    }
  })
  .service("$clipboard", Clipboard);
Clipboard.$inject = ['$window']

function Clipboard($window) {
  this.getText = function($event) {
    console.log($event)
    var text;
    if ($window.clipboardData) { //IE
      text = $window.clipboardData.getData('Text');
    } else if ($event.clipboardData) {
      try {
        text = $event.clipboardData.getData('text/plain');
      } catch (ex) {
        text = undefined;
      }
    }
    if (text) {
      $event.preventDefault();
    }
    return text;
  };
}