Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active April 1, 2023 11:26
Show Gist options
  • Save tanaikech/8a6d46ca43665aa7e62965ed32336598 to your computer and use it in GitHub Desktop.
Save tanaikech/8a6d46ca43665aa7e62965ed32336598 to your computer and use it in GitHub Desktop.
Converting SVG Format to PNG Format using Google Apps Script

Converting SVG Format to PNG Format using Google Apps Script

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment