'API to export sheet that contains only a chart as a PDF or image
I was wondering if there is any API Call that can perform such operation (export sheet that contains only a chart as a PDF or image).
Firstly i was trying to export a chart from a sheet directly but i found out this is not possible via an API Call.
Solution 1:[1]
This is not possible to achieve with Sheets API. These are the interactions with charts that are available in the Sheets API.
With the Drive API, you can export a spreadsheet as a PDF in one line of code making use of the export() method (you can read more about it here). However, this will export the whole spreadsheet and not just the chart (most notably, the grid lines will be visible).
I would say that the best way to export just the chart is using Apps Script. It cannot be "directly" done here, as charts can only be fetched from a spreadsheet as images. Therefore, you then have to transform said image into a pdf. This can be accomplished in two ways: you can either create a blank pdf and append the image, or you can paste the image into a docs (or slides) file and save that docs file as a pdf.
Here I have opted for the second one:
function myFunction(){
var fileId = "YOUR_SPREADSHEET_ID";
var folderId = "YOUR_FOLDER_ID";
var chart = SpreadsheetApp.openById(fileId).getSheetByName("Sheet1").getCharts()[0];
var tempDocs = DocumentApp.create("temp");
tempDocs.getBody().appendImage(chart.getBlob());
tempDocs.saveAndClose();
docblob = DocumentApp.openById(tempDocs.getId()).getAs('application/pdf');
docblob.setName("myName");
DriveApp.getFolderById(folderId).createFile(docblob);
DriveApp.getFileById(tempDocs.getId()).setTrashed(true);
}
Having said that, you can also "brute force" export the entire spreadsheet as a pdf. You can achieve this by using the Drive API - Files - Export function. Note that this method belongs to the Drive API, not the Sheets API.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
