简体   繁体   English

Google文件-使用Google Apps脚本在书签上创建图像

[英]Google Docs - create image at bookmark with Google Apps Script

I have created a bookmark in Google Docs. 我已经在Google文档中创建了一个书签。 I know the bookmark Id. 我知道书签ID。

function addBookmark() {
 var doc = DocumentApp.getActiveDocument();
 var cursor = doc.getCursor();
 var bookmark = doc.addBookmark(cursor);

 var bookmarkId = bookmark.getId();

 Logger.log(bookmarkId);  
}
  1. Can I create an image from Google Drive at the bookmark? 我可以在书签的Google Drive中创建图像吗?
  2. After creating the image I want to update it regularly with a time-based trigger. 创建图像后,我想使用基于时间的触发器定期对其进行更新。 Do I have to delete the image before or can I update it without deleting it? 我必须先删除图像,还是可以不删除而更新它?

The bookmark Id is not helpful. 书签ID没有帮助。 Better is to write a paragraph with specific text in the document. 最好在文档中写一个带有特定文本的段落。 Now you can search for it and add or update the image. 现在,您可以搜索它并添加或更新图像。

function updateImageAtParagraph() {
 var body = DocumentApp.getActiveDocument().getBody();

 // Get an image in Drive from its ID.
 var image = DriveApp.getFileById('...imageId...');

 var searchString = "paragraphText";  //specific text paragraph in the document

 var paragraphSearch = body.findText(searchString);

 if(paragraphSearch !== null) {
   var paragraph = paragraphSearch.getElement().getParent().asParagraph();
   paragraph.clear().appendText(searchString);
   paragraph.appendInlineImage(image);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM