Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martinbowling/47d59b51a3af1b5d90b3a5589cb071a2 to your computer and use it in GitHub Desktop.
Save martinbowling/47d59b51a3af1b5d90b3a5589cb071a2 to your computer and use it in GitHub Desktop.
Google Doc Inline Base64 Images or Sanely Named Images ZIP
function downloadDocWithImagesInZip() {
const doc = DocumentApp.getActiveDocument();
const docName = doc.getName();
const docBody = doc.getBody();
const zip = Utilities.newBlob('', 'application/zip', `${docName}.zip`).getAs('application/zip');
let content = '';
let imageCount = 0;
const totalElements = docBody.getNumChildren();
for (let i = 0; i < totalElements; i++) {
const element = docBody.getChild(i);
content += processElement(element, zip, docName, imageCount);
}
const htmlContent = `
<html>
<head>
<title>${docName}</title>
</head>
<body>
${content}
</body>
</html>
`;
zip.createFile(Utilities.newBlob(htmlContent, 'text/html', `${docName}.html`));
const zipFile = zip.close();
const file = DriveApp.createFile(zipFile);
const url = file.getDownloadUrl();
// Create a download link and prompt the user
const htmlOutput = HtmlService.createHtmlOutput(`
<html>
<body>
<a href="${url}" target="_blank" download="${docName}.zip">Click here to download your document</a>
</body>
</html>
`);
DocumentApp.getUi().showModalDialog(htmlOutput, 'Download Document');
}
function processElement(element, zip, docName, imageCount) {
let content = '';
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
const paragraph = element.asParagraph();
content += '<p>' + processText(paragraph) + '</p>';
} else if (element.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
const image = element.asInlineImage();
const imageBlob = image.getBlob();
const mimeType = imageBlob.getContentType();
const extension = mimeType.split('/')[1];
const imageName = `${docName}_image_${imageCount}.${extension}`;
zip.createFile(imageBlob.setName(imageName));
content += `<img src="${imageName}" />`;
imageCount++;
} else if (element.getType() === DocumentApp.ElementType.TABLE) {
content += processTable(element.asTable(), zip, docName, imageCount);
}
return content;
}
function processText(textElement) {
return textElement.getText();
}
function processTable(table, zip, docName, imageCount) {
let content = '<table border="1">';
for (let i = 0; i < table.getNumRows(); i++) {
const row = table.getRow(i);
content += '<tr>';
for (let j = 0; j < row.getNumCells(); j++) {
const cell = row.getCell(j);
content += `<td>${processElement(cell, zip, docName, imageCount)}</td>`;
}
content += '</tr>';
}
content += '</table>';
return content;
}
function downloadDocWithBase64Images() {
const doc = DocumentApp.getActiveDocument();
const docName = doc.getName();
const docBody = doc.getBody();
let content = '';
const totalElements = docBody.getNumChildren();
for (let i = 0; i < totalElements; i++) {
const element = docBody.getChild(i);
content += processElement(element);
}
const htmlContent = `
<html>
<head>
<title>${docName}</title>
</head>
<body>
${content}
</body>
</html>
`;
const blob = Utilities.newBlob(htmlContent, 'text/html', `${docName}.html`);
const file = DriveApp.createFile(blob);
const url = file.getDownloadUrl();
// Create a download link and prompt the user
const htmlOutput = HtmlService.createHtmlOutput(`
<html>
<body>
<a href="${url}" target="_blank" download="${docName}.html">Click here to download your document</a>
</body>
</html>
`);
DocumentApp.getUi().showModalDialog(htmlOutput, 'Download Document');
}
function processElement(element) {
let content = '';
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
const paragraph = element.asParagraph();
content += '<p>' + processText(paragraph) + '</p>';
} else if (element.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
const image = element.asInlineImage();
const base64 = Utilities.base64Encode(image.getBlob().getBytes());
const mimeType = image.getBlob().getContentType();
content += `<img src="data:${mimeType};base64,${base64}" />`;
} else if (element.getType() === DocumentApp.ElementType.TABLE) {
content += processTable(element.asTable());
}
return content;
}
function processText(textElement) {
return textElement.getText();
}
function processTable(table) {
let content = '<table border="1">';
for (let i = 0; i < table.getNumRows(); i++) {
const row = table.getRow(i);
content += '<tr>';
for (let j = 0; j < row.getNumCells(); j++) {
const cell = row.getCell(j);
content += `<td>${processElement(cell)}</td>`;
}
content += '</tr>';
}
content += '</table>';
return content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment