1
votes

The following code can save an image to a sheet but I can't seem to figure out how to save it to Google Drive. Here is a Google sheet you can copy: https://docs.google.com/spreadsheets/d/1AkJqpwmq0-GTveneyuQugPL900LpTS2MDpQjHz_p5lk/edit?usp=sharing

function testchartbuild() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet1');
  var chart = sheet.newChart()
       .setChartType(Charts.ChartType.LINE)
       .addRange(sheet.getRange('A1:A12'))
       .setPosition(5, 5, 0, 0)
       .build();
  
  // create an image from that EmbeddedChart
  sheet.insertImage(chart.getBlob(),6,1);  // this works
  
  // the file created is only 4 bytes long
  var newFile = createGoogleDriveFile(chart.getBlob());
}


function createGoogleDriveFile(image) {
  var newFile;
  
  newFile = DriveApp.createFile('test.png',image,'image/png');//Create a new file in the root folder
  
  return newFile;
};
1

1 Answers

2
votes
  • You want to save the chart to the Google Drive.
  • You want to achieve this by modifying your script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • In your script, please modify the script of the function createGoogleDriveFile.
    • At the method of createFile(blob), the argument is blob.
    • When you want to give the filename, please use setName().
    • In this case, the mimeType has already been given to the blob.

Modified script:

When your script is modified, please modify as follows.

newFile = DriveApp.createFile('test.png',image,'image/png');
newFile = DriveApp.createFile(image.setName('test.png'));

References:

If I misunderstood your question and this was not the direction you want, I apologize.