Converting SVG Format to PNG Format using Google Apps Script

Gists

This is a sample script for converting the SVG image data to PNG image data using Google Apps Script.

Unfortunately, in the current stage, there are no methods for directly converting the SVG to PNG in Google Drive service. But it can be achieved by Drive API. The sample script is as follows.

Before you use this, please enable Drive API at Advanced Google services.

Sample script

function myFunction() {
  const svgFileId = "###"; // Please set the fileId of the SVG file.

  const url = Drive.Files.get(svgFileId).thumbnailLink.replace(
    "=s220",
    "=s1000"
  );
  const blob = UrlFetchApp.fetch(url).getBlob(); // blob is the image blob of PNG format.

  // In this sample, the retrieved image blob is put to Spreadsheet.
  const sheet = SpreadsheetApp.openById("###").getSheetByName("Sheet1");
  sheet.insertImage(blob, 1, 1).setWidth(500).setHeight(500);
}
  • In this sample script, the converted PNG image is put to the Spreadsheet.

  • About Drive.Files.get(svgFileId).thumbnailLink.replace("=s220", "=s1000"), you can see the detail information at this repository.

  • When above method is used, the images and movies which can be seen at Google Drive can be converted to the PNG data.

 Share!