How do I export a csv file?

Following on from importing a csv file displaying it in a field or a data grid and renaming the headers this lesson will show you how to export the data to a csv file.

Exporting from a field

Exporting from a field

We will take as our starting point a stack set up with a field with csv data in it, as described in the lesson How do I rename the headers on a text file? Add another button to the interface and name it "export":

Scripting the export button

Scripting the export button

Exporting our data will be quite similar to importing with a couple of changes. We will use the ask file command, instead of answer file, as this allows the user to provide the name and location of a new file or choose an existing file. We also want to ensure that we save out as a csv file so we check the file extension of the selected file.

on mouseUp
   local tFileName, tFileContents
   ask file "Please choose a where you want to save the file" with type "Comma Separated Values|csv|TEXT"
   if the result is not "cancel" then
      put it into tFileName
      ## Ensure the file extension is csv
      set the itemDel to "."
      if item 2 of tFileName is not "csv" then
         put "csv" into item 2 of tFileName
      end if
   end if
end mouseUp

Saving the data to file

As before we now have the location of the file we want to save our csv data into, all we have to do now is put the contents of the field into the file. Again we use the URL keyword, but this time we put the data into the url.

## Save the data out to file
put field "text" into tFileContents
put tFileContents into URL ("file:" & tFileName)

Exporting from a data grid

Exporting from a data grid

In the How do I rename the headers on a text file? lesson we imported our data and displayed it in a data grid. What if we want to export from a data grid? Again we can use the same LiveCode to get the file name, we just need to modify the LiveCode that saves the data.

Get the column names

Firstly we need to get the names of our columns, we get the data grid columns property, this is a line delimited list so we need to replace the item separators with commas:

put the dgProp[ "columns" ]  of group "text" into tColumnTitles
replace return with comma in tColumnTitles

Get the data

Next we need the data, we get the dgText of the data grid, the item separator for dgText is tab so we replace tabs with commas as our output is to be comma separated:

put the dgText  of group "text" into tData
replace tab with comma in tData

Save the data to file

Now we just need to save out titles and data to file:

put tColumnTitles & return & tData into URL ("file:" & tFileName)

6 Comments

Michael

Is it possible toread a CSV file in livecode?

I want to create a csv of username, password, details

User types in username and password. If there is a match in the csv gives details else error message "user doesn't exist"

Hanson Schmidt-Cornelius

Hi Michael,

sure LiveCode has great text manipulation capabilities. There are many ways to approach the problem and depending on the size of the data you may also want to consider implementing a mechanism that optimizes search for the relevant items. Maybe this is not necessary, but something you should at least consider.

With regards to how to do it. Just have a look at our Step by Step lessons and search for the word "text" on the web page. You will find a number of lessons that address different parts of what you want to do. The relevant link is as follows: http://lessons.runrev.com/m/4071

Kind Regards,

Hanson

Shane

Hello runrev team,

I'm having difficulty locating the previous tutorial to 'How to export a csv file', could you please forward a link to 'importing a csv file' tutorial!?

Kind Regards,

Shane

Elanor Buchanan

Hi Shane

The previous tutorial is "How do I rename the headers on a text file", you can find it at

http://lessons.runrev.com/s/lessons/m/4071/l/12063-how-do-i-rename-the-headers-on-a-text-file

I have re-orders the lessons so they should be in the correct order now.

Kind regards

Elanor

Ucar

Hello
I want to update a value in an already created CSV file from a LC field.
The file contains both columns and lines. How do i locate the specific line and column that i want to update?
Thank you.

Elanor Buchanan

Hi Ucar

You can update a specific line and column using a LiveCode chunk expression. LiveCode has a concept of 'items' which are separate by commas so you can say something like

put "New text" into item 5 of line 4 of tFileContents

which would update the 5th column in the 4th line of the variable tFileContents.

If you want to update a CSV file you would need to

- Read in CSV file in and store it in a variable
- Update the value in the variable
- Save the contents of the variable out to file

I hope that helps.

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.