IMPORTANT ANNOUNCEMENT

These forums were permanently set to read-only mode on July 20, 2022. From that day onwards, no new posting or comment is allowed on the site, but the historical content remains intact and searchable.

A new location for posting questions about PlanetPress Suite is now available:

OL Learn - PlanetPress Classic (opens in new tab)

Page 1 of 2 1 2 >
Topic Options
#49551 - 10/02/14 10:46 AM Using Comma Delimited CSV
strido Offline
OL Expert

Registered: 08/06/13
Posts: 159
Loc: MA
I'd like to design a form, just like an invoice or some such but the input data is .CSV (Comma delimited).

It seems to break each record into a separate data page, meaning data page 1 is actually the first row in the .CSV, usually associated with the headers. (Name, Address, City, State, Zip, QTY, etc....)

Each sucessive data page is the next row in the CSV, containing the appropriate values.

My question is this - assuming 20 records under each header, how would I display all 20 on ONE page, as opposed to displaying 20 pages containing only 1 record?

I'm assuming it's got to do with Line Repeat, but I'm unsure how to accomplish it even after reading up on it.

Top
#49552 - 10/02/14 11:17 AM Re: Using Comma Delimited CSV [Re: strido]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 316
Loc: London, UK
Write a function to break the CSV file into a set of lines and fields which can be referenced as an index.

Unfortunately this is a limitation of working with CSV otherwise.

Try this as a function:
Code:
function @ExtractData(&line:integer, &fieldnum:integer):string

define(&i, integer, 0)
define(&pos, integer, 1)
define(&curstr, string, '')
define(&curline, string, '')
define(&inquotes, boolean, false)
define(&j, integer, 0)

% Grab current line and store in working variable
&curline := @(&line, 1, 1000)

% Loop over line and replace any blanks with a space ('j' is semantic here)
for(&j, 1, 1, 100)
        &curline := stringreplace(&curline, ',,', ', ,')
endfor()

% Outer loop
repeat
        &curstr := ''
        % Loop until you see another '¦' or line is blank
        repeat
                % Look at data field 1,1
                &curstr := &curstr + mid(&curline, &pos, 1)
                % If we see caret, next char is data
                if(mid(&curline, &pos, 1) = '^')
                        % Checks quotes to see if data field exists (quotes = true)
                        if(&inquotes)
                                &inquotes := False
                        elseif()
                                &inquotes := True
                        endif()
                endif()
                % Counts the amount of chars in the data field
                &pos := &pos + 1
        until(((mid(&curline, &pos, 1) = '¦') or (&pos > length(&curline))) and (not(&inquotes)))
        % Loop counter
        &i := &i + 1
        &pos := &pos + 1
% &fieldnum specifed in function parameters
until(&i = &fieldnum)

% Strip out unwanted chars
&Result := stringreplace(stringreplace(&curstr, '^', ''), '¦', '')
endfunction()

Obviously you will need to change the tokens to reflect the actual delimiters you are using but this should do the trick!

1. Make a custom data selection to say "=@ExtractData(line:integer,field:integer)" where line is your first line of data and field is the column in the CSV you want. Now we want to repeat so set line to &current.line and the field number to the column you are creating - i.e. "=@ExtractData(&current.line, 1)"
2. Repeat this step for each column/field you want to display.
3. Now group all fields together to form one line of your data.
4. Set a line repeat on the group for the amount of data lines you have - i.e. From line 2 to 10 etc.

Let me know how you get on.


Edited by jim3108 (10/02/14 11:19 AM)

Top
#49554 - 10/02/14 11:25 AM Re: Using Comma Delimited CSV [Re: strido]
Dal Offline
OL Guru

Registered: 08/29/08
Posts: 114
Loc: UK
Not tried this, but you should be able to make a custom sized Virtual page which is just big enough to hold a single line of csv data.

Then on your physical page, use an NUp Printing object to call the virtual page - make sure you set the checkbox "Change Datapage with each repeat".

I reckon this would cover it without scripting the datafile up front.

Top
#49555 - 10/02/14 11:40 AM Re: Using Comma Delimited CSV [Re: strido]
strido Offline
OL Expert

Registered: 08/06/13
Posts: 159
Loc: MA
I'll try these solutions out - it's good that this particular instance isn't related to any production issues. I'm just kicking the tires, really. Thanks guys!

There should be better support for CSV in my opinion - I wonder if we'll get it in a future release?


Edited by strido (10/02/14 11:41 AM)

Top
#49556 - 10/02/14 12:15 PM Re: Using Comma Delimited CSV [Re: strido]
Benoit Potvin
Unregistered


Hi,

have you tried putting 21 pages in the buffer? i.e. set the 'Pages in Buffer' property to 21 in the emulation configuration.

When you set up the emulation as CSV, the number of lines per page is the max number of columns. For example, if each csv line has 30 columns, you can set up the emulation with 30 lines per page. Then, if you set Pages in buffer = 21, your data page will contain 21*30= 630 lines. Using a line repeat (or PlanetPress Talk) and some math calculation, you can then cycle through the entire subset of csv lines. For example, the first column of each line would be in row #31, 61, 91, etc.

Of course, this assumes that your data file will always contain 21 CSV lines per 'record'.

Hope this helps!


Edited by Benoit Potvin (10/02/14 12:17 PM)

Top
#49558 - 10/02/14 12:46 PM Re: Using Comma Delimited CSV [Re: strido]
strido Offline
OL Expert

Registered: 08/06/13
Posts: 159
Loc: MA
Jim - where does the function you posted go exactly?

Top
#49560 - 10/02/14 01:04 PM Re: Using Comma Delimited CSV [Re: ]
strido Offline
OL Expert

Registered: 08/06/13
Posts: 159
Loc: MA
Originally Posted By: Benoit Potvin
Hi,

have you tried putting 21 pages in the buffer? i.e. set the 'Pages in Buffer' property to 21 in the emulation configuration.

When you set up the emulation as CSV, the number of lines per page is the max number of columns. For example, if each csv line has 30 columns, you can set up the emulation with 30 lines per page. Then, if you set Pages in buffer = 21, your data page will contain 21*30= 630 lines. Using a line repeat (or PlanetPress Talk) and some math calculation, you can then cycle through the entire subset of csv lines. For example, the first column of each line would be in row #31, 61, 91, etc.

Of course, this assumes that your data file will always contain 21 CSV lines per 'record'.

Hope this helps!


Is there a way to learn presstalk without having been a programmer?

Top
#49561 - 10/02/14 03:08 PM Re: Using Comma Delimited CSV [Re: strido]
Benoit Potvin
Unregistered


Quote:
Is there a way to learn presstalk without having been a programmer?


Of course!

While having a background in programming definitely helps to learn PlanetPress Talk, you can still learn how to write PlanetPress Talk functions and expressions, and the PlanetPress Talk syntax without programming experience.

There is an online course on PlanetPress with a section on PlanetPress Talk which contains useful information for both programmers and non-programmers.

https://ollearn.objectiflune.com/course/view.php?id=48

Hope this helps!

Top
#49568 - 10/03/14 05:40 AM Re: Using Comma Delimited CSV [Re: strido]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 316
Loc: London, UK
Originally Posted By: strido
Jim - where does the function you posted go exactly?

Sorry for the late reply.

Put the function in Global Functions in your Design Form - just literally create a new function and paste my code.

Change the tokens/delimiters as I suggested and then call the function using:
Code:
@ExtractData(line,column)

clearly replacing "line" and "column" with my instructions from above.
NB: Remember that injection of PT in page objects requires preceeding "="

If you need to email me you can and I'll take a look for you.

Better than creating virtual pages as the script gives you more flexibility.

Originally Posted By: strido
Is there a way to learn presstalk without having been a programmer?

Finally, it should be added that without a solid background in programming, it is going to be much more difficult to learn PressTalk (or Delphi).


Edited by jim3108 (10/03/14 05:52 AM)

Top
#49573 - 10/03/14 08:14 AM Re: Using Comma Delimited CSV [Re: strido]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1984
Loc: Objectif Lune, Montreal, Qc
Strido,

Native CSV support in PlanetPress is essentially designed to make it easy for data streams that have one data record per page. To process multiple (and variable) records per page, it would be far easier to use the database emulation.

I suggest you take a look at implementing an ODBC connection to your CSV file (that's natively available on any version of Windows). From then on, you'll have access to all fields as you would in a standard recordset returned from a standard database.
_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
Page 1 of 2 1 2 >