访问google.visualization.arrayToDataTable以构建图表

时间:2016-11-25 15:38:37

标签: javascript google-apps-script

我在这里有点新手,我试图在GoogleSheets中添加一个将数组转换为图表的菜单项。我已经在某处阅读过您可以使用google.visualization.arrayToDataTable命令,但我的脚本构建器无法识别它。理想情况下,它应该是:

function menuItem1() { 


    var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getActiveSheet();
     var data = google.visualization.arrayToDataTable([
              ['Country', 'Popularity'],
              ['Germany', 200],
              ['United States', 300],
              ['Brazil', 400],
              ['Canada', 500],
              ['France', 600],
              ['RU', 700]
            ]);

     sheet.insertChart(chart);

    }

我一直收到这个错误:

TypeError: Cannot find function arrayToDataTable in object [object Object].

感谢您的帮助(如果这是一个愚蠢的问题,请抱歉!)

1 个答案:

答案 0 :(得分:0)

这是使用Google表格作为来源的ChartWrapper课程,其中有详细信息。为了获得gid,您必须执行一项重要程序。 gid是正确识别特定工作表所需的queryString。之前,它可以通过使用小工具获得,但它们自2013年以来已经退役。谷歌将这一重要部分从AFAIK文件中删除(当然,如果有文档或更好的方法,@ WhiteHat会知道)。无论如何我做了一点slideshow

PLUNKER FIDDLE CODEPEN JSBIN
Query/GID Guide



<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
  <title>Basic GVis ChartWrapper Setup</title>
  <style></style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <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(drawChart);

    var options = {
      title: 'Google Visualization ChartWrapper Setup',
      titleTextStyle: {
        color: 'blue',
        fontName: 'Arial',
        fontSize: 22
      },
      hAxis: {
        textStyle: {
          color: '#993300'
        },
        title: 'Subjects',
        titleTextStyle: {
          color: '#993300',
          fontName: 'Verdana',
          fontSize: 22
        }
      },
      vAxis: {
        maxValue: 1000,
        textStyle: {
          fontName: 'Verdana',
          color: '#993300'
        },
        title: 'A Quick Basic ChartWrapper Setup from a Remote Google Sheet Source',
        titleTextStyle: {
          color: 'blue',
          fontName: 'Arial',
          fontSize: 16
        }

      }
    };

    function drawChart() {
      chart = new google.visualization.ChartWrapper();
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //Place your URL within setDataSourceUrl(...HERE...)

      chart.setDataSourceUrl('https://docs.google.com/spreadsheets/d/1_ljk07nqJf_A5tM5BJyVCHTc5Uw8jxBqdCDudJJgvmA/edit#gid=1248768532');

      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      chart.setChartType('LineChart');
      chart.setContainerId('chart');
      chart.setOptions(options);
      chart.draw();
    }
  </script>
</head>

<body>
  <header>
    <details>
      <summary>
        <p><a href='http://embed.plnkr.co/GKvadm3yOBYHJoOjisWs/'>Snippet</a> and <a href='http://plnkr.co/edit/GKvadm3yOBYHJoOjisWs?p=info'>README.md</a> file available at: Plunker.</p>
        <p><a href='https://developers.google.com/chart/interactive/docs/reference#chartwrapperobject'>ChartWrapper Reference</a>
        </p>
      </summary>
    </details>
  </header>
  <section id="chart"></section>

</body>

</html>
&#13;
&#13;
&#13;