使用带有字母类别的数据工具进行邮件合并

使用带有字母类别的数据工具进行邮件合并

我正在尝试适应这一点关于如何进行邮件合并的问题/答案与文档类一起工作letter

链接的答案对我来说很有效,但是当我尝试将文档类更改为时,letter我不断收到一个error| Argument on \@tempc has an extra },但似乎我的括号是平衡的。letterdatatool不兼容吗?这是我的示例。我如何才能更好地理解这个错误?谢谢。

\documentclass{letter}
\usepackage{datatool}
\usepackage{parskip}

\signature{Richard Herron}
\address{New York, NY}

\makeatletter
% Blank/missing fields commands
% \skipblank adds \\ to filled field
% * version adds \space instead of newline
\newcommand\skipblank{\@ifstar\@spskip\@nlskip}
\newcommand\@nlskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\\}}
\newcommand\@spskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\space}}
% \checkblank replaces blank fields with ***
\newcommand\checkblank[1]{\ifthenelse{\DTLiseq{#1}{}}{***}{#1}}
\makeatother

\begin{document}

\DTLloaddb{addresses}{mail-merge-addresses.csv} 
\DTLforeach*{addresses}{%
    \addtitle=Title,
    \firstname=FirstName,
    \lastname=LastName,
    \position=Position,
    \addressi=Address1,
    \addressii=Address2,
    \addressiii=Address3,
    \city=City,
    \state=State,
    \zip=Zip,
\country=Country}
{%
    \begin{letter}{\checkblank{\addtitle}\ \firstname\ \lastname\\
        \skipblank{\position}
        \skipblank{\addressi}
        \skipblank{\addressii}
        \skipblank{\addressiii}
        \city, \state\ \zip\\
        \skipblank*{\country}
    }

    \opening{Dear \checkblank{\addtitle}\ \lastname,}

    This is the text of the letter.

    \closing{Sincerely,}

\end{letter}
}

\end{document}

编辑:我应该补充一点,我正在使用与链接答案相同的.csv 文件数据库,但如果我删除空白条目,我会得到相同的结果。

Title,  LastName,   FirstName,  Position,   Address1,   Address2,   Address3,   City,   State,  Zip,    Country
Dr.,    Smith,  James,  Chair,  Dept. of Physics,   University of Somewhere,    434 East Hall,  Cambridge,  MA, 02139,  
Mr.,    Jones,  Bill,   Director of Human Resources,    ,   University of Nowhere,  A203 King St.,  Stanford,   CA, 94305,  
,   Brown,  Sally,  ,   Firstline,  ,   3rdline,    East Lansing,   MI, ,   USA

答案1

的参数\begin{letter}被修改以提取收件人。此修改与 不兼容\ifthenelse

有一个解决方法:我们以“明确”的形式提出论点。

\documentclass{letter}
\usepackage{datatool}
\usepackage[businessenvelope]{envlab}
\makelabels

\signature{Richard Herron}
\address{New York, NY}

\makeatletter
% Blank/missing fields commands
% \skipblank adds \\ to filled field
% * version adds \space instead of newline
\newcommand\skipblank{\@ifstar\@spskip\@nlskip}
\newcommand\@nlskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\\}}
\newcommand\@spskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\space}}
% \checkblank replaces blank fields with ***
\newcommand\checkblank[1]{\ifthenelse{\DTLiseq{#1}{}}{***}{#1}}

\newtoks\address@toks
\newcommand\addto@address[1]{%
  \address@toks=\expandafter{\the\expandafter\address@toks#1}%
}
\newcommand\makeaddress{%
  \address@toks={}%
  \ifthenelse{\DTLiseq{\addtitle}{}}{\addto@address{***\ }}{\addto@address{\addtitle\ }}%
  \addto@address{\firstname\ }%
  \addto@address{\lastname}%
  \addto@address{\noexpand\\}
  \ifthenelse{\DTLiseq{\position}{}}{}{\addto@address{\position\\}}
  \ifthenelse{\DTLiseq{\addressi}{}}{}{\addto@address{\addressi\\}}
  \ifthenelse{\DTLiseq{\addressii}{}}{}{\addto@address{\addressii\\}}
  \ifthenelse{\DTLiseq{\addressiii}{}}{}{\addto@address{\addressiii\\}}
  \addto@address{\city, }\addto@address{\state\ }\addto@address{\zip}
  \ifthenelse{\DTLiseq{\country}{}}{}{\addto@address{\noexpand\\}}
  \ifthenelse{\DTLiseq{\country}{}}{}{\addto@address{\country}}
  \begingroup
  \edef\x{\endgroup\noexpand\begin{letter}{\the\address@toks}}\x
}
\makeatother

\begin{document}

\DTLloaddb{addresses}{mail-merge-addresses.csv} 
\DTLforeach*{addresses}{%
    \addtitle=Title,
    \firstname=FirstName,
    \lastname=LastName,
    \position=Position,
    \addressi=Address1,
    \addressii=Address2,
    \addressiii=Address3,
    \city=City,
    \state=State,
    \zip=Zip,
\country=Country}
{%
 \makeaddress
    \opening{Dear \checkblank{\addtitle}\ \lastname,}

    This is the text of the letter.

    \closing{Sincerely,}

\end{letter}
}

\end{document}

相关内容