[csv]

Description

Link: [csv]
Author: Jason Huck
Category: Data Type
Version: 8.x
License:
Posted: Apr. 04, 2006
Updated: Apr. 26, 2006
More by this author...
This is a basic type for reading, writing, and serving CSV files. I've updated the parsing method to use Daniel James' method from this post.


Instance Variables

->fields - An array containing the column names for the CSV file. Will default to [field_names] on creation if/when possible.

->rows - An array of arrays containing the data for the CSV file. Will default to [rows_array] on creation if/when possible.

->loadpath - String, the path to a CSV file to parse into the type.

->titlerow - Boolean, whether or not the CSV file contains a title row. Pertains to both loading and saving files. Defaults to false.

->savepath - String, where to save the CSV file.

->filename - String, the name to use when saving the file. Defaults to "results.csv."


Member Tags/Methods

->parseline - Parses an individual line of a loaded file. Used internally by ->load.

->load - Loads a CSV file from the given path. Optionally accepts a boolean 'titlerow' parameter indicating whether or not the source file contains a title row.

->output - Returns the contents of the object formatted as a CSV file. Used internally by ->save and ->serve.

->save - Saves a CSV file to the given path.

->serve - Serves a CSV file to the browser using [file_serve].

->addrow - Accepts an array to add to ->rows.

Parameters

none


Sample Usage

var('fieldnames') = array('Color','Number','Size');
var('data') = array(
	array('Red',1,'small'),
	array('Green',2,'medium'),
	array('Blue',3,'large'),
	array('Black',4,'extra large with "some" quotes')
);

var('stuff') = csv(
	-fields=$fieldnames,
	-rows=$data,
	-titlerow=true
);
	
$stuff->addrow(array('Orange',5,'tiny'));

$stuff->save('atestfile.csv');

$stuff = null;
$stuff = csv;
$stuff->load('atestfile.csv');
$stuff->output;
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
define_type(
	'csv',
	-prototype,
	-description='A basic type for reading, writing, and serving CSV files.'
);
	local(
		'fields' = array,
		'rows' = array,
		'loadpath' = string,
		'titlerow' = false,
		'savepath' = string,
		'filename' = 'results.csv'
	);
	
	define_tag(
		'onCreate',
		-optional='filename',
		-type='string',
		-optional='loadpath',
		-type='string',
		-optional='titlerow',
		-type='boolean',
		-optional='savepath',
		-type='string',
		-optional='fields',
		-type='array',
		-optional='rows',
		-type='array'
	);		
		if(local_defined('fields'));
			self->fields = #fields;
		else(field_names->size);
			self->fields = field_names;
		/if;
		
		if(local_defined('rows'));
			self->rows = #rows;
		else(rows_array->size);
			self->rows = rows_array;
		/if;
		
		local_defined('filename') ? self->filename = #filename;
		local_defined('savepath') ? self->savepath = #savepath;
		local_defined('titlerow') ? self->titlerow = #titlerow;
		
		local_defined('loadpath') ? self->load( 
			-loadpath=#loadpath,
			-titlerow=self->titlerow
		);
	/define_tag;

	define_tag(
		'parseline', 
		-required='line'
	);
		local(
			'field' = string, 
			'i' = null, 
			'row' = array
		);
		
		iterate(
			string_findregexp(
				#line, 
				-find = '"(?:[^"]|"")*"|[^,]*|,'
			), 
			#i
		);
			if(#i == ',');
				#row->insertlast(#field);
				#field = '';
				
			else(#i->beginswith('"') && #i->endswith('"'));
				#field += #i->substring(2, #i->size - 2)->replace('""', '"')&;
				
			else;
				#field += #i;
				
			/if;
		/iterate;
		
		#row->insertlast(#field);
		
		return(#row);
	/define_tag;

	define_tag(
		'load',
		-required='loadpath',
		-type='string',
		-optional='titlerow',
		-type='boolean'
	);
		fail_if(
			(!file_exists(#loadpath) || !file_read(#loadpath)),
			file_currenterror( -errorcode),
			file_currenterror
		);
		
		self->loadpath = #loadpath;
		local_defined('titlerow') ? self->titlerow = #titlerow;
		
		local('in') = file_read(#loadpath);
		
		iterate(#in->split('\r\n'), local('line'));
			self->titlerow && loop_count == 1 
				? self->fields = self->parseline(#line)
				| self->rows->insert(self->parseline(#line));
		/iterate;
	/define_tag;
	
	define_tag('output');
		local('out' = string);
		
		if(self->titlerow && self->fields->size);			
			iterate(self->fields, local('f'));
				#out += '"' + #f + '"';				
				loop_count == self->fields->size ? #out += '\r\n' | #out += ',';
			/iterate;
		/if;
	
		iterate(self->rows, local('r'));
			iterate(#r, local('f'));
				#f = string(#f);
				#f->replace('"','""')&replace('\r\n','\n')&replace('\r','\n');
				#out += '"' + #f + '"';				
				loop_count == #r->size ? #out += '\r\n' | #out += ',';
			/iterate;
		/iterate;
		
		#out->removetrailing('\r\n');
		
		return(@#out);
	/define_tag;
	
	define_tag(
		'save',
		-optional='savepath'
	);
		local_defined('savepath') ? self->savepath = #savepath;
		
		file_create(
			self->savepath, 
			-fileoverwrite
		);
		
		file_write(
			self->savepath,
			self->output,
			-fileoverwrite
		);
	/define_tag;
	
	define_tag(
		'serve',
		-optional='filename'
	);
		local_defined('filename') ? self->filename = #filename;
		
		file_serve(
			self->output,
			-file=self->filename,
			-type='text/csv'
		);
	/define_tag;
	
	define_tag(
		'addrow',
		-required='data',
		-type='array'
	);
		self->rows->insert(#data);
	/define_tag;
/define_type;

 

Comments

05/20/2008, P Schmitz
This crashes...
Hi! If I export 1-10 lines of my records (each record having 30 fields) it works. Anything more and Lasso crashes (I get an internal server error). I think this is a memory problem. How can I increase the available memory to csv/Lasso? Thanks, P. Schmitz (pschmitz A T hostnet.ch)
09/20/2007, Daniela Kalb
Great! Great!
thx for this type! A million hugs!
Email:


Password:



Newest

Most Popular