【问题标题】:Send multiple attachments using DriveApp.getFileById in Google Apps Script在 Google Apps 脚本中使用 DriveApp.getFileById 发送多个附件
【发布时间】:2019-01-27 15:45:12
【问题描述】:

我正在尝试运行一个脚本,该脚本从 Google 表格中获取多个文件 ID 并将它们附加到电子邮件中。我无法使用 DriveApp.getFileById 附加多个文件。

脚本运行并触发一封电子邮件,但没有附件。

function createAndSendDocument(values) {

  // Get the email address of the active user
  var email = Session.getActiveUser().getEmail();

  // Get the name of the document to use as an email subject line.
  var subject = 'New Paperwork';

    //Let's add the attachments variables
  var attachmentstring = values["Which documents do you want to attach?"];
  var array = attachmentstring.toString().split(",");
  var blobs = [];
  var x = 0;
  for (var i = 0; i < array.length; i++) {
   
    //check if even
    if (i % 2 === 0) {
        blobs[x] = array[i];
        x++;
    }
    
  }
  
  // Append a new string to the "url" variable to use as an email body.
  var body = 'your doc: ' + blobs[0] + blobs[1] + array[0] + array[1] + "length:" + array.length;

  // Send yourself an email with a link to the document.
  GmailApp.sendEmail(email, subject, body, {attachments: blobs});
}

我编辑了原始帖子以包含代码的 sn-p。这是 blob 和数组的输出。

undefinedundefinedW-4 2019.pdf1YjQqFNze8VX0L6wZ9O3Y9AJNznR8jfxqlength:4

【问题讨论】:

  • 关于你的脚本, 1.我认为你的脚本在脚本编辑器中保存时,最后一行出现语法错误。但是来自The script runs and fires off an email...,可以提供最新的脚本吗? 2.关于数组的循环过程,this thread有用吗?关于你的情况,你能提供更多信息吗? 1. array 中的所有文件 ID 的值是否正确? 2. 你想使用什么样的文件?添加信息时,您可以更新您的问题吗?我认为这些信息将帮助用户思考您的问题。
  • 感谢您的回复。我对 javascript 和应用程序脚本相当陌生,但正在学习。没有语法错误。它确实运行并发送电子邮件。即使数组确实显示长度为 4,并且我认为它们正在运行,但不确定它们为什么未定义,这些 blob 也未定义。然后,即使我没有未定义的问题,blob 也不会创建附件。用户会得到一个表单,他们可以在其中选择要附加的文档。我从中提取文件ID,然后尝试从云端硬盘中获取它们并将它们附加为PDF。还没有成功。
  • 感谢您回复和更新您的问题。我可以问你关于你的问题吗? 1. 我无法理解undefinedundefinedW-4 2019.pdf1YjQqFNze8VX0L6wZ9O3Y9AJNznR8jfxqlength:4。 2.你能提供array的值吗?当然,请删除您的个人信息。 3、如果你想从array检索偶数索引的值,这些值是文件ID,你能不能把blobs[x] = array[i]修改成blobs[x] = DriveApp.getFileById(array[i]).getBlob()再试一次?
  • blob[x] = DriveApp.getFileById(array[i]).getBlob() 成功了。有用!谢谢!
  • 感谢您的回复。我很高兴你的问题得到了解决。您可以发布正确的脚本作为答案吗?这样,其他用户可以将您的问题视为已解决的问题。

标签: email google-apps-script gmail


【解决方案1】:

此代码有效:

function createAndSendDocument(values) {

// Get the email address of the active user
  var email = Session.getActiveUser().getEmail();

  // email subject line.
  var subject = 'New Paperwork';

    // add the attachments variables
  var attachmentstring = values["Which documents do you want to attach?"];
  var array = attachmentstring.toString().split(",");
  var blobs = [];
  var x = 0;
  for (var i = 0; i < array.length; i++) {
   
    //check if even
    if (i % 2 === 0) {

    }
    else 
    {
        blobs[x] = DriveApp.getFileById(array[i]).getBlob();
        x++;
    }
    
  }
  
  // an email body.
  var body = 'Hello';

  // Send an email with attachments
  GmailApp.sendEmail(email, subject, body, {attachments: blobs});
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2019-10-28
    • 2016-05-05
    相关资源
    最近更新 更多