需要工具提示:将Google表格现有数据更改为DataTable

时间:2018-12-04 03:41:04

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

问题: 我看到的所有文档都使用DataTable,该DataTable在脚本本身中写入数据。我需要从现有行中调用此工具提示数据。 我需要了解HTML页面和Google工作表中的嵌入式图表之间的代码差异。

目标: 我有一个散点图,需要自定义工具提示。我需要将Q列中的数据显示为工具提示,将鼠标悬停在R和S列中的数据上 使用此代码和工具提示插入图表:

    function newChart() {
  // Generate a chart representing the data in the range of A1:B15.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[11];

  var chart = sheet.newChart()
     .setChartType(Charts.ChartType.SCATTER)
     .addRange(sheet.getRange('R3:S5000'))
     .addRange(sheet.getRange('Q3:Q5000'))

     .setPosition(5, 5, 0, 0)
     .build();

  sheet.insertChart(chart);
}

-或-

 function drawChart() {
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn('string', 'Year');
        dataTable.addColumn('number', 'Sales');
        // A column for custom tooltip content
        dataTable.addColumn({type: 'string', role: 'tooltip'});
        dataTable.addRows([
          ['2010', 600,'$600K in our first year!'],
          ['2011', 1500, 'Sunspot activity made this our best year ever!'],
          ['2012', 800, '$800K in 2012.'],
          ['2013', 1000, '$1M in sales last year.']
        ]);

        var options = {
          tooltip: {isHtml: true},
          legend: 'none'
        };
        var chart = new google.visualization.ColumnChart(document.getElementById('col_chart_html_tooltip'));
        chart.draw(dataTable, options);
      }

参考: https://developers.google.com/chart/interactive/docs/reference#DataView https://developers.google.com/chart/interactive/docs/customizing_tooltip_content#tooltip-actions https://developers.google.com/chart/interactive/docs/spreadsheets

1 个答案:

答案 0 :(得分:1)

在嵌入式图表中,使用数据视图定义添加工具提示角色...

请参阅以下代码段...

function newChart() {
  // Generate a chart representing the data in the range of A1:B15.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[11];

  // build the data view definition
  var COLUMN_SPEC = [
    0,
    1,
    {sourceColumn: 2, role: 'tooltip'}
  ];
  var viewSpec = Charts.newDataViewDefinition()
     .setColumns(COLUMN_SPEC)
     .build();

  var chart = sheet.newChart()
     .setChartType(Charts.ChartType.SCATTER)
     .setDataViewDefinition(viewSpec)
     .addRange(sheet.getRange('R3:S5000'))
     .addRange(sheet.getRange('Q3:Q5000'))

     .setPosition(5, 5, 0, 0)
     .build();

  sheet.insertChart(chart);
}