Convert a Google Document into an EPUB File

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } /* @jsx mdx */ var _frontmatter = { "title": "Convert a Google Document into an EPUB File", "date": "2020-01-14", "slug": "/code/convert-google-doc-epub-200114", "template": "post", "category": "Code", "tags": ["Google Apps Script", "Google Docs", "epub"] }; var makeShortcode = function makeShortcode(name) { return function MDXDefaultShortcode(props) { console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope"); return mdx("div", props); }; }; var layoutProps = { _frontmatter: _frontmatter }; var MDXLayout = "wrapper"; return function MDXContent(_ref) { var components = _ref.components, props = _objectWithoutProperties(_ref, ["components"]); return mdx(MDXLayout, _extends({}, layoutProps, props, { components: components, mdxType: "MDXLayout" }), mdx("p", null, "With Google Documents, you can easily ", mdx("a", _extends({ parentName: "p" }, { "href": "/internet/convert-docs-to-epub-ebooks/29103/" }), "save any document"), " into an ePub file in the browser. Just open any document inside the Google Documents web editor and choose ", mdx("code", _extends({ parentName: "p" }, { "className": "language-text" }), "File > Download > EPUB"), " from the menu to convert the file."), mdx("p", null, "If you have bunch of documents in your Google Drive, converting them to EPUB format manually would be a tedious task and that\u2019s where Apps Script can help."), mdx("div", { "className": "gatsby-highlight", "data-language": "javascript" }, mdx("pre", _extends({ parentName: "div" }, { "className": "language-javascript" }), mdx("code", _extends({ parentName: "pre" }, { "className": "language-javascript" }), mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "function"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "convertDocToEPUB"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "{"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Get the currently opened document"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "var"), " document ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "="), " DocumentApp", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getActiveDocument"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Get the EPUB export link"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "var"), " mimeType ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "="), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token string" }), "\"application/epub+zip\""), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "var"), " exportLink ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "="), " Drive", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), "Files", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "get"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), "document", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getId"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), "exportLinks", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "["), "mimeType", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "]"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Get the blob of the epub exported file"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "var"), " response ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "="), " UrlFetchApp", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "fetch"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), "exportLink", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ","), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "{"), "\n headers", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), ":"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "{"), "\n Authorization", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), ":"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token string" }), "\"Bearer \""), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "+"), " ScriptApp", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getOAuthToken"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "}"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "}"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Save the EPUB file in the Google Drive root folder"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "var"), " file ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "="), " DriveApp", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "createFile"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), "response", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getBlob"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Set the file name same as the document name"), "\n file", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "setName"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), "document", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getName"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token operator" }), "+"), " ", mdx("span", _extends({ parentName: "code" }, { "className": "token string" }), "\".epub\""), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token comment" }), "// Return the file URL from Drive"), "\n ", mdx("span", _extends({ parentName: "code" }, { "className": "token keyword" }), "return"), " file", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "."), mdx("span", _extends({ parentName: "code" }, { "className": "token function" }), "getUrl"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "("), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ")"), mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), ";"), "\n", mdx("span", _extends({ parentName: "code" }, { "className": "token punctuation" }), "}")))), mdx("p", null, "We use the Google Drive Advanced Service of Apps Script to get get the export link for the EPUB MIME Type. Next, the ", mdx("code", _extends({ parentName: "p" }, { "className": "language-text" }), "URLFetchApp"), " service exports the Google Doc to the requested MIME type and returns the exported content as a blob."), mdx("p", null, "The blog is saved to Google Drive and the file URL is returned."), mdx("p", null, "A similar technique is used by ", mdx("a", _extends({ parentName: "p" }, { "href": "/topic/document-studio" }), "Document Studio"), " to convert merged documents into EPUB ebooks.")); } ; MDXContent.isMDXComponent = true;

source:https://ift.tt/2NrvWwO

Comments

Popular posts from this blog

The 101 Most Useful Websites

Compare MacBook Prices Worldwide with Google Sheets

Interesting Websites to Visit When You’re Bored