在Google电子表格中,我希望使用脚本绘制折线图,其中A列为X,B列为Y。
但是下面的代码将绘制2行。我该如何修改它以绘制一条直线,就像B = f(A)。
function myDemoLineChart() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B4")
Logger.log(range)
var lineChart = sheet.newChart()
.setPosition(6, 1, 0, 0)
.setChartType(Charts.ChartType.LINE)
.setOption("title", "Demo")
.setOption("hAxis.title","X")
.setOption("vAxis.title", "Y")
.setOption("curveType","function")
.setOption("pointSize",10)
.setOption("width",800)
.setOption("height",800)
.addRange(range)
.build()
sheet.insertChart(lineChart)
}
输入数据如下:
A B
3 5
1 7
4 2
2 9
答案 0 :(得分:2)
此修改如何?
setOption('useFirstColumnAsDomain', true)
作为选项。function myDemoLineChart() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B4")
Logger.log(range)
range.sort(1); // Added
var lineChart = sheet.newChart()
.setOption('useFirstColumnAsDomain', true) // Added
.setPosition(6, 1, 0, 0)
.setChartType(Charts.ChartType.LINE)
.setOption("title", "Demo")
.setOption("hAxis.title","X")
.setOption("vAxes", {0: {title: "Y"}}) // Modified
.setOption("curveType","function")
.setOption("pointSize",10)
.setOption("width",800)
.setOption("height",800)
.addRange(range)
.build()
sheet.insertChart(lineChart)
}
如果这不是您想要的结果,我表示歉意。