【问题标题】:Google Apps Script EmbeddedComboChartBuilder does not allow changing orientationGoogle Apps 脚本 EmbeddedComboChartBuilder 不允许更改方向
【发布时间】:2020-05-18 13:15:51
【问题描述】:

我正在尝试以编程方式构建垂直组合图。这可以通过将 ComboChart 的“方向”选项设置为“垂直”来完成(根据https://developers.google.com/chart/interactive/docs/gallery/combochart

如果您在上面链接的页面上的第一个图像下方打开 JSFiddle 并添加选项“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 时,设置相同的选项不会改变任何内容(结果图表处于标准水平方向)。

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 表格时此选项根本不可用?

【问题讨论】:

    标签: google-apps-script charts google-visualization orientation


    【解决方案1】:

    您无法为嵌入式电子表格图表构建器设置选项"orientation", "vertical" 的原因是 Google 表格 UI 中没有此类选项

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

    如果嵌入这样的图表对您很重要,您需要使用解决方法。

    例如将图表嵌入为图像或Modal dialog

    模态对话框示例:

    代码.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

    
    <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>
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2020-10-18
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多