import csv file and rename columns

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
westerw
Posts: 14
Joined: Tue Apr 09, 2013 7:33 pm

import csv file and rename columns

Post by westerw » Wed May 22, 2013 3:59 pm

i am able to import the data from csv file OK. the file has no header column record.
On the first import, the column names were "Col 1" ... "Col ##"
I put in set dgColumnName statements to rename the columns, but not all of them happened.
Now I am left with a datagrid that has partial column renames.

When I run the code, it appears that if the set command fails, the rest of the code does not execute.
How can I "trap" this apparent error and let the remaining statements execute.
In Visual Basic and Java, there is a "try-catch" methodology.

CODE:
on mouseUp

-- locate the CSV file for the data grid (dgKnights)
local tFileName
answer file "Please choose a file to import" with type "Comma Separeted Values|csv|CSV"

if the result is not "cancel" then

-- save the FilePathName
put it into tFileName

-- get the contents of the CSV file
put URL ("file:" & tFileName) into tFileContents

-- display the file contents in the data grid
replace comma with tab in tFileContents
set the dgText[false] of group "dgBaseRec" to tFileContents

-- rename the columns for BaseRec file
set dgColumnName ["Col 1"] of group "dgBaseRec" to "ID Number"
set dgColumnName ["Col 2"] of group "dgBaseRec" to "RelationLink"
set dgColumnName ["Col 3"] of group "dgBaseRec" to "AddressLink"
... rest of code now shown
end if

end mouseUp


Thank you
bill

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

Re: import csv file and rename columns

Post by Klaus » Wed May 22, 2013 4:28 pm

Hi Bill,

1. welcome to the forum! :-)

2. do you get an error? Does it work when you do NOT set the column names?
Maybe you can just set the column labels and keep the numbered column names?

Hint: NEVER forget the THE keyword when addressing custom properties:
---
set THE dgColumnName ["Col 1"] to ...
...
;-)


Best

Klaus

alessandropisoni
Posts: 12
Joined: Fri Jul 07, 2006 2:14 pm
Contact:

Re: import csv file and rename columns

Post by alessandropisoni » Sat Jun 01, 2013 8:56 am

i have this problem:
I want to create a datagrid where the column headings are the first line of a variable or a field TAB. It all works until I enter in the code the column name in quotation marks. It does not work when this parameter comes from a variable. Why? What could be wrong? the problem is in the last line of code with a set .....

thanks

on mouseUp
--put empty into field "OrdiniNazioni" of card "Ordini-Andamento"
set the dgData of group "ElencoOrdiniQuadrimestre" of stack "Programma" to empty
put empty into fld"RisultatoQuadrimestri"
set the itemDelimiter to "-"
put TAB & TAB after fld"RisultatoQuadrimestri"
put 3 into VAR_RIGHECOLONNE
repeat for each lines VAR_RIGA in field "OrdiniNazioni" of card "Ordini-Andamento"
if item 1 of VAR_RIGA <> "MBM srl " then
put TAB & Item 1of VAR_RIGA after fld"RisultatoQuadrimestri"
add 1 to VAR_RIGHECOLONNE
end if

end repeat
put TAB after fld"RisultatoQuadrimestri"
add 1 to VAR_RIGHECOLONNE
put empty into i

--CREATED AN EMPTY LINE AS ARE THE COLUMNS THAT I NEED
repeat until i = VAR_RIGHECOLONNE
add 1 to i
put TAB after VAR_NOMICOLONNE
end repeat


set the dgText of group "ElencoOrdiniQuadrimestre" of card "Ordini-Andamento" of stack "Programma" to VARNOMICOLONNE
set the dgColumnName ["Col 1"] of group "ElencoOrdiniQuadrimestre" to "Anno"
set the dgColumnName ["Col 2"] of group "ElencoOrdiniQuadrimestre" to "MBM Italia"
set the dgColumnName ["Col 3"] of group "ElencoOrdiniQuadrimestre" to "MBM O.T.I."
set the dgColumnName ["Col "&RIGHECOLONNE] of group "ElencoOrdiniQuadrimestre" to "Totali"

put 3 into i
set the itemDelimiter to TAB
repeat until i = VAR_RIGHECOLONNE - 1
add 1 to i
put item i of field "RisultatoQuadrimestri" of card "Ordini-Andamento" into VAR_NOMECOLONNA
set the dgColumnName ["Col "& i ] of group "ElencoOrdiniQuadrimestre" of card "Ordini-Andamento" of stack "Programma" to VAR_NOMECOLONNA
end repeat
end mouseUp

Post Reply