本文介绍了Google App脚本-合并多个文档,删除所有换行符,然后通过电子邮件以pdf格式发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am using Google App Script and have successfully managed to merge multiple documents from one folder into one document and remove all line breaks while keeping all styling intact.

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

Where i need some help is, how to send the document by mail, after the removeMultipleLineBreaks(element) function has finished.

有人可以帮我存档吗:(这是连接到表单的电子表格脚本,电子表格接收表单响应)

Can someone help me to archive this:(This is a Spreadsheet Script connected to a Form, Spreadsheet receives Form responses)

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

onFormSubmit trigger the mergeDocument. After merging all documents, removeMultipleLineBreaks and when finished, use the submitted Email from form to send a pdf version of the document to the user.

这是有效的脚本代码.

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

推荐答案

调用电子邮件功能:

  }
  // 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 = "[email protected]"; 

  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]
 });
}

这篇关于Google App脚本-合并多个文档,删除所有换行符,然后通过电子邮件以pdf格式发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:35