传递给 Google Visualization API 时字符串太大导致的错误

发布于 2024-09-10 11:45:30 字数 1279 浏览 8 评论 0原文

我正在尝试通过 WordCloud 创建="http://code.google.com/apis/visualization/documentation/reference.html" rel="nofollow noreferrer">Google 可视化 API。您必须创建一个 google.visualization.DataTable 并将字符串添加到表中的单元格中。我基本上复制了上面链接的示例,并传入了一个由几个单词组成的简单字符串(@note.articletext),WordCloud 工作得很好。

    <script type="text/javascript">
      google.load("visualization", "1");
      google.setOnLoadCallback(draw);
      function draw() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Text1');
        data.addRows(1);
        data.setCell(0, 0, '<%= @note.articletext %>');

        var outputDiv = document.getElementById('wcdiv');
        var wc = new WordCloud(outputDiv);
        wc.draw(data, {width:300, height:300});
      }
    </script>

但我想基于一篇文章创建WordCloud,该文章(显然)是一个非常大的字符串。当我传入包含整篇文章的字符串时,出现以下错误:

unterminated string literal
data.setCell(0, 0, 'There was a da...ne ended with a time-out in his crib.

同样,当我传入小字符串时不会发生此错误,因此这是由字符串的大小引起的。或者可能 google.visualization.DataTable 对单个单元格的大小有限制(尽管我找不到这样的限制)。有谁知道我该如何解决这个问题?感谢您的阅读。

编辑:如果它很重要,我正在使用 Ruby on Rails。

I am trying to create a WordCloud through the Google Visualization API. You have to create a google.visualization.DataTable and add the string to a cell in the table. I basically copied the example linked above, and passed in a simple string of a few words (@note.articletext), and the WordCloud worked fine.

    <script type="text/javascript">
      google.load("visualization", "1");
      google.setOnLoadCallback(draw);
      function draw() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Text1');
        data.addRows(1);
        data.setCell(0, 0, '<%= @note.articletext %>');

        var outputDiv = document.getElementById('wcdiv');
        var wc = new WordCloud(outputDiv);
        wc.draw(data, {width:300, height:300});
      }
    </script>

But I want to create the WordCloud based on a article, which (obviously) is a very large string. When I pass in a string containing a whole article, I get the following error:

unterminated string literal
data.setCell(0, 0, 'There was a da...ne ended with a time-out in his crib.

Again, this error doesn't occur when I pass in a small string, so it's caused by the size of the string. Or potentially the google.visualization.DataTable has a limit on how big a single cell can be (I can't find mention of such a limit though). Does anyone know how can I fix this? Thanks for reading.

Edit: In case it's important, I'm using Ruby on Rails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-09-17 11:45:30

您添加到数据单元的文本很可能跨越多行,导致“未终止的字符串文字”错误。

尝试解析文本并将其全部放在一行中。或者逐行添加单元格。

The text you are adding to the datacell most likely spans multiple lines causing the 'unterminated string literal' error.

Try parsing the text and put it all on one line. Or add cells on a line by line basis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文