gpt4 book ai didi

google-apps-script - 我可以将一个文档的内容复制到另一个文档的特定位置吗? (谷歌应用程序脚本)

转载 作者:行者123 更新时间:2023-12-05 06:32:30 25 4
gpt4 key购买 nike

我有一个文档,上面写着“在此处粘贴内容”,我想将另一个文档的内容粘贴(插入、追加,无论需要什么)到那个位置,同时保留原始文档的格式。

我修改了一些规范代码来进行复制,我将其粘贴在这里以防有帮助。

我在想我可能需要拆分原始文档,然后使用我的代码创建一个包含以下内容的文档

原作的第 1 部分东西原作的第 2 部分

这很丑陋,所以我希望有人知道更好的方法!

 function copyFromTo(fromDocID,toDocID) {
var toDoc = DocumentApp.openById(toDocID);
var body = toDoc.getBody();


var fromDocBody = DocumentApp.openById(fromDocID).getBody();
Logger.log(fromDocBody.getAttributes());
var totalElements = fromDocBody.getNumChildren();
var latestElement;
for( var j = 0; j < totalElements; ++j ) {
var element = fromDocBody.getChild(j).copy();
var attributes = fromDocBody.getChild(j).getAttributes();
// Log attributes for comparison
Logger.log(attributes);
Logger.log(element.getAttributes());
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var pictattr = element.asParagraph().getChild(0).asInlineImage().getAttributes();
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
// Image attributes, e.g. size, do not survive the copy, and need to be applied separately
latestElement = body.appendImage(blob);
latestElement.setAttributes(clean(pictattr));
}
else latestElement = body.appendParagraph(element);
}
else if( type == DocumentApp.ElementType.TABLE )
latestElement = body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
latestElement = body.appendListItem(element);
else
throw new Error("Unsupported element type: "+type);
// If you find that element attributes are not coming through, uncomment the following
// line to explicitly copy the element attributes from the original doc.
//latestElement.setAttributes(clean(attributes));
}
}



/**
* Remove null attributes in style object, obtained by call to
* .getAttributes().
* https://code.google.com/p/google-apps-script-issues/issues/detail?id=2899
*/
function clean(style) {
for (var attr in style) {
if (style[attr] == null) delete style[attr];
}
return style;
}

最佳答案

为此,您需要遍历“toDoc”的每一段,直到找到所需文本为止。然后找出哪个元素是父元素(它可以是正文或表格)以及要替换的段落的相对索引。

/*This function receives the paragraph element that will replace the 
placeholder text 'Paste STUFF here' and uses the current ActiveDocument as the destination*/
function copyIntoPaceholder(copiedParagraph) {
var toDoc = DocumentApp.getActiveDocument();
var body = toDoc.getBody();
var paragraphs = body.getParagraphs();
var paragraph;
for(var p = 0; p <paragraphs.length; p++) { //iterate over every paragraph
paragraph = paragraphs[p];
if(paragraph.getText() === "Paste STUFF here") { //finds the text to be replaced
var container = paragraph.getParent(); //finds the parent of the paragraph to be replaced
var paragraphIndex = container.getChildIndex(paragraph) //gets the index of the paragraph to be replaced in its parent.
paragraph.removeFromParent() //deletes the placeholder paragraph from the document
container.insertParagraph(paragraphIndex, copiedParagraph) //insert the new paragraph at the desired location
break
}
}
}

关于google-apps-script - 我可以将一个文档的内容复制到另一个文档的特定位置吗? (谷歌应用程序脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51252160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com