Home > front end >  Google Apps Script: Insert Data Table into Google Slide
Google Apps Script: Insert Data Table into Google Slide

Time:02-24

I'm running into a problem when trying to insert my DataTable (using the Charts service) into a Google Slide.

The problem appears when I try to get the chart's Blob, or getting the chart as 'image/png' or jpeg, etc.

Here is the code:

function TablesConstructor(slide_obj){
  //slide_obj is an object that contains all the information needed.
  //Let's open the file
  var slide_file = SlidesApp.openById(slide_obj.slide_id);
  
  //Get the Slides
  var slides = slide_file.getSlides();
  var slides_num = slides.length;
  
  //Get the Slide N-1, and it's images
  var slide_images = slides[slides_num - 2].getImages();
 
  var slide_image_1 = slide_images[0];
    
  //Create Data Table chart
  var table_data = Charts.newDataTable()
  .addColumn(Charts.ColumnType.NUMBER, "Col1")
  .addColumn(Charts.ColumnType.NUMBER, "Col2")
  .addColumn(Charts.ColumnType.NUMBER, "Col3")
  .addColumn(Charts.ColumnType.NUMBER, "Col4")
  
  //Populate data table chart with the 2D array inside my object
  var rows = slide_obj.eem_table_data.length <= 10 ? slide_obj.eem_table_data.length : 10;

  for(var i = 0; i < rows; i  ){
    table_data.addRow([slide_obj.eem_table_data[i][0],
    slide_obj.eem_table_data[i][1], 
    slide_obj.eem_table_data[i][2], 
    slide_obj.eem_table_data[i][3], 
    );
  }

  //Build the table
  var table_chart = Charts.newTableChart().setDataTable(table_data).build();
  
  //ATTEMPT 1
  //Get the chart Blob and replace the image with the blob
  var image_blob = table_chart.getBlob();
  slide_image_1.replace(image_blob, true);
 
  slide_file.saveAndClose();  
}

I also made a couple of other approaches that unfortunately didn't work either

  //ATTEMPT 2
  //Set the ContentType to jpeg
  var table_jpeg = table_chart.getBlob().setContentType('image/jpeg')
  slide_image_1.replace(table_jpeg, true);
  
  //ATTEMP 3
  //Replace a Shape with the table chart
  slide_elements.forEach(function(pageElement){
    Logger.log(pageElement.getPageElementType());
    if(pageElement.asShape().getShapeType() !== "UNSUPPORTED"){
      if (pageElement.asShape().getShapeType() == "RECTANGLE"){
        pageElement.asShape().replaceWithImage(table_chart.getAs('image/jpeg'));
      }
    }
  });

I have other kind of charts in my slide (bars) and they work just fine but for this particular one the error message I get is:

Exception: We're sorry, a server error occurred. Please wait a bit and try again.

This, on line

var image_blob = table_chart.getBlob();

CodePudding user response:

From your proposal, I posted this. In this case, please set the size using setDimensions as follows.

From:

var table_chart = Charts.newTableChart().setDataTable(table_data).build();

To:

var table_chart = Charts.newTableChart().setDataTable(table_data).setDimensions(640, 480).build();

Reference:

CodePudding user response:

Solved it with this answer:

Google Script: how to send Tablechart in e-mail?

One needs to declare the size of the chart using setDimensions().

  • Related