0

I'm attempting to use Google Scripts to create a Pie Chart based on two columns of a Google Sheet but I'm unsure how to prevent my "Labels" dataset from showing redundant labels. I'm sure it's something simple but any help would be appreciated.

Pie Chart with redundant labels.

Sheets columns that I'm pulling data from.

Here is the code I'm using:

function onOpen() {
    var menuEntries = [{
        name: "Create chart from Calendar data",
        functionName: "makemyChart"
    }];
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.addMenu("Austin's test", menuEntries);
}




function makemyChart() {
  var sheet = SpreadsheetApp.getActiveSheet(); //retrieves active open sheet
   Logger.log(sheet) // check logs and it should tell you name of the sheet tab


  // start building a data table that will feed into the chart
   var chartDataRange = sheet.getRange(("F:G"));

  var hAxisOptions = {
    slantedText: true,
    slantedTextAngle: 60,
    gridlines: {
      count: 12
    }
  };



  //build a new pie chart from data

   var pieChartBuilder = sheet.newChart().asPieChart()
  var chart = pieChartBuilder
    .addRange(chartDataRange)
    .setTitle('Event Break Down')
    .setNumHeaders(1)
    .setPosition(5, 5, 0, 0)
    .setLegendPosition(Charts.Position.RIGHT)
    .setOption('hAxis', hAxisOptions) //Orginally 'hAxis', hAxisOptions
    .build();


  // insert chart IDEALLY on a new sheet(tab) of this same spreadsheet)

  // Adds tab
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.duplicateActiveSheet(); //Copies current sheet
  ss.renameActiveSheet("Time Breakdown"); //Renames sheet
  ss.moveActiveSheet(1); //Moves sheet to the first position

  // Prepares tab (clears old content)
  var sheet = ss.getSheetByName("Time Breakdown");
  var rangeToClear = ("A:K")
  sheet.getRange(rangeToClear).clearContent()


  sheet.insertChart(chart);  


}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
BFBeam
  • 1
  • 1
  • What labels do you consider redundant here? – TheMaster May 07 '20 at 19:37
  • For example, I'd like "B," and "Test," to only show up once in the Pie Chart instead of multiple times. – BFBeam May 07 '20 at 20:13
  • If you were to create the chart manually in Google sheets, were you able to achieve the desired result? – TheMaster May 07 '20 at 20:43
  • 1
    Assuming the labels are the same(i.e., without different spaces) , try aggregate [here](https://stackoverflow.com/a/52012921/) – TheMaster May 07 '20 at 20:45
  • @TheMaster I plan on using the chart in a larger app that exports data and creates the Pie chart automatically or else the manual option would be ideal. But the aggregate example you linked worked perfectly! That seemed to solve my issue. I think using the word "redundant" in my description of the issue was a little off. I appreciate the help. – BFBeam May 08 '20 at 19:36
  • The intention was not to ask you to do it manually. But to check and confirm whether it can indeed be done manually. Confirming that would exclude a lot of issues. Anyway, glad to help. I marked this question as duplicate-accept it as duplicate if it solved your issue. – TheMaster May 08 '20 at 19:45

0 Answers0