Google apps script 谷歌应用程序脚本:如何复制文本和保留格式?
考虑下面3行表示的文档 …一些文本…Google apps script 谷歌应用程序脚本:如何复制文本和保留格式?,google-apps-script,string-formatting,children,deep-copy,inline-images,Google Apps Script,String Formatting,Children,Deep Copy,Inline Images,考虑下面3行表示的文档 …一些文本… 67891011 …一些文字 假设所有数字都是各自的字体大小(即10是字体大小10)。现在,我想在8和9之间的空间中插入一个内联图像,并删除该空间,但不破坏未受影响文本的格式(本例中的大小)。结果将是 …一些文本… 6 7 8“>9 10 11 …一些文字 然而,当我尝试 function placeImage() { var s = DocumentApp.getActiveDocument().getBody(); //Find in Docu
67891011
…一些文字 假设所有数字都是各自的字体大小(即10是字体大小10)。现在,我想在8和9之间的空间中插入一个内联图像,并删除该空间,但不破坏未受影响文本的格式(本例中的大小)。结果将是 …一些文本…
6 7 8“>9 10 11
…一些文字 然而,当我尝试
function placeImage() {
var s = DocumentApp.getActiveDocument().getBody();
//Find in Document
var found = s.findText("8");
if(found==null)
return 0;
var foundLocation = found.getStartOffset(); //position of image insertion
//Get all the needed variables
var textAsElement = found.getElement();
var text = textAsElement.getText();
var paragraph = textAsElement.getParent();
var childIndex = paragraph.getChildIndex(textAsElement); //gets index of found text in paragraph
//Problem part - when removing the space, destroys all formatting
var textRemaining = text.substring(foundLocation + 2);
textAsElement.deleteText(foundLocation, text.length-1);
if(textRemaining != "")
paragraph.insertText(childIndex+1, textRemaining);//destroys formatting for the rest of the child index
//Insert image
var imgSource = UrlFetchApp.fetch("https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Red_information_icon_with_gradient_background.svg/48px-Red_information_icon_with_gradient_background.svg.png");
var ingBlob = imgSource.getBlob();
paragraph.getChild(childIndex+1).insertInlineImage(foundLocation, ingBlob);
}
问题是,当我删除空格并在段落中创建另一个子元素以插入图像时,子字符串也会删除剩余的文本格式。我已尝试查看copy(),但我不确定这是否有效
我研究过许多其他地方,两个答案都没有保留格式和位置。insertInlineImage()似乎像被问到的那样被破坏了。这基本上取决于文档的内容以及如何设置段落。尝试在文档中使用简单内容并插入图像 它很好地插入了图像,也没有改变其余文本的格式 检查以下代码:
function placeImage()
{
var s = DocumentApp.getActiveDocument().getBody();
var number = '8'
//Find in Document
var found = s.findText(number);
if(found==null)
return 0;
var foundLocation = found.getStartOffset(); //position of image insertion
//Get all the needed variables
var textAsElement = found.getElement();
var text = textAsElement.asText().copy();// getText();
textAsElement.editAsText().deleteText(foundLocation + number.length ,textAsElement.asText().getText().length -1)
text.asText().editAsText().deleteText(0, foundLocation + 1 );
//Insert image
var imgSource = UrlFetchApp.fetch('https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Red_information_icon_with_gradient_background.svg/48px-Red_information_icon_with_gradient_background.svg.png');
var ingBlob = imgSource.getBlob();
var children =DocumentApp.getActiveDocument().getBody().getChild(0).asParagraph().appendInlineImage(ingBlob);
DocumentApp.getActiveDocument().getBody().getChild(0).asParagraph().appendText(text);
var child = DocumentApp.getActiveDocument().getBody().getNumChildren();
}
测试的文档内容为:
6 7 8 9 10 11
(文本的大小与您提到的类似。'8'是大小8,依此类推)
您必须根据文档内容使用试错法进行测试
希望有帮助!您从哪里获得logoResult值?它包含文档中的哪些元素?请更新并添加详细信息。哦!很抱歉,应该可以找到它。感谢您的更新。我们将尝试找到一些可能的解决方案。太好了!我在问题末尾添加了一句话,其中包含无效的解决方案(然而),也许解决其中一个问题也能帮你解决这个问题。效果很好!你能告诉我为什么使用.asText()可以保留格式,而我所做的却不能?