MimeTypeApp: Flexible MimeType Converter with Google Apps Script

Gists

Abstract

This is a Google Apps Script library for converting files from various MIME types to a specified target MIME type. The library accepts both file IDs and blobs as input values.

Introduction

Recently, I encountered a scenario where I needed to convert files of various MIME types to a specific target MIME type. While converting files with known source MIME types is relatively straightforward, the process becomes more complex when the source MIME type is unknown. To simplify this task, I developed a Google Apps Script solution that can effectively convert files of diverse MIME types to a desired target MIME type. The script can accept both file IDs and blobs as input values.

Repository

The repository of this library is https://github.com/tanaikech/MimeTypeApp.

Principle

This library follows the principles outlined below:

  1. Retrieve importFormats and exportFormats information about the user’s Drive using the about.get method of the Drive API v3. Ref
  2. Based on the retrieved formats, create a conversion route that transforms the source mimeType to the target mimeType.
  3. Apply the created route to convert the source mimeType to the target mimeType.

Example:

Consider converting from text/html to text/markdown. Here’s the conversion route:

  1. text/html
  2. application/vnd.google-apps.document (achieved by importing from text/html)
  3. text/markdown (achieved by exporting from application/vnd.google-apps.document)

Current Limitations:

This library cannot directly retrieve the content (blob) from Google Docs files (Documents, Spreadsheets, Slides, etc.) because of the current specification at Google side. Therefore, for the above route, while text/html and text/markdown can be handled as blobs, the intermediate step (application/vnd.google-apps.document) requires file access. This library takes this limitation into account.

When this library is used, the conversion from application/vnd.google-apps.spreadsheet to application/vnd.google-apps.document can be achieved. But, in this case, the format might be broken. So, please be careful about this.

Usage

1. Use this as a library

In order to use this as a library, please install the library as follows.

  1. Create a GAS project.

    • You can use this library for the GAS project of both the standalone type and the container-bound script type.
  2. Install this library.

    The library’s project key is 112yLH6obJWYpE8RY89DTF7CLp-SEZ2tnA3pesP5Lubq-qmujLkQJao5-.

2. Use this as a standalone

In order to use this as a standalone, please copy and paste the class MimeTypeApp script into your project.

Scopes

This library uses the following scopes.

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/script.external_request

Constructor

This script can be used as a Google Apps Script library and a standalone script.

When you want to use this as a Google Apps Script library, please install it to your project as a library and use the constructor as follows.

const m = MimeTypeApp;

When you want to use this as a standalone script, please copy and paste the class MimeTypeApp script to your project and use the constructor as follows.

const m = new MimeTypeApp();

The former is used in the following sample scripts.

IMPORTANT

  • Important Note: When retrieving a blob from Google Docs files (Documents, Spreadsheets, Slides, etc.) using Google Apps Script, the original mimeType is automatically converted to PDF (application/pdf).
  • To convert the original mimeType of Google Docs files, use the file ID instead of the blob. When using a blob from Google Docs files, the source mimeType will always be PDF. Please be aware of this limitation.
  • When the target mimeType is a Google Docs file, the output will be the file ID, not a blob. This is because Google Docs files cannot be directly converted to blobs.
  • This library converts MIME types using exportFormats and importFormats of the about.get method. However, there are MIME types it cannot convert. Additionally, format integrity might be compromised when converting between certain Google Docs formats, such as converting a Google Spreadsheet to a Google Document. Please use caution.

Sample script

In the following sample scripts, it is supposed that MimeTypeApp has been installed as a Google Apps Script library.

Sample 1

This sample script shows the list of the mimeTypes that this library can convert.

const res = MimeTypeApp.getConversionList();
console.log(res);

Sample 2

This sample script converts a PDF file to a Markdown file. Please set the file ID of the PDF file to fileIds.

This library can use multiple file IDs. So, when you want to convert each mimeType of the multiple files to the specific mimeType, please add them to fileIds.

const fileIds = ["###fileId of PDF file###"];
const object = { mimeType: "text/markdown" };

const res = MimeTypeApp.setFileIds(fileIds).getAs(object);
res.forEach((blob) => {
  if (blob && blob.toString() == "Blob") {
    DriveApp.createFile(blob);
  }
});

In this case, res is Blob[]. When the source mimeType cannot be converted to the target mimeType, null is returned as the element of the array.

Sample 3

This sample script converts a PDF blob to a Markdown blob. Please set your PDF blob to blob.

const blob = // Please set your PDF blob.
const object = { mimeType: "text/markdown" };

const res = MimeTypeApp.setBlobs([blob]).getAs(object);
const convertedBlob = res[0]; // Markdown blob.

When this script is run, convertedBlob is the Markdown blob.

Sample 4

This sample script converts a PDF file to a Google Document. Please set the file ID of the PDF file to fileIds.

const dstFolderId = "###"; // Please set the destination folder ID.
const fileIds = ["###fileId of PDF file###"];
const object = { mimeType: MimeType.GOOGLE_DOCS, folderId: dstFolderId };

const res = MimeTypeApp.setFileIds(fileIds).getAs(object);
console.log(res);

In this case, res is String[] including the file ID of the file converted to Google Document. The converted Google Document is created in the folder of dstFolderId.

Sample 5

This sample script converts a PDF blob to a Google Document. Please set your PDF blob to blob.

const dstFolderId = "###"; // Please set the destination folder ID.
const blob = // Please set your PDF blob.
const object = { mimeType: MimeType.GOOGLE_DOCS, folderId: dstFolderId };

const res = MimeTypeApp.setBlobs([blob]).getAs(object);
console.log(res);

In this case, res is String[] including the file ID of the file converted to Google Document. The converted Google Document is created in the folder of dstFolderId.

Sample 6

This sample script converts a file to a thumbnail image. Please set your file ID to fileIds.

const fileIds = ["###fileId###"];

const res = MimeTypeApp.setFileIds(fileIds).getThumbnails();
console.log(res);

In this case, res is Blob[]. The width of the exported image size is 1000 pixels as the default. When you want to change this, please set the width to the argument of getThumbnails like getThumbnails(500).

Summary

This report introduced sample scripts for converting file and blob mimeTypes using Google Apps Script. This library provides functions to convert various mimeTypes to a specific target mimeType, making it a valuable tool for a range of Google Apps Script applications.

 Share!