【问题标题】:Google App Script - merge multiple documents, remove all line breaks and sent as pdf by EmailGoogle App Script - 合并多个文档,删除所有换行符并通过电子邮件以 pdf 格式发送
【发布时间】:2015-05-15 23:02:33
【问题描述】:

我正在使用 Google App Script,并成功地将一个文件夹中的多个文档合并到一个文档中,并删除了所有换行符,同时保持所有样式不变。

我需要帮助的是,在 removeMultipleLineBreaks(element) 函数完成后如何通过邮件发送文档。

谁能帮我存档这个: (这是一个连接到表单的电子表格脚本,电子表格接收表单响应)

onFormSubmit 触发mergeDocument。合并所有文档后,删除MultipleLineBreaks,完成后,使用表单提交的电子邮件将文档的pdf版本发送给用户。

这是正在运行的脚本代码。

function mergeGoogleDocs() {
// set folder ID were we should look for files to merge  
  var folder = DriveApp.getFolderById('0BwqMAWnXi8hMmljM3FZpaowb1'); 
  var docIDs = [];
  var files = folder.getFiles();

  while (files.hasNext()){
    file = files.next();
    docIDs.push(file.getId());
  }
// check if we have some ids  
  Logger.log(docIDs); 
// set document id of doc which will contain all merged documents  
  var baseDoc = DocumentApp.openById('0BwqMAWnXi8hMmljM3FZpaowb1');
// clear the whole document and start with empty page
  baseDoc.getBody().clear();
  var body = baseDoc.getActiveSection();

  for (var i = 1; i < docIDs.length; ++i ) {
    var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();    
    var totalElements = otherBody.getNumChildren();
    for( var j = 0; j < totalElements; ++j ) {
      var element = otherBody.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH )
        body.appendParagraph(element);
      else if( type == DocumentApp.ElementType.TABLE )
        body.appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        body.appendListItem(element);
      else
        throw new Error("Unknown element type: "+type);
    }
  }
// after merging all docs, invoke function to remove all line breaks in the just merged document
  removeMultipleLineBreaks();
}


function removeMultipleLineBreaks(element) {
  if (!element) {
    // set document id of doc where to remove all line breaks 
    element = DocumentApp.openById('0BwqMAWnXi8hMmljM3FZpaowb1').getBody();
  }
  var parent = element.getParent();
  // Remove empty paragraphs
  if (element.getType() == DocumentApp.ElementType.PARAGRAPH 
      && element.asParagraph().getText().replace(/\s/g, '') == '') {
    if (!(parent.getType() == DocumentApp.ElementType.BODY_SECTION 
         && parent.getChildIndex(element) == parent.getNumChildren() - 1)) {
      element.removeFromParent();
    }
  // Remove duplicate newlines in text
  } else if (element.getType() == DocumentApp.ElementType.TEXT) {
    var text = element.asText();
    var content = text.getText();
    var matches;
    // Remove duplicate carriage returns within text.
    if (matches = content.match(/\r\s*\r/g)) {
      for (var i = matches.length - 1; i >= 0; i--) {
        var match = matches[i];
        var startIndex = content.lastIndexOf(match);
        var endIndexInclusive = startIndex + match.length - 1;
        text.deleteText(startIndex + 1, endIndexInclusive);
      }
    }
    // Grab the text again.
    content = text.getText();
    // Remove carriage returns at the end of the text.
    if (matches = content.match(/\r\s*$/)) {
      var match = matches[0];
      text.deleteText(content.length - match.length, content.length - 1);
    }
    // Remove carriage returns at the start of the text.
    if (matches = content.match(/^\s*\r/)) {
      var match = matches[0];
      text.deleteText(0, match.length - 1);
    }
  // Recursively look in child elements
  } else if (element.getNumChildren) {
    for (var i = element.getNumChildren() - 1; i >= 0; i--) {
      var child = element.getChild(i);
      removeMultipleLineBreaks(child);
    }
  }
}

【问题讨论】:

    标签: javascript google-apps-script merge


    【解决方案1】:

    调用邮件函数:

      }
      // after merging all docs, invoke function to remove all line breaks in the just merged document
      removeMultipleLineBreaks();
    
      //email document
      emailDocument();
    }
    

    邮件功能:

    function emailDocument() {
      //Replace this email address with your own email address
      var email = "sample@email.com"; 
    
      var fileToAttach = DriveApp.getFileById('Put your file ID here').getAs('application/pdf');
    
      var message = "This is a test message";
      var subject = "New Merged Document";
    
     // Send an email with an attachment: a file from Google Drive
     MailApp.sendEmail(email, subject, message, {
         attachments: [fileToAttach]
     });
    }
    

    【讨论】:

      【解决方案2】:

      将以下内容附加到您的 removeMultipleLineBreaks();

      \\Save and close the document.
      baseDoc.saveAndClose();
      

      调用 SendAttachment 函数。 发送附件();

      function SendAttachment(){
      \\Create the PDF
      var pdf = DriveApp.getFileByID('your file ID').getAs("application/pdf");
      
      \\Attach the PDF and send the email
      var subject = "Insert Subject here");
      var body = "Insert the HTML body here");
      
      MailApp.sendEmail({
      to: "insert primary recipient email address",
      cc: "insert cc recipient email address",
      replyTo: "insert the replyto email address",
      subject: subject, 
      htmlBody: body + "<br>",
      attachments: pdf, 
      });
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2023-03-10
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多