Google Apps脚本EmbeddedComboChartBuilder不允许更改方向 [英] Google Apps Script EmbeddedComboChartBuilder does not allow changing orientation

查看:69
本文介绍了Google Apps脚本EmbeddedComboChartBuilder不允许更改方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式构建垂直组合图.可以通过将ComboChart的"orientation"选项设置为"vertical"来完成(根据

I am trying to build a vertical combo chart programmatically. This can be done by setting the "orientation" option of a ComboChart to "vertical" (according to https://developers.google.com/chart/interactive/docs/gallery/combochart)

如果您在上面链接的页面上的第一张图片下面打开JSFiddle,并添加选项"orientation:'vertical',",

This works fine if you open the JSFiddle below the first image on the page linked above and add the option "orientation: 'vertical',"

    var options = {
      title : 'Monthly Coffee Production by Country',
      orientation: 'vertical',
      vAxis: {title: 'Cups'},
      hAxis: {title: 'Month'},
      seriesType: 'bars',
      series: {5: {type: 'line'}}        };

但是,在Google Apps脚本中创建EmbeddedComboChart时,设置相同的选项不会更改任何内容(结果图表处于标准水平方向).

However, when creating an EmbeddedComboChart in Google Apps Script, setting the same option does not change anything (resulting chart is in standard horizontal orientation).

function createEmbeddedComboChart() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var chartDataRange = sheet.getRange(
    'Tabellenblatt1!B10:D12');

  var comboChartBuilder = sheet.newChart().asComboChart();
  var chart = comboChartBuilder
    .addRange(chartDataRange)
    .setOption("orientation", "vertical")
    .setPosition(5, 8, 0, 0)
    .build();

  sheet.insertChart(chart);  
}

我是否缺少某些东西,或者在将图表嵌入Google表格中时根本无法使用此选项?

Am I missing something or is this option simply not available when embedding the chart in google sheets?

推荐答案

无法为嵌入式电子表格图表构建器设置选项方向",垂直" 的原因是,没有Google表格用户界面中的此类选项

换句话说,使用Google表格无法创建垂直组合图.

The reason why you cannot set the option "orientation", "vertical" for the embedded Spreadsheet chart builder is that there is no such option in the Google Sheets UI

In other words, using Google Sheets you cannot create vertical Combo Charts.

如果嵌入这样的图表对您很重要,则需要使用替代方法.

If it is important for your to embed such a chart, you would need to use a workaround.

例如将图表作为图像和图像嵌入到模式对话框.

E.g. embed the chart as and image or inside a Modal dialog.

模式对话框的示例:

Code.gs

Code.gs

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Custom Menu')
  .addItem('Show chart', 'openDialog')
  .addToUi();
}
function getData() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var chartDataRange = sheet.getRange('Sheet1!B10:D12').getValues();
  var data = chartDataRange;
  Logger.log("data: " + data);
  return JSON.stringify(data);
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('index').setHeight(600).setWidth(900);//.createHtmlOutputFromFile('index'); 
  SpreadsheetApp.getUi().showModalDialog(html, 'This is the chart');
}

index.html

index.html


<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawVisualization);
      function drawVisualization() {
         google.script.run.withSuccessHandler(onSuccess).getData();
      }
      function onSuccess(data){
        data = google.visualization.arrayToDataTable(JSON.parse(data));
        var options = {
          title : 'Monthly Coffee Production by Country',
          orientation: 'vertical',
          vAxis: {title: 'Cups'},
          hAxis: {title: 'Month'},
          seriesType: 'bars',
          series: {1: {type: 'line'}}        };

        var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
        chart.draw(data, options);
        }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

这篇关于Google Apps脚本EmbeddedComboChartBuilder不允许更改方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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