gpt4 book ai didi

google-apps-script - 谷歌应用程序脚本 : How to copy text and preserve formatting?

转载 作者:行者123 更新时间:2023-12-01 15:00:56 26 4
gpt4 key购买 nike

考虑下 3 行表示的文档。

..一些文字..
6 7 8 9 10 11
..一些文字..

假设所有数字都是它们各自的字体大小(即 10 是字体大小 10)。现在我想在 8 和 9 之间的空间中插入一个内联图像,并删除该空间,但不破坏未受影响文本的格式(本例中的大小)。结果会是

..一些文字..
6 7 8 <some image here> 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(),但我不确定它如何有效地工作。

我研究了很多其他地方,都是答案here不保留格式,并且 position.insertInlineImage() 似乎已损坏,如所问here .

最佳答案

这基本上取决于您的文档内容如何设置段落。尝试在文档中使用简单的内容并插入图像。

它可以很好地插入图像,也不会改变其余文本的格式。

检查下面的代码:

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 等等)

您必须根据您的文档内容使用试错法进行测试。

希望对您有所帮助!

关于google-apps-script - 谷歌应用程序脚本 : How to copy text and preserve formatting?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31495697/

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