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?
.md), is there a way to automate this native export functionality using Google Apps Script?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.
.md export option in Google Docs using Google Apps Script for multiple documents in a folder?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!
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.
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);
}
}
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 });
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With