如何使用谷歌应用脚​​本复制文档的一个或多个现有页面 [英] How to copy one or more existing pages of a document using google apps script

查看:91
本文介绍了如何使用谷歌应用脚​​本复制文档的一个或多个现有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功编写了一个小脚本,它基于Google电子表格中的数据为每个字母/地址创建一个新文档,从而创建一个连续字母(对多个收件人的实际字母)。
它可以工作,但是对于大型邮件来说,这种方法有点麻烦,因为大量文档被创建并需要单独打印。



现在我会喜欢做同样的事情,但结果是在一个Google文档中包含所有字母。

是否有任何方法复制现有文档的内容并多次插入相同或任何其他文档(即通过应用程序脚本复制/粘贴) ?

解决方案

继您的评论之后,下面是我用来合并未确定数量文档的完整代码。
所有文档ID都是作为主函数参数的一个ID数组中,结果是一个新的文档,并在名称后附加了多页。如果您需要更多的解释,那么请让我知道...(注意,它只适用于包含文本和表格的文档,如果您有其他数据类型的图像,则必须在我们检查ElementType的主循环遵循相同的逻辑)




编辑:第一个代码删除,更新后我尝试了这种方法,假设您的主文档中只有段落...尝试一下,我想您可以从那里开始开发您的项目。

 函数Serialletter_Singledocument(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Datenbank);
var LastColumn = sheet.getLastColumn();

//这里您需要从url获取文档ID(例如,1oWyVMa-8fzQ4leCrn2kIk70GT5O9pqsXsT88ZjYE_z8)
var FileTemplateFileId =1Wrf2qvUTyc5tMmJIly40Z4U4sJb4-QhT5z -UfJmtQ-M//Browser.inputBox(\"ID der Serienbriefvorlage(aus Dokumentenlink kopieren):);
var doc = DocumentApp.openById(FileTemplateFileId);
var DocName = doc.getName();

var headpara ='*****';

//获取包含数据的整个表
var data = sheet.getDataRange()。getValues();

//创建模板文档的副本并将其打开
var SerialLetterID = DocsList.getFileById(FileTemplateFileId).makeCopy(DocName +Serienbrief)。getId();
var docCopy = DocumentApp.openById(SerialLetterID);
var totalParagraphs = docCopy.getBody()。getParagraphs(); //获取段元素总数
Logger.log(totalParagraphs);
var elements = [];
for(var i = 1; i< data.length; i ++){//为电子表格中的每个记录(包含替换字母中的变量的内容)做
for(var e = 0; e< totalParagraphs.length; e ++){

var element = totalParagraphs [e] .copy();
// Logger.log(element.editAsText()。getText())
for(var c = 0; c< data [0] .length; c ++){
element.replaceText (<+ data [0] [c] +>,data [i] [c]); //用实际值
替换变量(来自列标题)
elements.push(element); //将段落存储在数组中
}

for( var el = 0; el< elements.length; el ++){
var paragraph = elements [el] .copy();
docCopy.getBody()。appendParagraph(paragraph);
}
docCopy.getBody()。appendPageBreak()
}
docCopy.saveAndClose();

Browser.msgBox(Serienbrief ist erstellt。Sie finden die erstellten Dokumente in Google Drive unter Meine Ablage);
}


I have sucessfully written a small script, which creates a serial letter (physical letter to several recipients) based on data in a Google spreadsheet creating a new document for each letter/addresse. It works, but for large mailings this approach is a bit cumbersome as a large amount of documents are created and need to be printed individually.

Now i would like to do the same but as a result having all of the letters in one single Google document.

Is there any way to copy the content of an existing document and inserting it a number of times into the same or any other documents (i.e. copy/paste via apps script)?

解决方案

Following your comment, here is the full code I use to merge an undetermined number of docs in a new one. All document IDs are in an array of IDs as argument for the main function, the results is a new doc with "multi-page" appended to the name. If you need more explanation than provided by the in code comments just let me know... (note that it will work only for documents containing text and tables, if you have images ot other data type you'll have to handle that case in the main loop where we check the ElementType following the same logic)


EDIT : first code removed, following your update I tried this approach assuming you have only paragraphs in your master doc... give it a try and I guess you could start from there to developp your project.

function Serialletter_Singledocument() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Datenbank");
  var LastColumn = sheet.getLastColumn();

  //here you need to get document id from url (Example, 1oWyVMa-8fzQ4leCrn2kIk70GT5O9pqsXsT88ZjYE_z8)
  var FileTemplateFileId = "1Wrf2qvUTyc5tMmJIly40Z4U4sJb4-QhT5z-UfJmtQ-M" //Browser.inputBox("ID der Serienbriefvorlage (aus Dokumentenlink kopieren):");
  var doc = DocumentApp.openById(FileTemplateFileId);
  var DocName = doc.getName();

  var headpara=' ***** ';

  // Fetch entire table containing data
  var data = sheet.getDataRange().getValues();

  //Create copy of the template document and open it
  var SerialLetterID = DocsList.getFileById(FileTemplateFileId).makeCopy(DocName +" Serienbrief").getId();
  var docCopy = DocumentApp.openById(SerialLetterID);
  var totalParagraphs = docCopy.getBody().getParagraphs() ;// get the total number of paragraphs elements
  Logger.log(totalParagraphs);
  var elements = [];
  for ( var i = 1; i < data.length; i++) { //do for every record in the spreadsheet (containing the content to replace the variables in the letter)
    for (var e=0;e<totalParagraphs.length;e++){

      var element = totalParagraphs[e].copy();
//      Logger.log(element.editAsText().getText())
      for(var c=0;c<data[0].length;c++){     
        element.replaceText("<" +data[0][c] +">", data[i][c]); //replace variable (from column title) with actual value
      }
      elements.push(element);// store paragraphs in an array
    }

    for(var el=0;el<elements.length;el++){ 
      var paragraph = elements[el].copy();
      docCopy.getBody().appendParagraph(paragraph);
    }
  docCopy.getBody().appendPageBreak()
  }
  docCopy.saveAndClose();

  Browser.msgBox("Serienbrief ist erstellt. Sie finden die erstellten Dokumente in Google Drive unter Meine Ablage");
}

这篇关于如何使用谷歌应用脚​​本复制文档的一个或多个现有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆