Skip to content Skip to sidebar Skip to footer

Get As Image Generates Wrong Image - Google App Script

My goal is to create google documents using information from a google sheet with google app script. I can generate a radar chart from the data in the google sheets. The radar char

Solution 1:

How about this answer?

Issue and workaround:

I think that this might be a bug. In this case, as a workaround, I use a Google Slides which can directly put the chart created with Google Spreadsheet. When the blob is retrieved from the chart put to the Google Slides, the blob is the same with the chrat on Google Spreadsheet.

Modified script:

When your script is modified, please modify as follows.

From:
var image = sheet.getCharts()[0].getBlob().getAs('image/png');
To:
const slides = SlidesApp.create("temp");
const imageBlob = slides.getSlides()[0].insertSheetsChartAsImage(chart).getAs("image/png");
DriveApp.getFileById(slides.getId()).setTrashed(true);
DriveApp.createFile(imageBlob);
  • When imageBlob is created with DriveApp.createFile(imageBlob) as a file, the following result can be obtained.

Result:

When image and imageBlob are created as a file, the left and right images are obtained, respectively.

enter image description here

Note:

  • If you want to put the correct chart image to Google Document using this workaround, please modify as follows.

    const slides = SlidesApp.create("temp");
      const imageBlob = slides.getSlides()[0].insertSheetsChartAsImage(chart).getAs("image/png");
      DriveApp.getFileById(slides.getId()).setTrashed(true);
    
      // This script puts the retrieved image blob to the Google Document.
      DocumentApp.openById("### Document ID ###").getBody().insertImage(0, imageBlob);
    

Reference:

Post a Comment for "Get As Image Generates Wrong Image - Google App Script"