Guest User

Untitled

a guest
Jan 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. function ConvertGoogleDocToCleanHtml() {
  2. var body = DocumentApp.getActiveDocument().getBody();
  3. var numChildren = body.getNumChildren();
  4. var output = [];
  5. var images = [];
  6. var listCounters = {};
  7.  
  8. // Walk through all the child elements of the body.
  9. for (var i = 0; i < numChildren; i++) {
  10. var child = body.getChild(i);
  11.  
  12. output.push( processItem(child, listCounters, images).replace('<li><p>', '<li>').replace('</p></li>', '</li>') );
  13. }
  14.  
  15.  
  16. var html = output.join('\r');
  17. emailHtml(html, images);
  18. //createDocumentForHtml(html, images);
  19. }
  20.  
  21. function emailHtml(html, images) {
  22. var attachments = [];
  23. var j;
  24.  
  25. for (j = 0; j<images.length; j++) {
  26. attachments.push( {
  27. "fileName": images[j].name,
  28. "mimeType": images[j].type,
  29. "content": images[j].blob.getBytes()
  30. } );
  31. }
  32.  
  33. var inlineImages = {};
  34. for (j = 0; j<images.length; j++) {
  35. inlineImages[[images[j].name]] = images[j].blob;
  36. }
  37.  
  38. var name = DocumentApp.getActiveDocument().getName()+".html";
  39. attachments.push({"fileName":name, "mimeType": "text/html", "content": html});
  40. MailApp.sendEmail({
  41. to: Session.getActiveUser().getEmail(),
  42. subject: name,
  43. htmlBody: html,
  44. inlineImages: inlineImages,
  45. attachments: attachments
  46. });
  47. }
  48.  
  49. function createDocumentForHtml(html, images) {
  50. var name = DocumentApp.getActiveDocument().getName()+".html";
  51. var newDoc = DocumentApp.create(name);
  52. newDoc.getBody().setText(html);
  53. for(var j=0; j < images.length; j++)
  54. newDoc.getBody().appendImage(images[j].blob);
  55. newDoc.saveAndClose();
  56. }
  57.  
  58. function dumpAttributes(atts) {
  59. // Log the paragraph attributes.
  60. for (var att in atts) {
  61. Logger.log(att + ":" + atts[att]);
  62. }
  63. }
  64.  
  65. function processItem(item, listCounters, images) {
  66. var output = [];
  67. var prefix = "", suffix = "";
  68.  
  69.  
  70.  
  71. if (item.getType() == DocumentApp.ElementType.PARAGRAPH) {
  72.  
  73. if (item.getNumChildren() === 0) {
  74. return "";
  75. }
  76.  
  77. var isHeading = item.getHeading();
  78.  
  79. if (isHeading == DocumentApp.ParagraphHeading.HEADING6) {
  80. prefix = "<h6>";
  81. suffix = "</h6>";
  82. }
  83. else if (isHeading == DocumentApp.ParagraphHeading.HEADING5) {
  84. prefix = "<h5>";
  85. suffix = "</h5>";
  86. }
  87. else if (isHeading == DocumentApp.ParagraphHeading.HEADING4) {
  88. prefix = "<h4>";
  89. suffix = "</h4>";
  90. }
  91. else if (isHeading == DocumentApp.ParagraphHeading.HEADING3) {
  92. prefix = "<h3>";
  93. suffix = "</h3>";
  94. }
  95. else if (isHeading == DocumentApp.ParagraphHeading.HEADING2) {
  96. prefix = "<h2>";
  97. suffix = "</h2>";
  98. }
  99. else if (isHeading == DocumentApp.ParagraphHeading.HEADING1) {
  100. prefix = "<h1>";
  101. suffix = "</h1>";
  102. }
  103. }
  104.  
  105. if (item.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
  106. processImage(item, images, output);
  107. }
  108.  
  109. if (item.getType() === DocumentApp.ElementType.LIST_ITEM) {
  110. var listItem = item;
  111. var gt = listItem.getGlyphType();
  112. var key = listItem.getListId() + '.' + listItem.getNestingLevel();
  113. var counter = listCounters[key] || 0;
  114.  
  115. // First list item
  116. if ( counter === 0 ) {
  117. if (gt === DocumentApp.GlyphType.BULLET || gt === DocumentApp.GlyphType.HOLLOW_BULLET || gt === DocumentApp.GlyphType.SQUARE_BULLET) {
  118. prefix = '[ul]\n<li>';
  119. suffix = "</li>";
  120. }
  121. else {
  122. prefix = "<ol>\n<li>";
  123. suffix = "</li>";
  124. }
  125. }
  126. else {
  127. prefix = "<li>";
  128. suffix = "</li>";
  129. }
  130.  
  131. if (item.isAtDocumentEnd() || (item.getNextSibling() && (item.getNextSibling().getType() != DocumentApp.ElementType.LIST_ITEM))) {
  132. if (gt === DocumentApp.GlyphType.BULLET || gt === DocumentApp.GlyphType.HOLLOW_BULLET || gt === DocumentApp.GlyphType.SQUARE_BULLET) {
  133. suffix += "\n[/ul]";
  134. }
  135. else {
  136. suffix += "\n</ol>";
  137. }
  138.  
  139. }
  140.  
  141. counter++;
  142. listCounters[key] = counter;
  143. }
  144.  
  145. if (item.getType() == DocumentApp.ElementType.TEXT) {
  146. var text = item.getText();
  147. if (text.indexOf('[h6]') === 0) {
  148. prefix = "<h6>";
  149. suffix = "</h6>";
  150. }
  151. else if (text.indexOf('[h5]') === 0) {
  152. prefix = "<h5>";
  153. suffix = "</h5>";
  154. }
  155. else if (text.indexOf('[h4]') === 0) {
  156. prefix = "<h4>";
  157. suffix = "</h4>";
  158. }
  159. else if (text.indexOf('[h3]') === 0) {
  160. prefix = "<h3>";
  161. suffix = "</h3>";
  162. }
  163. else if (text.indexOf('[h2]') === 0) {
  164. prefix = "<h2>";
  165. suffix = "</h2>";
  166. }
  167. else if (text.indexOf('[h1]') === 0) {
  168. prefix = "<h1>";
  169. suffix = "</h1>";
  170. } else {
  171. prefix = "<p>";
  172. suffix = "</p>";
  173. }
  174. }
  175.  
  176. output.push(prefix);
  177.  
  178. if (item.getType() == DocumentApp.ElementType.TEXT) {
  179. processText(item, output, prefix);
  180. }
  181. if (item.getNumChildren) {
  182. var numChildren = item.getNumChildren();
  183.  
  184. // Walk through all the child elements of the doc.
  185. for (var i = 0; i < numChildren; i++) {
  186. var child = item.getChild(i);
  187. output.push(processItem(child, listCounters, images));
  188. }
  189. }
  190.  
  191. output.push(suffix);
  192. return output.join('');
  193. }
  194.  
  195.  
  196. function processText(item, output) {
  197. var text = item.getText();
  198.  
  199. text = text.replace(/\[h\d\]/, '').trim();
  200.  
  201. output.push(text);
  202. }
  203.  
  204. function processImage(item, images, output) {
  205.  
  206. images = images || [];
  207. var blob = item.getBlob();
  208. var contentType = blob.getContentType();
  209. //var extension = "";
  210. var extension = contentType.split('.').pop();
  211. //
  212. // if (/\/png$/.test(contentType)) {
  213. // extension = ".png";
  214. // } else if (/\/gif$/.test(contentType)) {
  215. // extension = ".gif";
  216. // } else if (/\/jpe?g$/.test(contentType)) {
  217. // extension = ".jpg";
  218. // } else {
  219. // throw "Unsupported image type: " + contentType;
  220. // }
  221.  
  222. var imagePrefix = "Image_";
  223. var imageCounter = images.length;
  224. var name = imagePrefix + imageCounter + extension;
  225. imageCounter++;
  226.  
  227. output.push('<img src="cid:' + name + '" />');
  228. images.push( {
  229. "blob": blob,
  230. "type": contentType,
  231. "name": name
  232. } );
  233. }
Add Comment
Please, Sign In to add comment