使用Google Apps脚本更新Google文档中的嵌入式图表 [英] Updating Embedded Charts in Google Docs with Google Apps Script

查看:66
本文介绍了使用Google Apps脚本更新Google文档中的嵌入式图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR;如何使用文档中的脚本编辑器来更新文档中嵌入的表格图表?

TLDR; How do I use the Script Editor in Docs to update an embedded Sheets chart in the document?

我知道有一个脚本可以对Google幻灯片执行此操作,但是我正尝试在Google文档中执行此操作,并且找不到任何文档.

I know there is a script that does this for Google Slides, but I'm trying to do it in Google Docs and can't find any documentation thereof.

https://developers.google.com/slides/how-tos/add-chart#refreshing_a_chart

具体来说,我有一个Google文档.该文档包含大约三十个表格和嵌入的图表,它们都链接到单独的Google表格.全部三十个都来自一个Google表格.现在,每当电子表格发生更改时,我可以让我们的非怪异人员单击所有三十个更新"悬停按钮,但是我希望该电子表格发生很大变化,并且我希望对文档进行白痴验证以确保其始终处于工作状态-迄今为止.据我所知,这不是Google Apps开箱即用的功能,因此我想编写一个脚本来实现.

To be specific, I have a Google Doc. This doc contains about thirty tables and embedded charts that are all linked to a separate Google Sheet. All thirty come from a single Google Sheet. Now, I can have our non-geeky people click on all thirty "Update" hover buttons every time the spreadsheet changes, but I expect the spreadsheet to change a lot, and I would like to idiot-proof the document to ensure it's always up-to-date. As far as I can tell, this isn't a feature Google Apps does out of the box, so I wanted to write a script to do it.

但是我找不到从Google文档访问EmbeddedChart的任何方法.

But I can't find any way to access an EmbeddedChart from a Google Doc.

如果我可以像在Sheets中那样运行类似的内容,我可能会弄清楚,但我做不到:

If I could run something like this like you can in Sheets, I could probably figure it out, but I can't:

var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var charts = sheet.getCharts();
    for (var i in charts) {
       var chart = charts[i];
       // Update the chart
    }
}

尽管docs具有以下功能 DocumentApp.getActiveDocument(),但是对象文档不包含功能 getCharts().我相信它们被视为图片,但是图片没有更新功能.

Although docs has the following function, DocumentApp.getActiveDocument(), an object Document doesn't contain a function getCharts(). I believe they're considered to be Images, but Images don't have an update function.

是否甚至可以使用脚本访问/更新文档中的EmbeddedChart?也许通过在编辑时通过电子表格运行脚本并从那里更新文档?似乎很奇怪,您可以在所有内容的幻灯片中进行此操作,但不能在文档中进行操作.

Is it even possible to access/update an EmbeddedChart in Docs using a script? Maybe by running the script through the spreadsheet on edit and updating the doc from there? Seems weird that you can do it in Slides of all things, but not Docs.

推荐答案

在Google Docs DOM图表中作为InlineImages出现,这里是记录图像属性的脚本.但是它们看起来是只读的,并且此方法的文档以结尾结尾:如果元素包含此属性的多个值,则为null." https://developers.google.com/apps-脚本/参考/文档/inline-image#getLinkUrl()

in Google Docs DOM charts as InlineImages here is the script that logs images attributes. but in they looks to be read only and documentation for this method ends with dead end: "null if the element contains multiple values for this attribute." https://developers.google.com/apps-script/reference/document/inline-image#getLinkUrl()

function myFunction() {
  var doc = DocumentApp.getActiveDocument()
  var body = doc.getBody()
  var pars = body.getParagraphs()
  var atts = img.getAttributes();
  // Log the paragraph attributes.
  for (var att in atts) {
    Logger.log(att + ":" + atts[att]);
  }
  
  for (var par in pars) {
    Logger.log(pars[par].getText());
    var chN = pars[par].getNumChildren()
    Logger.log(chN);
    if(chN>0){
      var type = pars[par].getChild(0).getType()
      Logger.log(type);
      if(type=="INLINE_IMAGE"){
            
        var atts = pars[par].getChild(0).getAttributes()
        Logger.log(JSON.stringify(atts));
          for (var att in atts) {
            Logger.log(att + ":" + atts[att]);
          }
      }
    
    }
  }
  return
}

这篇关于使用Google Apps脚本更新Google文档中的嵌入式图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆