Copy & paste Data Grid data?!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MichaelBluejay
Posts: 234
Joined: Thu Jul 01, 2010 11:50 am

Copy & paste Data Grid data?!

Post by MichaelBluejay » Tue Mar 17, 2020 7:37 am

Since I'm no fan of the Data Grid, I wasn't surprised when I selected some rows and found that the "Copy" command is grayed out, making it impossible for a user to copy data out of the Data Grid and paste into, say, a spreadsheet.

Am I missing something, or is the Data Grid truly this crippled?

If there's no native support, I think solution would be to make a "Copy" button with this code:

Code: Select all

set the clipboardData["text"] to the dgText of grp "myDG"
The good news is that the workaround is a mere single line of code for the whole DG. It does copy the whole DG; if just a selection is needed it gets tricker, but probably still do-able.

Probably there's a way to create my own "Copy" menu item and link it to that script, and also to have it work with Command-C, but I haven't messed with menus yet.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Copy & paste Data Grid data?!

Post by dunbarx » Tue Mar 17, 2020 1:39 pm

Hi.

The DG API has all the things you need to work with columns and lines. You just have to learn it all.

That said, I almost always take the whole dataSet, either with the dgText or dgData, do whatever with it "back in LC", and reload the whole.

Craig

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Copy & paste Data Grid data?!

Post by Klaus » Tue Mar 17, 2020 1:49 pm

Unfortunately a DATAGRID is not a (single) native LC object, but a group of groups of groups of groups with fields
and whatnot and a biiiiiiig library to mange all this. Therefore it is a bit tricky to handle a Datagrid. 8)

For your problem you need to collect the hilited lines of the datagrid first:

Code: Select all

...
put the dgText of grp "myDG" into tData
put the dgHilitedlines of grp "myDG" into tLines
put empty into tDataOfTheSelectedLines
repeat for each item tLine in tLines
  put line tLine of tData & CR after tDataOfTheSelectedLines
end repeat

## Get rid of trailing CR
delete char -1 of tDataOfTheSelectedLines

## Done:
set the clipboardData["text"] to tDataOfTheSelectedLines
...

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Copy & paste Data Grid data?!

Post by FourthWorld » Tue Mar 17, 2020 3:50 pm

Regardless how the DG is implemented, by its flexible nature decisions about what gets copied and how cannot be made by the implementor, necessarily falling on the developer using it.

DG rows are nearly infinite in the various layouts the object supports. They may include plain text, styled text, images, selection controls, and more, or any combination of those. The representation of such varied object types in clipboard data will be different, depending on the details of the use case.

A row may also include hidden elements, such as IDs, which may or may not be relevant in other contexts where the data may be used. Should we include things the user cannot see? Again, I believe it depends on the specifics of a given DG's usage.

In the absence of a one-size-fits-all solution to these questions, a little scripting such as shown above leaves the developer in the driver's seat to make the most appropriate decisions for the case at hand.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Copy & paste Data Grid data?!

Post by bangkok » Tue Mar 17, 2020 4:53 pm

Only one solution.

download/file.php?id=5272

:D

Datagrids are great.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Copy & paste Data Grid data?!

Post by FourthWorld » Tue Mar 17, 2020 5:37 pm

bangkok wrote:
Tue Mar 17, 2020 4:53 pm
Only one solution.

download/file.php?id=5272
I'm on a phone at the moment. I could email a bookmark to myself for when I get back to the office, download the file, decompress it, launch LiveCode, and open the file to see what it is.

But could you take a moment to at least briefly describe it?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Copy & paste Data Grid data?!

Post by bangkok » Wed Mar 18, 2020 1:43 am

FourthWorld wrote:
Tue Mar 17, 2020 5:37 pm

But could you take a moment to at least briefly describe it?
It's a stack of "datagrid tips and tricks" that i did.

(in english)

it shows several ways to deal with those wild but useful "beasts". :D

datagrids are complicated to "grasp" for a beginner. the best way (i believe) is to "show" what they can do. then the learning curve is shorter.

so from time to time, on the forum, i post again the link.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Copy & paste Data Grid data?!

Post by FourthWorld » Wed Mar 18, 2020 2:44 am

Super cool. Sounds very useful. Thanks, Bangkok.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply