Saturday, 18 January 2020

Count the Number of Words and Characters in a Google Document

If you were to count the nubmer of words and characters in a Google Document, open the document, go to the Tools menu and choose Word Count. That’s a good option for counting words in a single document manually but what if you have a folder of files in Google Drive, say student assignments, and wish to know the words or characters per document.

That’s where Google Apps Script can help.

Go to Tools > Script Editor and paste the code to programmatically get the word count of any document in Google Document. You can either provide the document ID to the function or it will use the currently opened document.

function getWordCount(fileId) {
  const SEPARATOR = ' ';
  const document = fileId
    ? DocumentApp.openById(fileId)
    : DocumentApp.getActiveDocument();
  const text = document.getBody().getText();
  const words = text.replace(/\s+/g, SEPARATOR).split(SEPARATOR);
  const characters = words.join('');
  Logger.log("Word Count: " + words.length);
  Logger.log("Character Length: " + characters.length);
}

A more advanced version of the function uses regular expressions and it can work with Chinese, Japanese and Korean scripts - Credit.

function getWordCountCJK(data) {
  var pattern = /[a-zA-Z0-9_\u0392-\u03c9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
  var m = data.match(pattern);
  var count = 0;
  if( m === null ) return count;
  for (var i = 0; i < m.length; i++) {
    if (m[i].charCodeAt(0) >= 0x4E00) {
      count += m[i].length;
    } else {
      count += 1;
    }
  }
  return count;
}


function getWordCount(fileId) {
  const SEPARATOR = ' ';
  const document = fileId
    ? DocumentApp.openById(fileId)
    : DocumentApp.getActiveDocument();
  const text = document.getBody().getText();
  const count = getWordCountCJK(text);
  Logger.log("Word Count: " + count);
}


from Digital Inspiration https://ift.tt/30yUqJU

Tuesday, 14 January 2020

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;

from Digital Inspiration https://ift.tt/2NrvWwO