此问题已交叉发布到Web应用程序:Help me find my Element in a Google Doc so I can act on it in script?
这是我的Google文档: https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing
你可以看到一个标题为ENTER NEW NOTE的按钮。
我成功地浏览了doc的元素以找到表并根据需要替换这些区域中的txt。但是这里的按钮需要更改URL,我无法弄清楚如何做到这一点。
这个网址似乎有了一个想法,但我不能把它变成我的答案,因为我不太明白。 Mail merge: can't append images from template
有人会帮我这么做以显示实际的代码,因为我已经尝试编辑了很多关于元素,setURL和寻找父母等的例子。我最后弄得一团糟。
我正在通过Google表格调用脚本,以便在Google Docs的BUNCH上进行操作。 (我将浏览电子表格以获取下一个文档的URL,以便更换它的URL。
这与我相信我已经接近了:
function getDocElements() {
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing"),
body = doc.getBody(),
numElements = doc.getNumChildren(),
elements = [];
for (var i = 0; i < numElements; ++i){
var element = doc.getChild(i),
type = element.getType();
// daURL = element.getURL();
// Look for child elements within the paragraph. Inline Drawings are children.
// if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
var drawingRange = body.findElement(DocumentApp.ElementType.INLINE_DRAWING);
while (drawingRange != null) {
var element = drawingRange.getElement();
var drawingElement = element.asInlineDrawing();
//drawingElement.removeFromParent();
drawingElement.setURL("http://www.google.com");
drawingRange = body.findElement(DocumentApp.ElementType.INLINE_DRAWING);
}
// For whatever reason, drawings don't have their own methods in the InlineDrawing class. This bit copies and adds it to the bottom of the doc.
//var drawing = element.asParagraph().copy();
//body.appendParagraph(drawing);
}
Logger.log(i + " : "+type);
}
这是我最新的迭代,在日志中显示了元素,包括我想要更改的inLineDrawing ......
===========
function getDocElement() {
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing"),
body = doc.getBody(),
numElements = doc.getNumChildren(),
elements = [];
for (var i = 0; i < numElements; ++i){
var element = doc.getChild(i),
type = element.getType();
// daURL = element.getURL();
Logger.log(i + " : " + numElements + " : "+ type + " " + element);
// Search through the page elements. Paragraphs are top-level, which is why I start with those.
if( type == DocumentApp.ElementType.PARAGRAPH ){
// Look for child elements within the paragraph. Inline Drawings are children.
if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
//element.getParent().setLinkUrl("http://www.google.com");
Logger.log(element.asParagraph().getChild(0).getType() + " : " + element.getAttributes());
// For whatever reason, drawings don't have their own methods in the InlineDrawing class. This bit copies and adds it to the bottom of the doc.
var drawing = element.asParagraph().copy();
//body.appendParagraph(drawing);
// body.appendParagraph();
if(element.getParent() !=''){
//element.asParagraph().appendHorizontalRule();
//element.editAsText().appendText("text");
// element.getParent().insertHorizontalRule(0);
}
}
}
}
}
答案 0 :(得分:2)
我不确定为什么 setLinkUrl()不适用于InlineDrawing
如果您可以用图像替换图形(您可以将图形下载为png或svg并插入),您将可以使用setLinkUrl
以下是一个例子:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
// All inline images as a RangeElement
var images = body.findElement(DocumentApp.ElementType.INLINE_IMAGE);
// select first image, in case your doc has more than one you'll need to loop
var element = images.getElement();
var image = element.asInlineImage();
image.setLinkUrl("www.google.com");
}
答案 1 :(得分:0)
不幸的是,类InlineDrawing没有方法来访问附加的链接,也没有任何其他方法以编程方式将其更改为InlineImage 1 。在我看来,您必须手动更改链接。
相关功能请求:
参考文献
1 :Answer由Henrique Abreu致Modifying a drawing using Google Apps Script