在Excel(或Calc)中进行字符串模板化?

时间:2019-01-28 14:07:36

标签: excel openoffice-calc libreoffice-calc

Excel或OpenOffice / LibreOffice Calc中是否存在某种字符串模板系统?基本上,我有一个引用其他单元格的文本模板,并希望产生最终的合并结果,例如:

My name is [A1]. 
I have been working at [A2] for [A3] years.
Welcome to you all.

(模板变量不必直接是单元格引用;它可以是函数参数,因此公式复制不会很麻烦。)

我了解字符串连接,但是它似乎不支持多行字符串,也不支持“ \ n”之类的C样式转义序列,这要求我将CHAR(10)链接起来,这很笨拙:

="My name is "& A1 & CHAR(10) &
"I have been working at " & A2 & " for " & A3 & " years." & CHAR(10) &
"Welcome to you all."

2 个答案:

答案 0 :(得分:0)

实际上,您可以在字符串中插入换行符(Alt + Enter)

尝试使用此公式,不要忘记为该单元格激活Wrap text

="My name is "& A1 & "
I have been working at " & A2 & " for " & A3 & " years.
Welcome to you all."

Result

答案 1 :(得分:0)

正如您在评论中指出的那样,这与SuperUser问题类似。在这种情况下,我要做的是连续替换方法:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("My name is %name. I've been working at %company for %years years. Welcome to you all.", 
    "%name", A1), 
    "%company", A2), 
    "%years", A3)

我认为,如果您(1)在命名引用中包含模板字符串,并且(2)在命名引用中也包含替换值,或者使用表,此方法将变得更加清晰

enter image description here

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(template_string,
    "%name", [@Name]),
    "%company",[@Company]),
    "%years",[@Years])