如何通过Google App Script更新组合图表的数据范围?

huangapple go评论66阅读模式
英文:

How can update data range of combo chart by Google App Script?

问题

The error you're encountering when trying to update the data range of the Combo chart could be due to a couple of reasons:

  1. Chart Positioning: Make sure that the Combo chart is the first chart in the sheet, as you're accessing it with charts[0]. If the order of charts changes, this code might not work as expected.

  2. Valid Range: Ensure that the new data range you're trying to add (CQ51:DB58) is a valid range for the Combo chart. Verify that this range exists within the sheet and is in the correct format.

  3. Chart Type Compatibility: Double-check that the Combo chart can accommodate the new data range you're trying to add. Different chart types have specific data requirements, and the new range must be compatible with the Combo chart type.

  4. Permissions: Ensure that the script has the necessary permissions to access and modify the spreadsheet. It might not have the required access to update the Combo chart.

By addressing these potential issues, you should be able to update the data range of the Combo chart successfully.

英文:

In a Google Sheet, i have 2 charts: a Combo chart, a Pie Chart

Data Range of Combo chart: C51:O57,Q51:AB58,AD51:AO58,AQ51:BB58,BD51:BO58,BQ51:CB58,CD51:CO58
and Combine Range is Horizontally

Data Range of Pie Chart: A51:A55,CQ51:CQ55
and Combine Range is Horizontally

I want update Data Range of 2 Charts:

Combo chart: add Range "CQ51:DB58"

Pie Chart: replace "CQ51:CQ55" to "DD51:DD55"

I had try this code:

Pie Chart update ok.

Combo chart occur error: Exception: Service Spreadsheets failed while accessing document with id

    var oSpreadSheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet_Des=oSpreadSheet.getSheetByName("Sheet1");
    var charts = sheet_Des.getCharts();

    //Pie Chart 
   var chart = charts[1];
   var ranges = chart.getRanges();
   var newRange=sheet_Des.getRange(51,108 ,5,1);
  // var  rangename=newRange.getA1Notation();
   var chart = chart.modify()
          .removeRange(ranges[1])          
          .addRange(newRange)
          .build();
   sheet_Des.updateChart(chart);

    //Combo Chart 
    chart = charts[0];
    ranges = chart.getRanges();
    newRange=sheet_Des.getRange(51,95 ,8,12);
    // var  rangename2=newRange.getA1Notation();
    chart = chart.modify()          
          .addRange(newRange)
          .build();       
    sheet_Des.updateChart(chart);

Why can't update data range of Combo chart?

答案1

得分: 1

我测试了你的脚本,出现了相同的错误。这似乎是一个问题,我在Google的问题跟踪器上找到了类似的报告这个是另一个示例。你可以在这两个问题上添加评论以请求更新,或者查看是否有其他人找到了解决方法。

对我有效的一种解决方法是删除图表并创建一个新的,我使用了以下脚本:

function createChart() {
  var oSpreadSheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet_Des = oSpreadSheet.getSheetByName("Sheet1");
  var charts = sheet_Des.getCharts();

  //Pie Chart 
  var chart = charts[1];
  var ranges = chart.getRanges();
  var newRange = sheet_Des.getRange(51, 108, 5, 1);
  // var  rangename=newRange.getA1Notation();
  var chart = chart.modify()
    .removeRange(ranges[1])          
    .addRange(newRange)
    .build();
  sheet_Des.updateChart(chart);

  //Combo Chart 
  chart = charts[0];
  var options = charts[0].getOptions().get('title'); //如果你想在删除组合图表之前复制一些选项,需要将每个选项单独存储在变量中
  ranges = chart.getRanges();
  newRange = sheet_Des.getRange(51, 95, 8, 12);
  // var  rangename2=newRange.getA1Notation();
  sheet_Des.removeChart(chart);
  chart = sheet_Des.newChart()
    .asComboChart()
    .addRange(newRange)
    .setOption('title', options) //设置你想要的选项(标题、宽度、高度等)
    .build()
  sheet_Des.insertChart(chart.modify().setPosition(43, 6, 0, 0).build()) //调整要创建图表的位置
}

更新

这里是使用Sheets API的示例。你可以点击JAVASCRIPT标签以了解如何在Apps Script中发出请求。

注意: 如果你使用Apps Script访问Sheets API,你需要启用高级Sheets服务。

参考链接:

英文:

I tested your script and I got the same error. This seems to be an issue, I found similar reports on Google's Issue Tracker, this is another sample. You can add comments on both issues to ask for updates or check if someone else found a workarond.

A workaround that worked for me, is deleting the chart and creating a new one, I used the following script:

function createChart() {
  var oSpreadSheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet_Des=oSpreadSheet.getSheetByName("Sheet1");
  var charts = sheet_Des.getCharts();

  //Pie Chart 
  var chart = charts[1];
  var ranges = chart.getRanges();
  var newRange=sheet_Des.getRange(51,108 ,5,1);
  // var  rangename=newRange.getA1Notation();
  var chart = chart.modify()
    .removeRange(ranges[1])          
    .addRange(newRange)
    .build();
  sheet_Des.updateChart(chart);

  //Combo Chart 
  chart = charts[0];
  var options = charts[0].getOptions().get('title'); //If you want to copy some options from the Combo Chart before deleting it, you need to get each option in a separate variable
  ranges = chart.getRanges();
  newRange=sheet_Des.getRange(51,95 ,8,12);
  // var  rangename2=newRange.getA1Notation();
  sheet_Des.removeChart(chart);
  chart = sheet_Des.newChart()
    .asComboChart()
    .addRange(newRange)
    .setOption('title',options) //Set the options that you want (title, width, height, etc)
    .build()
  sheet_Des.insertChart(chart.modify().setPosition(43,6,0,0).build()) //Adjust the position where you want to create the Chart
}

UPDATE

This is a sample with the Sheets API. You can click in the JAVASCRIPT tab to get an idea of how you make the request in Apps Script.

Note: If you access the Sheets API using Apps Script, you will have to enable the Advanced Sheets Service.

References:

huangapple
  • 本文由 发表于 2023年5月22日 18:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定