Ng-Paste - Retrieving Data from Clipboard as a List or Array

The Concept

Currently, we are in the process of developing an Angular 1.5.x app and are exploring ways to incorporate a feature that allows users to paste a single column of cells from an excel sheet or another spreadsheet (regardless of row count) into an input field.

Within our data table, there are inline inputs setup as follows:

The concept revolves around pasting data from a single column of a spreadsheet into one of the inputs, with each cell's value being assigned to the corresponding input in descending order. For instance, if a column with values [4.52, 6.235, 9.2301] is pasted into the top input containing 15.23, then 15.23 should transform into 4.52, 3.1234 into 6.235, and 3.1322 into 9.2301. We have figured out how to assign these variables to the inputs but require clipboardData in array format instead of one cohesive string.

The Issue at Hand

We have explored using the ng-paste directive along with the $event.clipboardData property, yet we can only access the data as a string. Although we possess a means of parsing the string, having the elements delivered as an array or list would reduce potential errors on our part when employing delimiters to segment the string.

Linked here is an active plunker showcasing our current efforts.

Below is a sample dataset for copying and pasting into the designated input:

Interestingly, when transferring a column from an excel sheet, it doesn't contain any separators between values. Splitting on the '\n' character works seamlessly when extracting data across multiple columns within a single row. Our aim is to enable users to copy from both horizontal rows and vertical columns; however, the challenge arises due to the lack of delineation when pasting a column from excel.

0.89663.91783.91773.91773.9178

These are the values copied from an excel/google sheet^, but arranging them in a singular column will suffice. Rest assured, the entered data will primarily derive from a spreadsheet source.

A Potential Resolution

Do you have any insights on extracting clipboard data formatted as an array?

Answer №1

Here's the solution you need:

HTML:

<input ng-paste="performPaste($event)"><br />
<p>Display pasted input as array:</p>
<p ng-repeat="data in pasted track by $index">{{data}}</p>

JavaScript:

$scope.performPaste = function(event) {
    $scope.pasted = event.clipboardData.getData('text').split(" ").map(Number);
    console.log($scope.pasted);
}

See it in action: Live Demo

Answer №2

An innovative workaround may involve incorporating additional data rather than relying solely on current information. The revised body code would look like this:

<body ng-controller="MainCtrl">
  <input ng-paste="pasteFunction($event)">
  <input ng-repeat="t in test" ng-model="t"/>
</body>

The controller will need to be adjusted accordingly:

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.obj = {};
  $scope.test = [111, 222, 333];
  $scope.pasteFunction = function(ev) {
    $scope.test = ev.clipboardData.getData('text').split(" ").map(Number);
  }
  // input this: 999 888 777
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending JSON data back to React in a file format

I have a json file within the project that contains the necessary data to display in my component. Can someone guide me on how to properly access the json file in react and output the data from it? data.json { "dictionary": [ { "index": "1", ...

How can the 'Read more' feature be modified to impact multiple boxes? (Using jQuery, JS, and CSS)

I am trying to add a 'Read more' feature on my friend's website. I was able to achieve the desired effect, but when I tried to adjust the alignment of the box, it affected the 'Read more' feature. Original design: https://codepen. ...

Extracting the value of an HTML element from a string variable in AngularJS

I am facing an issue with my application where the content of an HTML element is received as a template from the server. I am attempting to assign this template, which is essentially a string, and have the variables within the template linked to the contro ...

Scrolling text box utilizing Jquery

Currently, I am utilizing a scrolling box that functions well * view here * under normal circumstances. However, when there is an extensive amount of content below it, such as: <article class="content"> ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

Executing a JavaScript function within MainPage.xaml.cs codebehind file in a Windows application

I am currently working on a project developing a Windows 8.1 app using HTML5 and Javascript (Silverlight). I have encountered an issue with implementing the functionality for the hardware back button. Within the MainPage.xaml.cs Codebehind file, I need to ...

Retrieving information from mlab without needing to make a new request, for example, updating the front end without refreshing the page

I understand that many people have asked questions similar to mine, but my issue is unique. I am trying to retrieve data from my mlab database without having to reload the page. For example, when a new cricket score is added to my db in mlab, I want this s ...

Is it possible to pre-select a value in a PrimeVue Dropdown component?

Situation: Currently, I am incorporating PrimeVue into a Vue.js project. Within this setup, there is a dropdown component sourced from PrimeVue, utilizing an array of objects. The structure of the dropdown component appears as follows: <template #positi ...

What steps can I take to resolve the issue of my popup closing automatically upon opening and simultaneously refreshing my webpage?

After creating a popup in my HTML page, when I click on the element that triggers it, the popup appears for a brief moment before automatically closing and causing my page to refresh. Below is the code I am using: HTML Code : <a class="lv-footer" id= ...

Combine two columns into a single table column using HTML/CSS and JavaScript with the help of Datatables

I utilize the DataTables library from https://datatables.net/ to create sortable and searchable tables on my website. However, my table becomes too wide due to the numerous columns it contains. I am looking for a way to condense the width by merging relate ...

Encountering the issue of "Cannot read properties of undefined" while attempting to pass data to a prop

Currently, I am developing a Vue application that heavily relies on charts. The issue lies in the fact that I am fetching data from the database individually for each chart component, resulting in multiple calls and a slower page load time. I am looking to ...

Initiate modal from sub-component

Right now, I am developing a vue application with a structure that closely resembles the following: <div class="characters"> <Character v-for="character in this.charactersToSearch" :key="character.id" :name="cha ...

Is there a way to merge two arrays with a specific condition?

I need to update the state in one array based on the values of another array. The goal is to include all statuses from the second array into the first without any duplicates. However, the provided sample code is not comprehensive. const arr1 = [{ agentId: ...

The collision function is currently not functioning properly, causing a hindrance in movement. There are no other codes restricting movement

const rightPressed = false; const leftPressed = false; const upPressed = false; const downPressed = false; const players = []; players[0] = new victim(1234); const arrayw = 50; const arrayh = 50; const canvas = document.getElementById("myCanvas"); const ...

Getting the Featured Image from a Post Link in Wordpress: A Step-by-Step Guide

For my Wordpress project, I have a group of links leading to inner pages, each with their own featured image. These links are created using the menu feature in Wordpress. My goal is to allow users to click on these links and have the featured image from t ...

JavaScript: Adjust DIV Width Based on Window Height

Is there a way to make the width of a div equal to the height of the window in jquery? I attempted this method: <script type="text/javascript"> $(".rt-container").style.width = $(window).height(); </script> Unfortunately, it did not work. I ...

What is the best way to link multiple return statements in Node.js using ioredis?

Currently utilizing ioredis and interested in retrieving the path and value shown in the code snippet below up to the anonymous function. console.log( function (jsonGraphArg) { return Redis.hget(jsonGraphArg[0], jsonGraphArg[1], function(error ...

Errors in Compiling Dependencies for d3.js Using Typescript

Currently, I am in the process of developing a web application utilizing Node.js alongside Angular, Typescript, and d3.js, among other technologies. The application is functioning properly with library features working as expected. However, I am encounteri ...

css problem with scaling in firefox

My goal is to create a website that adjusts its size based on the document size. When the document size is between 0px and 1024px, I want the design to be responsive. This can easily be achieved with CSS media queries. Similarly, when the document size is ...

Preventing preventDefault() from firing in JQuery only when necessary

I have come up with a script that I recently created. <script> $(document).ready(function(){ $('a').data('loop',true); $('body').on('click', 'a', function(event){ console.lo ...