replaceText Google Apps 脚本将空白值传递给 Google Doc Merge
replaceText Google Apps Script pass through blank values to Google Doc Merge
我正在做类似邮件合并的事情——从 google sheets(实际上是脚本数组)中获取值并替换 google 文档模板中的占位符变量。我有大约 50 个这样的。以下是其中的三个。格式如下所示:
body.replaceText("{Property Address}", propertyAddress);
body.replaceText("{Lead Type}", leadType);
body.replaceText("{Deal Type}", dealType);
我 运行 遇到的问题是有时变量可以为空 - 例如假设 leadType
为空或 ""
。如果它是空白的,则会发生错误。我想要做的是,如果 leadType
有一个值,则用该值替换 Google Doc 中的 {Lead Type}
。否则,如果 leadType
= null
或 ""
或 undefined
等,则将 Google 文档中的 {Lead Type}
替换为 ""
。我宁愿不必为这 50 行中的每一行都执行 if/then 语句。有没有一种简单的方法可以将值或空白推送到 sheet (我想要一个空白,因为我不想用 {} 变量打印最终的 sheet )
使用 OR 运算符怎么样?
body.replaceText("{Property Address}", propertyAddress || "");
body.replaceText("{Lead Type}", leadType || "");
body.replaceText("{Deal Type}", dealType || "");
我正在做类似邮件合并的事情——从 google sheets(实际上是脚本数组)中获取值并替换 google 文档模板中的占位符变量。我有大约 50 个这样的。以下是其中的三个。格式如下所示:
body.replaceText("{Property Address}", propertyAddress);
body.replaceText("{Lead Type}", leadType);
body.replaceText("{Deal Type}", dealType);
我 运行 遇到的问题是有时变量可以为空 - 例如假设 leadType
为空或 ""
。如果它是空白的,则会发生错误。我想要做的是,如果 leadType
有一个值,则用该值替换 Google Doc 中的 {Lead Type}
。否则,如果 leadType
= null
或 ""
或 undefined
等,则将 Google 文档中的 {Lead Type}
替换为 ""
。我宁愿不必为这 50 行中的每一行都执行 if/then 语句。有没有一种简单的方法可以将值或空白推送到 sheet (我想要一个空白,因为我不想用 {} 变量打印最终的 sheet )
使用 OR 运算符怎么样?
body.replaceText("{Property Address}", propertyAddress || "");
body.replaceText("{Lead Type}", leadType || "");
body.replaceText("{Deal Type}", dealType || "");