Gmail应用程序脚本:基于Google Drive URL的附件

发布于 2025-02-13 21:54:28 字数 808 浏览 3 评论 0原文

我正在使用我在此处找到的邮件合并脚本,但是我很难找到适合附加文件的类(第14和15行)。

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:B2
  var dataRange = sheet.getRange(startRow, 1, numRows, 1)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[3];  // First column
    var message = row[10];       // Second column
    var subject = row[6];
    var attachment = Attachment.setUrl(row[11]);
    MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});
  }
}

我已经尝试了几种附件功能的变体,但是每次都遇到了错误。行对应于Google表中的列,而行[11]是Google Drive中图像的URL。

I am using a mail merger script I found here, but I'm having trouble finding the right class to attach the file (rows 14 and 15).

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:B2
  var dataRange = sheet.getRange(startRow, 1, numRows, 1)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[3];  // First column
    var message = row[10];       // Second column
    var subject = row[6];
    var attachment = Attachment.setUrl(row[11]);
    MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});
  }
}

I've tried a handful of variations for the attachment function, but I've gotten an error each time. The rows correspond to columns in a Google Sheet, and row[11] is the URL for an image in google drive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2025-02-20 21:54:28

这是您脚本的一个工作示例:您有2个错误:

  1. 范围定义只有一个列,
  2. 附件的参数

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:B12 since you use row[11], the data must at least be 12 cells wide
  var dataRange = sheet.getRange(startRow, 1, numRows, 12);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[3];  // Fourth column
    var message = row[10];       // elevenths column
    var subject = row[6];
    var attachment = [UrlFetchApp.fetch(row[11]).getBlob()];
    MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});
  }
}

也应该是blobs的数组,即url必须有效,并且文件必须公开共享:示例:示例:

https://drive.google.com/uc?export=view&id=0B3qSFd3iikE3TUFFLTc5MDE0MzkwLWQxYWItNDkwNy05ZjVkLWIyZDhiZDM4MjdmMg

here is a working example of your script : you had 2 errors :

  1. the range definition had only one column
  2. the argument of attachment should be an array of blobs

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:B12 since you use row[11], the data must at least be 12 cells wide
  var dataRange = sheet.getRange(startRow, 1, numRows, 12);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[3];  // Fourth column
    var message = row[10];       // elevenths column
    var subject = row[6];
    var attachment = [UrlFetchApp.fetch(row[11]).getBlob()];
    MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});
  }
}

note also that url must be valid and that the file must be publicly shared : example :

https://drive.google.com/uc?export=view&id=0B3qSFd3iikE3TUFFLTc5MDE0MzkwLWQxYWItNDkwNy05ZjVkLWIyZDhiZDM4MjdmMg
┾廆蒐ゝ 2025-02-20 21:54:28

与Serge Insas的答案类似,这是我必须要做的才能绕过401错误时使用urlfetchapp.fetch

var fileUrl = row[11] // Based on OP's case

var attachment = [
   UrlFetchApp.fetch(fileUrl, {
      headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
   }).getBlob(),
]

MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});

Similar to Serge insas's answer, this is what I had to do in order to bypass the 401 error when using UrlFetchApp.fetch

var fileUrl = row[11] // Based on OP's case

var attachment = [
   UrlFetchApp.fetch(fileUrl, {
      headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
   }).getBlob(),
]

MailApp.sendEmail(emailAddress, subject, message, {attachments: attachment});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文