"Joao B. Oliveira" wrote:
> Antonio wrote:
>>"Joao B. Oliveira" wrote:
>>> See? Does not look that bad, but an even better solution would be
>>> *other* feature: import .cvs files directly as tables.
>>I have a little perlscript that produce a floattable (or a simple table) in
>>lyx-format from a tab-separated-value file. (or comma separteded or "!"
>>separated, how you like) (for 4fix1 and 6fix1)
> I really think that such a script for converting tables would be really
> useful! Why don't you publish it on our list? It is always a nice tool
> to have (and if we don't have it, it is nice to know where to find it!)
Hi folks,
I send you my very little perlscript. If you think it is long, its not true.
there are a only 30 codelines, the rest is documentation.
Documentation?
Yes. pod (perl|poor|pretty old documentation).
on Linux do this: name@host ~/> pod2html csv2lyx > csv2lyx.html
how use it? look the documentation!
ok, ok. a little help (from the documentation :-)
Sorry for my english (if you want I can translate it to italian, german or
french)
ciao, antonio
(my home e-mail: mailto:[EMAIL PROTECTED])
csv2lyx - converter from csv-file to a LyX table
=======
HOW USE IT (under Linux):
=========================
antonio@bidone:~ >cat mycsvfile | csv2lyx > mytmptable.lyx
Then in LyX: Insert->LyXfile mytmptable.lyx
If you don't want a float table, then can you delete the default-title, the word
"Senseless" will are delete.
If you want a float table, then mark the title _and_ the table and then insert a
floattable
Insert->LyX File mytmptable.lyx
[mark the title and the table]
Insert->Float->Float table
the word "Senseless" will change to "Table"
Options
=======
--separator="what you want" (default: "\t" for a tab-sep-value file)
--title="A pretty title for a pretty table" (default:"Title of the LyX table")
==================================================================================
==================================================================================
#! /usr/bin/perl
# csv2lyx
# Copyright (C) 2001 Antonio Gulino
# This program is free software; you can redistribute it and/or modify it under
# the term of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.
=pod
=head1 NAME
csv2lyx - converter from csv-file to a LyX table
Converts a Comma Separated Value file to a float table in LyX format.
Good for LyX version 1.1.4fix1 and other.
=head1 HOW USE IT
under Linux:
antonio@bidone:~ >cat mycsvfile | csv2lyx > mytmptable.lyx
Then in LyX: Insert->LyXfile mytmptable.lyx
If you don't want a float table, then can you delete the default-title, the word
"Senseless"
will are delete.
If you want a float table, then mark the title and the table and then insert a
floattable
Insert->LyX File mytmptable.lyx
[mark the title and the table]
Insert->Float->Float table
=head2 Options
--separator="what you want" (default: "\t" for a tab-sep-value file)
--title="A pretty title for a pretty table" (default:"Title of the LyX table")
=head1 AUTHOR and COPYLEFT
Copyright (C) 2001, Antonio Gulino <[EMAIL PROTECTED]>
This program is free software; you can redistribute it and/or modify it under
the term of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or any later version.
=cut
# l'input non e' il nome di un file, ma il file stesso
# l'output avviene sullo schermo
# dunque l'utilizzo e' cosi:
# antonio@bidone > cat filecsv.csv | csv2lyx | filedioutput
# di default il separatore e' un tabulatore
use Getopt::Long;
$OPT_separator = "\t";
$OPT_title = 'Title of the LyX table';
&GetOptions( "separator=s" => \$OPT_separator,
"title=s" => \$OPT_title,);
@csv = <>;
chomp(@csv);
# vediamo quante righe ci sono
# how many row (=righe) are there?
$righe = $#csv + 1;
# adesso leggo l'intero array e creo una matrice: @matrix
# contemporaneamente calcolo il numero massimo di campi, per sapere quante colonne mi
servono
# now i read the whole array and put it into a matrix : @matrix
# at the same time i look how many column (=colonne) have the biggest row
$col = 0;
foreach my $r (@csv) {
my @r = split $OPT_separator, $r;
$colonne = $#r + 1 if $colonne <= $#r;
push @matrix, [@r];
}
# adesso stampo il codice lyx. l'allineamento avviene a destra (immagino che di solito
siano cifre)
# produco automaticamente una float table,
# versione funzionante per la 1.1.4fix1
# now I print the LyX format. Align=right (I think this is a matrix of numbers)
# dafault: a float table
print <<LYX;
\\begin_float tab
\\layout Caption
$OPT_title
\\layout Standard
\\align center \\LyXTable
multicol5
$righe $colonne 0 0 -1 -1 -1 -1
LYX
;
# qui viene deciso se ci sono righe in alto o in basso di ciascuna riga
# se la prima cifra e' "1" allora c'e una linea in alto
# se la seconda cifra e' "1" allora c'e una linea in basso
# here I say if there are a border
# if the first number is "1", then a border on the top of the line
# if the second number is "1", then a border on the bottom of the line
$top = '0';
$bottom = '0';
print "$top $bottom 0 0\n" foreach @matrix;
# adesso viene deciso il formato delle colonne
# se la prima cifra e' 2, allora allineamento a sinistra
# se la prima cifra e' 4, allora allineamento a destra
# now the align of the column
# if the first number is "2", then align=left
# if the first number is "4", then align=right
$align = '4';
for ($i=0; $i<$colonne; $i++) {print $align, ' 0 0 "" ""',"\n"};
# adesso tocca alla formattazione delle singole celle.
# personalmente non ci capisco nulla e dunque faccio come ho trovato
# le celle della prima riga hanno delle informazioni supplementari che di fatto pare
riguardino
# tutta la colonna sottostante
# la primissima cella pare che abbia una formattazione ancora differente dalle altre
# now the format of each Cell
# I have make only a little "reverse engeneering", i think it's enough.
# the cells of the first row have more informations. These informations are the same
# for all the cells of the same column
# the very very first cell have (so I have understand) an other format
# primissima cella
# very very first cell
print "0 $align 0 0 0 0 0 \"\" \"\"\n";
# prima riga, senza la primissima cella
# first row, whitout the very very fist cell
for ($i=1; $i<$colonne; $i++) {
print "0 $align 1 0 0 0 0 \"\" \"\"\n";
}
# le altre celle, riga per riga, dalla seconda riga in poi
# the other cells, row by row, start with the second row
for ($j=1; $j<$righe; $j++) {
for ($i=0; $i<$colonne; $i++) {
print "0 8 1 0 0 0 0 \"\" \"\"\n";
}
}
# segue una riga vuota
# a empty line
print "\n";
# adesso segue il contenuto dell celle, riga per riga
# mi raccomando di scrivere qualcosa anche per le celle non esistenti nel file iniziale
# now the contents of each cell, row by row
# remember to put something in each cell
for ($j=0; $j<$righe; $j++) {
for ($i=0; $i<$colonne; $i++) {
print $matrix[$j][$i],"\n";
unless ($j==$righe-1 and $i==$colonne-1) {print '\newline',"\n"};
}
}
print '\end_float',"\n\n";
1