Importing a chart directly into slides using apps script

957 views Asked by At

I have been trying to write a code that will allow me to import all my charts in a specific google sheet by using the "get sheet by name" method.

After that I replace the specific shape with sheets chart, however I keep getting an error saying "object is not of type Shape. (line 22)". I got the same error for page element, and i fixed that, but now I'm getting another error as well, so I am unsure how to move forward.

Please find the document links, and the code snippet below: Slide link: https://docs.google.com/presentation/d/1CyUkJ7S4eq00MRol3RzcdyAG1m6ovUFBK0H9fxfu-28/edit#slide=id.g8519fa7182_0_355 Sheets link: https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit#gid=0 Code:

function importViewFreqCharts() {
  var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit#gid=0"; //make sure this includes the '/edit at the end
  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var deck = SlidesApp.getActivePresentation();
  var deckTitle = SlidesApp.getActivePresentation().getName();
  var sheet = ss.getSheetByName(deckTitle);
  var charts = sheet.getCharts();
  var slides = deck.getSlides();
  var pieChartSlide = slides[0];
  var pieChart = charts[0];
  var pieChartText1 = sheet.getRange('G7').getValue();
  var pieChartText2 = sheet.getRange('G8').getValue();
  var pageElements = pieChartSlide.getPageElements();
    
  pageElements.forEach(function(pageElement){
   if(pageElement.getPageElementType() == "SHAPE"){
     
   if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
     if(pageElement.asShape().getShapeType() == "RECTANGLE"){
     pageElement.asShape().replaceWithSheetsChart(pieChart);}
     
     if(pageElement.asShape().getShapeType() == "TEXT_BOX"){
     pageElement.asShape().getText().replaceAllText('{{Sample text 1}}',pieChartText1);
     pageElement.asShape().getText().replaceAllText('{{Sample text 2}}',pieChartText2);
     }}}});
     }
1

There are 1 answers

2
Tanaike On BEST ANSWER

Modification point:

  • I think that in your script, when pageElement.asShape().replaceWithSheetsChart(pieChart) is run, pageElement becomes SHEETS_CHART. By this, an error occurs at if(pageElement.asShape().getShapeType() == "TEXT_BOX"){. I think that the reason of your issue is this.

In order to avoid this issue, how about the following modification?

From:

if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
  if(pageElement.asShape().getShapeType() == "RECTANGLE"){
  pageElement.asShape().replaceWithSheetsChart(pieChart);}
  
  if(pageElement.asShape().getShapeType() == "TEXT_BOX"){
  pageElement.asShape().getText().replaceAllText('{{Sample text 1}}',pieChartText1);
  pageElement.asShape().getText().replaceAllText('{{Sample text 2}}',pieChartText2);
  }}}});

To:

pageElements.forEach(function(pageElement){
  if(pageElement.getPageElementType() == "SHAPE"){
    if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
      if (pageElement.asShape().getShapeType() == "RECTANGLE"){
        pageElement.asShape().replaceWithSheetsChart(pieChart);
      } else if(pageElement.asShape().getShapeType() == "TEXT_BOX"){
        pageElement.asShape().getText().replaceAllText('{{Sample text 1}}',pieChartText1);
        pageElement.asShape().getText().replaceAllText('{{Sample text 2}}',pieChartText2);
      }
    }
  }
});

Or

pageElements.forEach(function(pageElement){
  if(pageElement.getPageElementType() == "SHAPE"){
    if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
      if (pageElement.asShape().getShapeType() == "RECTANGLE"){
        pageElement.asShape().replaceWithSheetsChart(pieChart);
      }
      if(pageElement.getPageElementType() == "SHAPE" && pageElement.asShape().getShapeType() == "TEXT_BOX"){
        pageElement.asShape().getText().replaceAllText('{{Sample text 1}}',pieChartText1);
        pageElement.asShape().getText().replaceAllText('{{Sample text 2}}',pieChartText2);
      }
    }
  }
});