【问题标题】:Google Docs - create image at bookmark with Google Apps ScriptGoogle Docs - 使用 Google Apps 脚本在书签中创建图像
【发布时间】:2017-10-02 08:12:25
【问题描述】:

我在 Google 文档中创建了一个书签。我知道书签 ID。

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

 var bookmarkId = bookmark.getId();

 Logger.log(bookmarkId);  
}
  1. 我可以在书签上从 Google 云端硬盘创建图像吗?
  2. 创建映像后,我想使用基于时间的触发器定期更新它。我必须先删除图像还是可以在不删除的情况下更新它?

【问题讨论】:

  • “创建图像”是什么意思?截图?插入云端硬盘中的图片?您可以通过多种方式完成第二部分。使用 DocumentApp 的Paragraph 类中的方法。
  • 谢谢。是的,我从云端硬盘拍摄图像。我创建了几个段落。我可以用 f.e. 添加图像。 var childIndex = body.getNumChildren() - 1;body.insertImage(childIndex, image); 添加更多段落时,此 childIndex 会发生变化。如何找到特定段落的确切 childIndex?通过搜索文本字符串?它是如何工作的?
  • 我用body.getChild(0).asParagraph().clear(); body.getChild(0).asParagraph().appendInlineImage(image); 更新子索引0 上的图像 - 这是更新图片的常用方法吗?我的问题:图片在绝对位置子索引0。当另一个用户在这个位置之前添加一个段落时,下一次图片更新会将图片附加到这个位置而不是原始段落。如何获得相对子索引位置,取决于段落文本?
  • 您可以在新图像上附加一个书签并在文档的属性中停止它,以将其与其他用户的任何其他设备区分开来。
  • 您已经说服我使用段落类中的方法 - 没有书签 :) 总结。我添加了一张子索引为 0 的图片。body.getChild(0).asParagraph().clear(); body.getChild(0).asParagraph().appendInlineImage(image); 在这之后,我在它之前添加了几个段落。图片位于子索引 X,而不是现在的 0。然而,我会准确地更新这张照片。子索引未知。我怎样才能知道图片现在有什么子索引?

标签: google-apps-script google-docs


【解决方案1】:

书签 ID 没有帮助。更好的是在文档中写一个带有特定文本的段落。现在您可以搜索它并添加或更新图像。

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);
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2018-03-29
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    相关资源
    最近更新 更多