将文字,图像,表格,所有格式,GDoc上的边距复制到另一个 [英] Copy text, images, tables, ALL formatting, margins from on GDoc to another
问题描述
问题在于它不会将文本格式,页边距或图像复制到Gmail或新的GDoc中,而只是纯文本。
我使用 DocumentApp.openById> getActiveSection> getText()来捕获文本。
以下是GDoc中的代码 http://goo.gl/fO5vP
我可以似乎没有共享一个脚本,所以我不得不把它放在一个文档中。将它复制到一个新的脚本中,并且它将以彩色编码。
您应该首先使用 DocsList
复制模板,一个完整的初始文档。
var template = DocsList.getFileById(docIDs [0]); //获取模板模型,在这个示例中,我有一个可能的模板数组,我拿了第一个
var newmodelName = template.substr(0,11)+'multipage'+ template.substring(18); //定义一个新名称,在这里做你所需要的...
var baseDocId = DocsList.copy(template,newmodelName).getId(); //制作头文件的副本并给它从serie中构建新的baseocname(保留边距等) ...)
var baseDoc = DocumentApp.openById(baseDocId); //这是修改
的新文件
,然后使用文档类,它具有直接 replaceText方法
编辑:关于你的第二个问题,这里是关于你的建议可以做。除了 inlineImage
之外,它很好地工作,我会继续看这个。您还可以通过添加其他元素类型来使脚本更具普遍性......
function myFunction(){
var template = DocsList.getFileById(key); //获取模板模型
var newmodelName ='testcopy'; //定义一个新名称,在这里执行你所需要的操作...
var baseDocId = DocsList。复制(template,newmodelName).getId(); //制作一个firstelement的副本并为它从系列中构建新的basedocname(保留边距等)。 //这是修改
的新文档var body = baseDoc.getActiveSection();
body.appendPageBreak();
var totalElements = body.getNumChildren(); (var j = 0; j var element = body.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 if(type == DocumentApp.ElementType.INLINE_IMAGE)
{var blob = body.getChild(j).asInlineImage()。getBlob();
body.appendImage(blob); }
}
}
编辑2 感谢 @Fausto ,这是一个完整的工作版本。嵌入式图像包含在一个段落中,所以我们不得不挖一层以获得blob ...
$ b $ pre $ function myFunction() {
var template = DocsList.getFileById(key); //获取模板模型
var newmodelName ='testcopy'; //定义一个新名称,在这里做你需要的...
var baseDocId = DocsList.copy(template,newmodelName).getId(); //创建一个firstelement的副本并为其创建新的basedocname构建(保留边距等)
var baseDoc = DocumentApp .openById(baseDocId); //这是修改
的新文档var body = baseDoc.getActiveSection();
body.appendPageBreak();
var totalElements = body.getNumChildren(); (var j = 0; j
var type = element.getType();
if(type == DocumentApp.ElementType.PARAGRAPH){
if(element.asParagraph()。getNumChildren()!= 0&& element.asParagraph()。getChild(0).getType ()== DocumentApp.ElementType.INLINE_IMAGE){
var blob = element.asParagraph()。getChild(0).asInlineImage()。getBlob();
body.appendImage(blob);
}
else body.appendParagraph(element.asParagraph());
}
else if(type == DocumentApp.ElementType.TABLE)
body.appendTable(element);
else if(type == DocumentApp.ElementType.LIST_ITEM)
body.appendListItem(element);
}
}
After trying a few mail merge scripts, I decided t write my own. My merge script runs as a separate fiIt reads a template from a GDoc, reads data from a GSpreadsheet, and merges it either into Gmails or into a new GDoc - one page / email per SS row.
The problem is that it doesn't copy text formatting, margins or images into the Gmail or new GDoc ... only the plain text.
I am using DocumentApp.openById > getActiveSection > getText() to capture the text.
Here is the code in GDoc http://goo.gl/fO5vP I can't seem to share a script so I had to put it in a doc. Copy it to a new script and it will be color coded.
You should copy the template first using DocsList
so you start with a "complete" initial document.
var template = DocsList.getFileById(docIDs[0]);// get the template model, in this sample I had an array of possible templates, I took the first one
var newmodelName=template.substr(0,11)+'multipage'+template.substring(18);// define a new name, do what you need here...
var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
then use the document class that has a direct replaceText method
EDIT : about your secondary question, here is a suggestion on how you could do. It works nicely except for inlineImage
, I'll keep looking at this. You could also make the script more universal by adding other element types...
function myFunction() {
var template = DocsList.getFileById(key);// get the template model
var newmodelName='testcopy';// define a new name, do what you need here...
var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
var body = baseDoc.getActiveSection();
body.appendPageBreak();
var totalElements = body.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = body.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 if( type == DocumentApp.ElementType.INLINE_IMAGE )
{ var blob = body.getChild(j).asInlineImage().getBlob();
body.appendImage(blob); }
}
}
Edit 2 Thanks to @Fausto, here is a fully working version. Inline images are included in a paragraph so we had to dig one level more to get the blob...
function myFunction() {
var template = DocsList.getFileById(key);// get the template model
var newmodelName='testcopy';// define a new name, do what you need here...
var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
var body = baseDoc.getActiveSection();
body.appendPageBreak();
var totalElements = body.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = body.getChild(j).copy();
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
body.appendImage(blob);
}
else body.appendParagraph(element.asParagraph());
}
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}
这篇关于将文字,图像,表格,所有格式,GDoc上的边距复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!