【问题标题】:Properly attaching pictures to an email with google scripts使用谷歌脚本将图片正确附加到电子邮件
【发布时间】:2020-06-08 19:19:43
【问题描述】:

我正在尝试使用谷歌表单和表格制作一个脚本,以帮助自动化和跟踪我们技术人员在工地上的照片。

设置是他们拍摄工作现场的照片,并用信息填写谷歌表格并将照片附在那里。提交表单后,它会运行此脚本以将电子邮件发送到办公室中每个人都可以看到的预定电子邮件。

到目前为止,除了图片之外,我还可以通过电子邮件发送表格中的信息。

所附图片的信息以驱动器 url 的形式出现,该驱动器 url 全部作为字符串转储到一个单元格中。

"https://drive.google.com/open?id=xxxxxxxx, https://drive.google.com/open?id=yyyyyyyy, https://drive.google.com/open?id=zzzzzzzz"

我使用.split(" ,) 将这个字符串转换为一个数组,然后输出这个。

[https://drive.google.com/open?id=xxxxxxxx, https://drive.google.com/open?id=yyyyyyyy, https://drive.google.com/open?id=zzzzzzzz]

然后我遍历数组并使用.slice(33) 删除 url,所以我只剩下驱动器 ID(可能有更好的方法,但现在可以使用)。

[xxxxxxxx, yyyyyyyy, zzzzzzzz]

这是我遇到麻烦的部分。 然后我通过该数组迭代 agian 并获取 driveID 并将文件作为 JPEG 获取。 然后我使用.push 将其放入另一个数组中,用于将它们附加到电子邮件中。

问题是我认为我没有正确执行此步骤,因为我没有将正确的东西推入数组和/或假设MailApp.sendEmail 甚至可以将数组用于附件。 我也不完全确定[Blobs] 是如何工作的以及如何正确使用它们,这可能就是我遇到困难的地方。

再一次,这是代码的编写经验非常少,可能会进一步优化,但目前,我只需要让它正确地附上图片以显示它的工作原理。

function onFormSubmit(e) {
  //for testing purposes
  var values = e.namedValues;
  //gets the form's values
  var pureValues = e.values;

  //sets the values
  var email = pureValues[1];
  var woNum = pureValues[2];
  var firstN = pureValues[3];
  var lastN = pureValues[4];
  var desc = pureValues[5];
  var superDuperRawPics = pureValues[6];

  //splits the picture urls into an array
  var superRawPics = superDuperRawPics.split(", ");

  //slices the url part off to get the drive ID
  var i, rawPics =[]
  for (i = 0; i < superRawPics.length; ++i) {
    rawPics.push(superRawPics[i].slice(33))
       }

  //takes the array of ID's and gets the drive file
  var j, picAttach =[]
  for (j = 0; j < rawPics.length; ++j) {
    var driveID = DriveApp.getFileById(rawPics[j]);
    var drivePic = driveID.getAs(MimeType.JPEG);
    picAttach.push(drivePic);
  }

  //sets the subject of the email to be Jobsite Pictures and the work number
  var subject = "Jobsite Pictures" + " " + woNum;

  //sets the body of the email
  var body = "Technician: " + email + " \n" + 
             "WO#: " + woNum + " \n" + 
             "Customer: " + firstN + " " + lastN + " \n" + 
             "Description: " + desc;

  //for checking if the vars are set correctly
  Logger.log(superDuperRawPics);
  Logger.log(superRawPics);
  Logger.log(rawPics);
  Logger.log(picAttach);
  Logger.log(subject);
  Logger.log(body);

  //sends email to me with the new info
  MailApp.sendEmail('example@domian.com', subject, body, {attachments: [picAttach]});


}

【问题讨论】:

    标签: email google-apps-script google-drive-api blob email-attachments


    【解决方案1】:

    如果您只想附加它们,请使用options attachments

    【讨论】:

    • 最后一行没有把图片附加到邮件里吗? MailApp.sendEmail('alex@laconiares.com', subject, body, {attachments: [picAttach]});
    • 应该是 {attachments: picAttach} 来自表单...urls?
    【解决方案2】:

    我很笨,在 attachments: 不需要括号时添加了括号。

    正确的做法是这样的。

    MailApp.sendEmail('example@domian.com', subject, body, {attachments: picAttach});

    更改此设置让脚本发送带有图片的电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-04
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多