【问题标题】:Google Sheets App Script- Add signature when sending e-mail谷歌表格应用脚本 - 发送电子邮件时添加签名
【发布时间】:2021-04-03 21:19:03
【问题描述】:

我正在编写一个脚本,它将我的 Google 表格电子表格保存为 pdf 格式,并将其作为附件通过电子邮件发送。我使用的代码有效,但我不知道如何在我发送的电子邮件中添加签名。这可能吗,如果是的话;最干净的方法是什么?这是我正在使用的代码。我知道,它不漂亮。

function displayPrompt() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getActiveSheet().getName();
var ui = SpreadsheetApp.getUi();
var result = ui.prompt("Please enter email body");

//Get the button that the user pressed.
var button = result.getSelectedButton();
var message = {
to: "someone@somewhere.com",
subject: sheetName,
  body: "Hi team,\n\nPlease find attached the quote that you requested.\n" + 
result.getResponseText() + "\nThank you,\nMe",
name: "My Name",
attachments: 
[SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]  
}
if (button === ui.Button.OK) {
Logger.log("The user clicked the [OK] button.");
Logger.log(result.getResponseText());
MailApp.sendEmail(message);
} else if (button === ui.Button.CLOSE) {
Logger.log("The user clicked the [Cancel] button.");
}

}

【问题讨论】:

  • EmailApp 未定义
  • 对不起,我更正了。我复制并粘贴了我的代码的早期版本。就像我说的,我的代码有效,它向我发送了电子邮件,但我不知道如何添加签名。

标签: email google-apps-script google-sheets


【解决方案1】:

我相信你的目标如下。

  • 您想使用MailApp.sendEmail将签名添加到Gmail的邮件中

在这种情况下,我认为有两种模式。

模式一:

在此模式中,通过将签名声明为变量,添加签名。

示例脚本:

请在您的脚本中修改message,如下所示。

var signature = "\nsample signature";  // Added
var message = {
  to: "someone@somewhere.com",
  subject: sheetName,
  body: "Hi team,\n\nPlease find attached the quote that you requested.\n" + result.getResponseText() + "\nThank you,\nMe" + signature,  // Modified
  name: "My Name",
  attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]
}

模式 2:

在此模式中,通过检索 Gmail 的签名,添加签名。

示例脚本:

在此示例中,使用了 Gmail API。所以,在你使用这个脚本之前,please enable Gmail API at Advanced Google services。并且,请在您的脚本中修改message,如下所示。

var signature = "<br>" + Gmail.Users.Settings.SendAs.list("me").sendAs[0].signature;  // Added
var message = {
  to: "someone@somewhere.com",
  subject: sheetName,
  htmlBody: ("Hi team,\n\nPlease find attached the quote that you requested.\n" + result.getResponseText() + "\nThank you,\nMe").replace(/\n/g, "<br>") + signature, // Added
  name: "My Name",
  attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]
}
  • 从 Gmail 检索到的签名是 HTML。所以在这个模式中,htmlBody 被用来代替body

参考资料:

【讨论】:

  • Gmail API 已启用,但仍未添加签名。这是我添加的代码。也许我有语法错误,但没有出现错误并且电子邮件仍在发送。 var signature = "
    " + Gmail.Users.Settings.SendAs.list("me").sendAs[0].signature; result.getResponseText() + "\n谢谢,\n我".replace(/\n/g, "
    ") + 签名,
  • @Geo 感谢您的回复。我带来的不便表示歉意。在你的情况下,我可以问你console.log(Gmail.Users.Settings.SendAs.list("me").sendAs.map(e =&gt; e.signature))的价值吗?如果这不是值,则认为在您的 Gmail 中没有设置签名。届时请确认并在Gmail的设置中添加签名并再次测试。
  • 好的,我检查了它,它显示的是一个空字符串。原来在 Gmail 中有一个未选中的选项,所以感谢您的帮助,非常感谢!如果我想调整签名中的图像大小,或者可能是居中、粗体或调整签名中包含的某些文本的大小,我该怎么做?再次...非常感谢您的回答。
  • @Geo 感谢您的回复。我很高兴你的问题得到了解决。我愿意支持你。但是你的回复请求是新问题,和你的问题不同。那么您可以通过包含详细信息将其作为新问题发布吗?因为当您的初始问题被评论更改时,看到您的问题的其他用户会感到困惑。通过将其发布为新问题,包括我在内的用户都可以想到它。如果你能合作解决你的新问题,我很高兴。你能合作解决你的新问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 2020-04-22
  • 2013-11-15
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多