google-apps-scriptgoogle-workspace

How to Automate Exporting Google Docs to Markdown Using Google Apps Script?


I'm trying to automate the process of exporting Google Docs to Markdown format using Google Apps Script. I have noticed that when I go to a Google Doc, there are several export options available, including one for .md (Markdown) that works great. How can I automate this process?

Key Aspects of My Problem:

  1. Native Export Options: Given that Google Docs provides an option to export documents as Markdown (.md), is there a way to automate this native export functionality using Google Apps Script?

Closest Solution I Found:

I read this Google Apps Script sample that generates a download link for one exact file, but it doesn't handle exporting many files at once.

Specific Question:

Any guidance, examples, or insights on how to effectively automate this process would be greatly appreciated!

Update:

I just wanted to share an update that, with the help of Tanaike, I was able to create the tool I needed! This tool allows you to export any Google Drive folder while maintaining its subfolder structure and converting Google Docs to a specific file format (like Markdown or PDF). I've made the tool public so that others can benefit from it as well.

You can check out the tool here: Google Drive Folder Export & Google Docs Converter

A big thank you to Tanaike for the invaluable assistance and resources!


Solution

  • In your situation, is this post useful? Ref (Author: me) In this post, the sample scripts for converting Google Document to markdown format and vice versa are shown. When this script is used to Is there a way to automate the native .md export option in Google Docs using Google Apps Script for multiple documents in a folder?, how about the following sample script?

    When you use this script, please enable Drive API at Advanced Google services. And, please set the values srcFolderId and dstFolderId.

    Sample script 1:

    In this script, Google Documents in a folder are converted to markdown format.

    function sample1() {
      const srcFolderId = "###"; // Please set the folder ID of the folder including Google Documents.
      const dstFolderId = "###"; // Please set the destination folder ID.
    
      const token = ScriptApp.getOAuthToken();
      const dstFolder = DriveApp.getFolderById(dstFolderId);
      const files = DriveApp.getFolderById(srcFolderId).getFilesByType(MimeType.GOOGLE_DOCS);
      while (files.hasNext()) {
        const doc = files.next();
        const documentId = doc.getId();
        const url = `https://docs.google.com/feeds/download/documents/export/Export?exportFormat=markdown&id=${documentId}`;
        const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + token } });
        const blob = res.getBlob().setName(doc.getName() + ".md").setContentTypeFromExtension();
        dstFolder.createFile(blob);
      }
    }
    

    Sample script 2:

    In this script, markdown files in a folder are converted to Google Documents.

    function sample2() {
      const srcFolderId = "###"; // Please set the folder ID of the folder including Google Documents.
      const dstFolderId = "###"; // Please set the destination folder ID.
    
      const files = DriveApp.getFolderById(srcFolderId).searchFiles("(mimeType='text/markdown' or mimeType='text/x-markdown') and trashed=false");
      while (files.hasNext()) {
        const doc = files.next();
        const fileId = doc.getId();
        Drive.Files.copy({ name: doc.getName(), mimeType: MimeType.GOOGLE_DOCS, parents: [dstFolderId] }, fileId, { supportsAllDrives: true });
      }
    }
    

    Note:

    References: