本文介绍了Google Apps Script Chart 使用第 1 行作为标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图表:

var ss = ... // get sheet
var chart = ss.newChart().asLineChart();
chart.setTitle("My Title");
chart.addRange(dataSheet.getRange(1, 1, ss.getLastRow(), 4));
chart.setColors(["#3c78d8", "#a61c00", "#38761d"]);
chart.setXAxisTitle("X Title");
chart.setYAxisTitle("Y Title");
chart.setPosition(1, 1, 0, 0);
chart.setCurveStyle(Charts.CurveStyle.SMOOTH);
ss.insertChart(chart.build());

此代码将使用显示第一行作为数据的一部分,而不是使用它来标记图例.附带说明一下,如果我使用 asColumnChart 而不是折线图,它会做正确的事情.

This code will use the show the first row as part of the data, instead of using it to label the legend. As a side note, if I use asColumnChart instead of line chart, it does the right thing.

如何使用 Google Apps 脚本告诉图表将第一行用作标题?

How can I tell a chart to use the first row as headers using Google Apps Script?

推荐答案

首先 - 你的代码没问题.我拿了一份副本并对其进行了细微的更改,只是为了使用一张纸进行说明:

First off - your code is OK. I took a copy and made minor changes to it just to use a single sheet for illustration:

问题可能出在您的数据上,特别是第一行.这是同一个示例,其中一些标题"从文本更改为数字.我相信它的行为方式与您所描述的很糟糕.

The problem is likely with your data, specifically the first row. Here's that same example, with some of the "Headers" changed from text into numbers. I believe it is behaving in the bad way you're describing.

再一次,但标题中的数字强制为文本.(在电子表格中,我将它们输入为 '4'5 - 前导单引号告诉电子表格将其视为文本.)

One more time, but with the numbers in the header forced to be text. (In the spreadsheet, I input them as '4 and '5 - the leading single quote tells Spreadsheet to treat as text.)

只需查看第一行中的数据类型,并确保它们是文本.

Just take a look at the data types in your first row, and make sure that they are Text.

这篇关于Google Apps Script Chart 使用第 1 行作为标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:59