Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
bsouthuk
- Posts: 261
- Joined: Fri Dec 05, 2008 7:25 pm
Post
by bsouthuk » Wed Nov 30, 2011 1:29 pm
Hi
I am trying to import data from a csv file into a datagrid which I have done with the following code:
Code: Select all
on mouseUp
local tFileName, tFileContents
answer file "Please choose a file to import" with type "Comma Separated Values|csv|CSV"
if the result is not "cancel" then
put it into tFileName
## Get the contents of the file
put URL ("file:" & tFileName) into tFileContents
## Display the file contents in the field
put line 1 of tFileContents into tColumnTitles
replace comma with return in tColumnTitles
//set the dgProp[ "columns" ] of group "MobileNumbersDataGrid" to tColumnTitles
end if
replace comma with tab in tFileContents
set the dgText[true] of group "MobileNumbersDataGrid" to tFileContents
## Get the complete data
put the DGData of grp "MobileNumbersDataGrid" into tArray
## Set the data of the datagrid back to the now modified array
set the DGData of grp "MobileNumbersDataGrid" to tArray
end mouseUp
What I need to do now, is upload the data from the csv file into 1 column of the datagrid withought going over/replacing any of the data from the other columns of the datagrid.
If anyone could help I would be most grateful.
Thanks
Daniel
-
Klaus
- Posts: 14213
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Wed Nov 30, 2011 5:51 pm
Hi Daniel,
you need to loop through the complete DGDATA of your datagrid and only update the column you want/need.
Something like this, out of my head and untested
Code: Select all
...
## tFileContent = the content of your CSV file
## I leave out that part!
## WAHTEVER = the name of the 1 column you want to fill/modify/overwrite
put the dgData of grp "MobileNumbersDataGrid" into tDGData
put 1 into tCounter
repeat for each key tKey in tDGData
put line tCounter of tFileContent into tDGData[tKey]["WHATEVER"]
## or ITEM 1 of line tCounter... ?
add 1 to tCounter
end repeat
set the dgData of grp "MobileNumbersDataGrid" to tDGData
...
Of course this requires that the number of lines of tFileContent = the number of lines/keys in your datagrid!
Best
Klaus
-
bsouthuk
- Posts: 261
- Joined: Fri Dec 05, 2008 7:25 pm
Post
by bsouthuk » Sat Dec 03, 2011 3:43 pm
Thats great Klaus - works perfectly!
Cheers
Daniel