从 CSV 传递变量时可能存在的错误

从 CSV 传递变量时可能存在的错误

我认为从 CSV 文件调用十进制数字时存在错误。请查看。

  1. 创建一个文件夹。

  2. 添加一个名为的 CSV 文件,texDemo.CSV其中包含以下内容。

    UniqueID,Name,DecimalPercentageComplete,PercentageComplete
    1,Task1,0.45,45
    2,Task2,0.95,95
    3,Task3,0.10,10
    4,Task4,1,100
    
  3. .tex在名为的同一文件夹中创建文档CallFromCSV.tex并添加以下代码。由于自动格式化,它看起来有两行分开。确保将所有行添加到文件中CallFromCSV.tex

    \usepackage{datatool}
    \DTLloaddb{CHCplanDB}{texDemo.csv}
    \newcommand\GetUniqueIDCHC[1]{
    \DTLforeach*
    [\equal{\UniqueID}{#1}]% condition
    {CHCplanDB}% database
    {\UniqueID=UniqueID,\UniqueID=UniqueID}% assignments
    {\UniqueID }% body
    }
    \newcommand\GetUniqueIDCHC[1]{
    \DTLforeach*
    [\equal{\UniqueID}{#1}]% condition
    {CHCplanDB}% database
    {\UniqueID=UniqueID,\UniqueID=UniqueID}% assignments
    {\UniqueID }% body
    }
    \newcommand\GetTaskNameCHC[1]{
    \DTLforeach*
    [\equal{\UniqueID}{#1}]% condition
    {CHCplanDB}% database
    {\UniqueID=UniqueID,\Name=Name}% assignments
    {\Name }% body
    }
    \newcommand\GetPercentageCompleteCHC[1]{
    \DTLforeach*
    [\equal{\UniqueID}{#1}]% condition
    {CHCplanDB}% database
    {\UniqueID=UniqueID,\PercentageComplete=PercentageComplete}% assignments
    {\PercentageComplete }% body
    }
    \newcommand\GetDecimalPercentageCompleteCHC[1]{
    \DTLforeach*
    [\equal{\UniqueID}{#1}]% condition
    {CHCplanDB}% database
    {\UniqueID=UniqueID,\DecimalPercentageComplete=DecimalPercentageComplete}% assignments
    {\DecimalPercentageComplete }% body
    }
    
  4. 创建另一个文件Report.tex并添加以下内容:

    \documentclass{report}
    
    \RequirePackage[a4paper, portrait, top=20mm, bottom=20mm, left=17.5mm, right=17.5mm]{geometry}
    
    \usepackage{datatool}
    \input{CallFromCSV.tex}
    
    %used for making a progress bar
    
    \usepackage{progressbar}
    \usepackage{graphicx}
    
    \newcommand{\PerCompleteDecimal}{0.45}
    \newcommand{\PerCompleteInt}{45}
    \newcommand{\chPercentage}{\GetDecimalPercentageCompleteCHC{1}}
    \newcommand{\chPercentageA}{\GetDecimalPercentageCompleteCHC{2}}
    
    \usepackage{numprint}
    \usepackage{siunitx}
    
    
    \begin{document}
    
    I have to use MS Project and I've created a basic export.  The table below is what you'll see in the CSV file.
    
    \begin{center}
    \begin{tabular}{ |c|c|c|c| }
    \hline
    UniqueID&Name&DecimalPercentageComplete&PercentageComplete \\ 
    \GetUniqueIDCHC{1} & \GetTaskNameCHC{1} & \GetDecimalPercentageCompleteCHC{1} & \GetPercentageCompleteCHC{1}\\ 
    \GetUniqueIDCHC{2} & \GetTaskNameCHC{2} & \GetDecimalPercentageCompleteCHC{2} & \GetPercentageCompleteCHC{2}\\
    \GetUniqueIDCHC{3} & \GetTaskNameCHC{3} & \GetDecimalPercentageCompleteCHC{3} & \GetPercentageCompleteCHC{3}\\
    \GetUniqueIDCHC{4} & \GetTaskNameCHC{4} & \GetDecimalPercentageCompleteCHC{4} & \GetPercentageCompleteCHC{4}\\ 
    \hline
    \end{tabular}
    \end{center}
    
    \section{Section 1}
    When I put the numbers into the progressbar, it works fine.
    
    \progressbar[linecolor=blue, filledcolor=green, heighta=1cm, width=5cm]{.25} \begin{picture}(0,0)\put(-73,1.5) {\large$25\%$} \end{picture}\\
    
    When I call the variables from the newcommand - \textbackslash PerCompleteDecimal and \textbackslash PerCompleteInt, it works fine.
    
    \progressbar[linecolor=blue, filledcolor=green, heighta=1cm, width=5cm]{\PerCompleteDecimal} \begin{picture}(0,0)\put(-73,1.5) {\large$\PerCompleteInt\%$} \end{picture}\\
    
    If I try and call \textbackslash GetDecimalPercentageCompleteCHC{3} or \textbackslash GetPercentageCompleteCHC{3}, it errors out.  You'll see that it errors out.  Below I should have gotten a progressbar colot that shows 10\% but that doesn't happen.
    
    \progressbar[linecolor=blue, filledcolor=green, heighta=1cm, width=5cm]{\GetDecimalPercentageCompleteCHC{3}} \begin{picture}(0,0)\put(-73,1.5) {\large$\GetPercentageCompleteCHC{3}\%$} \end{picture}\\
    
    Here's what happens when I break the progressbar down.  I've put 0.10 for the color and I put\\
     \textbackslash GetPercentageCompleteCHC{3} to show the number.
    
    \progressbar[linecolor=blue, filledcolor=green, heighta=1cm, width=5cm]{0.10} \begin{picture}(0,0)\put(-73,1.5) {\large$\GetPercentageCompleteCHC{3}\%$} \end{picture}\\
    
    Here's one last thing for you.  I can manipulate it a little and put \textbackslash PerCompleteInt from the newcommand and place it like a decimal.  Here's what it looks like.  Notice that I put it as 0.\textbackslash PerCompleteInt
    
    \progressbar[linecolor=blue, filledcolor=green, heighta=1cm, width=5cm]{0.\PerCompleteInt} \begin{picture}(0,0)\put(-73,1.5) {\large$\PerCompleteInt\%$} \end{picture}\\
    
    Bascially, I'm just trying to put a progressbar in a table for a report.  Without submitting the whole report I've given a simplified one.  We have so much of our documentation and I want to automate this as much as possible when I have to submit my monthly reports this is why I'm asking the question.
    
    
    
    \begin{center}
      \begin{tabular}{ |c|c|c| } 
        \hline
        UniqueID&Name&Progress \\ 
        \GetUniqueIDCHC{1} & \GetTaskNameCHC{1} & \progressbar[linecolor=blue, filledcolor=green]{.45} \begin{picture}(0,0)\put(-35,1.5) {\tiny$45\%$} \end{picture} \\ 
        \GetUniqueIDCHC{2} & \GetTaskNameCHC{2} & \progressbar[linecolor=blue, filledcolor=green]{.95} \begin{picture}(0,0)\put(-35,1.5) {\tiny$95\%$} \end{picture} \\
        \GetUniqueIDCHC{3} & \GetTaskNameCHC{3} & \progressbar[linecolor=blue, filledcolor=green]{0.10} \begin{picture}(0,0)\put(-35,1.5) {\tiny$10\%$} \end{picture} \\
        \GetUniqueIDCHC{4} & \GetTaskNameCHC{4} & \progressbar[linecolor=blue, filledcolor=green]{1} \begin{picture}(0,0)\put(-35,1.5) {\tiny$100\%$} \end{picture} \\
        \hline
      \end{tabular}
    \end{center}
    
    I have to call from many exported CSV files because if I call from one big CSV with about 10000 lines, it takes forever for TexStudio to compile it into a PDF.  It handles many smaller files much easier than one big file.  I don't want to use something else and then call a PDF or any other way.  The reason why I like Latex is because I have so many files and parts of files that I can call from including code like XML or C\#.  \\
    
    Could this be a bug?  I think it might be.\\
    
    Thanks\\
    
    Mike
    
    \end{document}
    

编译后Report.tex,您会注意到其中一个进度条出现错误。这就是问题所在。只需阅读整个文档,您就会明白我的意思。

我挑战任何人解释为什么这不起作用。我愿意采取另一种方法,但由于我的客户确实想要进度条,有没有另一种方法可以使用进度条并像我尝试从 CSV 文件调用变量那样使用它。

感谢任何能回答这个问题的人。

麦克风

答案1

您希望命令是可扩展的,并且您不想每次调用其中一个命令时都读取数据库。

将代码更改CallFromCSV.tex

\usepackage{datatool}
\DTLloaddb{CHCplanDB}{texDemo.CSV}

\newcommand{\definefromcsv}[2]{%
  \expandafter\xdef\csname#1@\UniqueID\endcsname{#2}%
}

\newcommand\GetUniqueIDCHC[1]{%
  \csname GetUniqueIDCHC@#1\endcsname
}
\newcommand{\GetTaskNameCHC}[1]{%
  \csname GetTaskNameCHC@#1\endcsname
}
\newcommand{\GetDecimalPercentageCompleteCHC}[1]{%
  \csname GetDecimalPercentageCompleteCHC@#1\endcsname
}
\newcommand{\GetPercentageCompleteCHC}[1]{%
  \csname GetPercentageCompleteCHC@#1\endcsname
}

\DTLforeach*{CHCplanDB}{%
  \UniqueID=UniqueID,%
  \Name=Name,%
  \DecimalPercentageComplete=DecimalPercentageComplete,%
  \PercentageComplete=PercentageComplete%
}{%
  \definefromcsv{GetUniqueIDCHC}{\UniqueID}%
  \definefromcsv{GetTaskNameCHC}{\Name}%
  \definefromcsv{GetPercentageCompleteCHC}{\PercentageComplete}%
  \definefromcsv{GetDecimalPercentageCompleteCHC}{\DecimalPercentageComplete}%
}

你就完成了。

相关内容