简体   繁体   English

如何在Google Apps脚本中为Google电子表格创建具有多个范围的图表

[英]How to create a chart with multiple ranges in google apps script for google spreadsheets

I have a google apps script which deploys a variable number of sheets in the spreadsheet it is attached to, with the intent to gather data from an API, which is working just fine. 我有一个Google Apps脚本,该脚本在附有电子表格的电子表格中部署了可变数量的工作表,目的是从API收集数据,效果很好。

However, I want to visualize this data in graphs, and since it's meant to be self-deplorable, and because there are a variable number of these sheets of data, I want to create a number of charts representing various parts of the data, and in these charts have a range (series, or y axis), for every sheet of data, so it's variable depending on the initialization or setup. 但是,我想在图表中可视化此数据,并且由于它的意思是可自我贬低的,并且由于这些数据表的数量是可变的,因此我想创建许多图表来表示数据的各个部分,并且这些图表中的每张数据都有一个范围(系列或y轴),因此它的可变性取决于初始化或设置。

Long story short and to make it less confusing, I simply want to know how to create a chart and add ranges for every chart. 长话短说,并且为了减少混乱,我只想知道如何创建图表并为每个图表添加范围。 I could iterate through a list for example, but the problem is that I'm getting errors such as: "TypeError: Cannot find function modify in object EmbeddedChartBuilder. (line 97, file "Code") Dismiss" 例如,我可以遍历一个列表,但是问题是我遇到了诸如以下的错误:“ TypeError:在对象EmbeddedChartBuilder中找不到函数修改。(第97行,文件“代码”)关闭”

Also, note I'm trying to pull data from other sheets, so I'm using the sheetName!range notation. 另外,请注意,我正在尝试从其他工作表中提取数据,所以我使用的是sheetName!range表示法。

Here's the particular function I'm working on, note I'm using placeholder values just to get something working for now. 这是我正在使用的特定功能,请注意,我正在使用占位符值只是为了使现在的东西工作。

// TODO
function createCharts(){
  var sheet = getSheet('Graphs');
  var winrateChart = sheet.newChart().setChartType(Charts.ChartType.LINE).setPosition(2, 1, 0, 0);
  // for every user, add appropriate range to chart 
  winrateChart = winrateChart.modify().addRange('username1!A1:B2');
  winrateChart = winrateChart.modify().addRange('username2!A1:B2');
  sheet.updateChart(winrateChart);
  winrateChart = winrateChart.build()
  sheet.insertChart(winrateChart);
}

I've figured out what the problems were, mostly through experimentation, I still am of the opinion the documentation is lacking clarity and good examples. 我已经找出了问题所在,主要是通过实验,我仍然认为文档缺乏清晰性和良好的示例。 But here is my my understanding. 但是,这是我的理解。

First of, there are three classes to deal with for this problem, Sheet, EmbeddedChart and EmbeddedChartBuilder. 首先,有3个类用于处理此问题,即Sheet,EmbeddedChart和EmbeddedChartBuilder。

EmbeddedChartBuilder is what is returned by Sheet.newChart(), by using the .build() method you get an EmbeddedChart class, which can be used as a parameter to Sheet.insertChart(). 通过使用.build()方法,Sheet.newChart()返回的是EmbeddedChartBuilder,您将获得一个EmbeddedChart类,该类可以用作Sheet.insertChart()的参数。 Furthermore through experimentation it seems like you must insert a chart by Sheet.insertChart(EmbeddedChart) before you can use EmbeddedChart.modify(), which in turn returns an EmbeddedChartBuilder, which you can use to chain any number of methods such as add range. 此外,通过实验,似乎您必须先通过Sheet.insertChart(EmbeddedChart)插入一个图表,然后才能使用EmbeddedChart.modify(),而后者又返回一个EmbeddedChartBuilder,您可以使用它链接许多方法,例如添加范围。 And of course you still have to use .build() to return an EmbeddedChart from an EmbeddedChartBuilder, then Sheet.updateChart(EmbeddedChart) afterwards. 当然,您仍然必须使用.build()从EmbeddedChartBuilder返回EmbeddedChart,然后再使用Sheet.updateChart(EmbeddedChart)。

The last problem was a mistake I've made, I was calling .addRange() using a string as parameter, however addRange requires a Range object. 最后一个问题是我犯了一个错误,我使用字符串作为参数调用.addRange(),但是addRange需要一个Range对象。

Anyway, here is some code demonstrating an example use, you can chain as many modify() as you want, for example in a loop that iterates through sheets. 无论如何,这是一些演示示例用法的代码,您可以根据需要链接任意数量的modify(),例如,在循环浏览工作表的循环中。

Note: getSheet is a custom function, does what you'd expect. 注意:getSheet是一个自定义函数,可以实现您所期望的功能。

function createCharts(){
  var sheet = getSheet('Graphs');
  var chart = sheet.newChart().setChartType(Charts.ChartType.LINE).setPosition(2,1,0,0).build(); // build returns an EmbeddedChart object 
  Logger.log(chart);
  sheet.insertChart(chart);
  chart = chart.modify().addRange(getSheet('someSheetName').getRange('B1:B999')).build();
  Logger.log(chart);
  sheet.updateChart(chart);
}

EDIT: here's the entire, working function, not useful outside this particular code, however, you can see the logic behind it, I'm gonna try to make it universally useful by iterating through sheets or something. 编辑:这是整个工作功能,在该特定代码之外没有用,但是,您可以看到其背后的逻辑,我将尝试通过遍历工作表或其他方式使它通用。

function createChart(range, x, y, title, width, height){
  // range: string; x: int; y: int; title: string; width: int; height; int
  // Creates a chart with data from all usernames for a given range at x row, y column, with a title, a width and height
  var sheet = getSheet('Graphs');
  // chart setup
  var chart = sheet.newChart()
  .setChartType(Charts.ChartType.LINE)
  .setPosition(x,y,0,0)
  .setNumHeaders(1)
  .setOption('title', title)
  .setOption('width', width)
  .setOption('height', height)
  .build();
  sheet.insertChart(chart);
  // for every username, add corresponding data into chart
  for(var i = 0; i < fortniteUsernames.length; i++){
    var username = fortniteUsernames[i];
    chart = chart.modify().addRange(getSheet(username).getRange(range)).build();
    sheet.updateChart(chart);    
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM