Google App脚本 - 合并多个文档,删除所有换行符并通过电子邮件发送为pdf

时间:2015-03-13 12:47:57

标签: javascript google-apps-script merge

使用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);
    }
  }
}

2 个答案:

答案 0 :(得分: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]
 });
}

答案 1 :(得分:0)

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

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

调用SendAttachment函数。     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, 
});
}