使用Google Apps脚本将图表从Google表格正确插入文档 [英] correctly insert chart from Google Sheet into Document using Google Apps Script

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

问题描述

我想使用Google应用程序脚本将电子表格中的图表复制到文档中。
插入图表工作,但a)权限和b)格式有问题。



图表可插入如下:

  var b = d.getBody(); 
var charts = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(charts)。getCharts();
for(var i in charts){
b.appendImage(charts [i]);
Logger.log(charts [i] .getBlob()。getName()); //正确:chart.png
}




  • 当我手动复制图表并将其粘贴到文档中时,格式与电子表格完全相同。
  • 当我使用脚本进行复制时,格式设置看起来很奇怪,甚至导致错误(对于日期在x轴上的折线图,它显示的图像包含文本轴#1的数据列不能是字符串类型


顶行是手动复制的,底行是脚本的结果。



第二个相关?问题:电子表格需要共享(链接的任何人都可以查看),否则会显示图像(正确的名称,宽度,高度等)为错误消息:用户不是登录帐户。登录



任何建议如何以正确的格式插入图表并且不必共享电子表格?

解决方案

Stefan指出我正确的方向(谢谢!)。
在粘贴/保存图像时,渲染看起来完全不同。
不同的轴设置,颜色,主题等等。



我最终手动创建了图表。例如。对于第一个条形图,以下结果显示在电子表格和文档中相同的图表:

  var chart = dest.newChart(); 
图表
.setChartType(Charts.ChartType.BAR)
.addRange(myrange)
.setPosition(2,8,0,0)
.setOption(主题,最大化)
.setOption(colors,[#3366CC,#FF9900,#DC3912,#109618])
.setOption('isStacked' ,true)
.setOption('width',500)
.setOption('height',130)
.setOption('hAxis.viewWindow.max',countActions)
.setOption('vAxis.gridlines.count',0)
.setOption('legend',{position:'in',textStyle:{fontSize:12}})

var chart = chart.build();
dest.insertChart(图表);

我也必须更改数据范围,因为它出现以下图表设置不能手动设置。
$ b


  • 切换行/列

  • 使用第一行作为标题

  • 为了解决这个问题,我手动调换了电子表格中的范围( = TRANSPOSE(original_range))并让 myrange 指向新的范围,并且我添加了一个空列,以便将其用作空头。



    我还没有看过折线图,可能是Chart API无法像Spreadsheet那样创建时间线。


    I want to copy charts from my spreadsheet to a document using Google apps script. Inserting the charts works, but there is an issue with a) permissions and b) formatting.

    Charts can be inserted as follows:

    var b = d.getBody();
    var charts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("charts").getCharts();
    for (var i in charts) {
      b.appendImage(charts[i]);
      Logger.log(charts[i].getBlob().getName()); // correct: "chart.png"
    }
    

    • When I manually copy the chart and paste it in the document, the formatting is exactly as in the spreadsheet.
    • when I copy using the script, formatting looks very strange or even causes an error (for the line chart with dates on the x-axis it shows an image with the text Data column(s) for axis #1 cannot be of type string)

    Top row is manually copied, bottom row is the result of the script.

    The second -related?- issue: the spreadsheet needs to be shared ("anyone with the link can view"), otherwise it shows the image (correct name, width, height, etc.) as an error message: User not signed-in. Sign in.

    Any suggestions how to insert the chart with the correct formatting and without having to share the spreadsheet?

    解决方案

    Stefan pointed me in the right direction (thanks!). It appears the rendering is done quite differently when pasting/saving images. Different axis settings, colors, theme, etc.

    I ended up manually creating the chart. E.g. for the first bar chart, the following results in a chart that appears the same in the spreadsheet and in the document:

    var chart = dest.newChart();
    chart
      .setChartType(Charts.ChartType.BAR)
      .addRange(myrange)  
      .setPosition(2,8,0,0)
      .setOption("theme","maximized")
      .setOption("colors",["#3366CC","#FF9900","#DC3912","#109618"])
      .setOption('isStacked', true)
      .setOption('width', 500)
      .setOption('height', 130)
      .setOption('hAxis.viewWindow.max', countActions)
      .setOption('vAxis.gridlines.count', 0)
      .setOption('legend', {position: 'in', textStyle: {fontSize: 12}})
    
    var chart = chart.build();
    dest.insertChart(chart);
    

    I also had to change the data range, as it appears the following chart settings can not be set manually.

    • switch rows/columns
    • use first row as header

    To solve that I have manually transposed the range in the spreadsheet (=TRANSPOSE(original_range)) and let myrange point to that new range, and I have added an empty column so that one is used as an empty header.

    I have not looked at the line chart yet, it could be the Chart API can not create a timeline in the same way as the Spreadsheet does.

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

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