1
\documentclass{article}
\begin{filecontents*}{products.tex}
    @ Plant &  60 &  2 \\
    @ Book  &  90 &  4 \\
    @ Other & 100 & 10 \\
\end{filecontents*}
\usepackage{spreadtab,booktabs,xpatch}
\makeatletter
\def\spreadtab@ii{\IfSubStr\ST@tab{\noexpand\input}{\expandafter\spreadtab@iii\ST@tab\@nil}\relax}
\def\spreadtab@iii#1\input#2#3\@nil{%
    \long\def\spreadtab@iv##1\spreadtab@iv{\endgroup\def\ST@tab{#1##1#3}\spreadtab@ii}%
    \begingroup
    \everyeof{\spreadtab@iv\noexpand}%
    \expandafter\spreadtab@iv\@@input#2
}
\xpretocmd\spreadtab@i\spreadtab@ii{}{}
\makeatother
\begin{document}
    \begin{spreadtab}{{tabular}{lcc}}
        \toprule
        @ Product name & @ Price & @ Count \\
        \midrule
        \input{products}
        \midrule
        @ Total & :={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1])\\
        \bottomrule
    \end{spreadtab}
\end{document}

This code just works for three columns, but what I have to change when I want five columns ?

I know that I have to change something here

\makeatletter
\def\spreadtab@ii{\IfSubStr\ST@tab{\noexpand\input}{\expandafter\spreadtab@iii\ST@tab\@nil}\relax}
\def\spreadtab@iii#1\input#2#3\@nil{%
    \long\def\spreadtab@iv##1\spreadtab@iv{\endgroup\def\ST@tab{#1##1#3}\spreadtab@ii}%
    \begingroup
    \everyeof{\spreadtab@iv\noexpand}%
    \expandafter\spreadtab@iv\@@input#2
}
\xpretocmd\spreadtab@i\spreadtab@ii{}{}
\makeatother

but I don't get fix it at all.

So what I want that this

\begin{filecontents*}{products.tex}
    @ Plant & 1 &  60 &  2 & 77 \\
    @ Book  & 2 &  90 &  4 & 64 \\
    @ Other & 3 & 100 & 10 &  6 \\
\end{filecontents*}

can put in a table with this structure

\begin{document}
    \begin{spreadtab}{{tabular}{lcc}}
        \toprule
        @ Product name & @ Number & @ Price & @ Count & Count2 \\
        \midrule
        \input{products}
        \midrule
        @ Total & &:={sumprod(b2:[0,-1];c2:[1,-1])} & sum(c2:[0,-1])& sum(d2:[0,-1])\\
        \bottomrule
    \end{spreadtab}
\end{document}
3

0

Browse other questions tagged .