You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tested in complex document and it generated error alerts... Add "if null" corrected,
functionConvertGoogleDocToCleanHtml(){varbody=DocumentApp.getActiveDocument().getBody();varnumChildren=body.getNumChildren();varoutput=[];varimages=[];varlistCounters={};// Walk through all the child elements of the body.for(vari=0;i<numChildren;i++){varchild=body.getChild(i);output.push(processItem(child,listCounters,images));}varhtml=output.join('\r');emailHtml(html,images);//createDocumentForHtml(html, images);}functionemailHtml(html,images){varattachments=[];for(varj=0;j<images.length;j++){attachments.push({"fileName": images[j].name,"mimeType": images[j].type,"content": images[j].blob.getBytes()});}varinlineImages={};for(varj=0;j<images.length;j++){inlineImages[[images[j].name]]=images[j].blob;}varname=DocumentApp.getActiveDocument().getName()+".html";attachments.push({"fileName":name,"mimeType": "text/html","content": html});MailApp.sendEmail({to: Session.getActiveUser().getEmail(),subject: name,htmlBody: html,inlineImages: inlineImages,attachments: attachments});}functioncreateDocumentForHtml(html,images){varname=DocumentApp.getActiveDocument().getName()+".html";varnewDoc=DocumentApp.create(name);newDoc.getBody().setText(html);for(varj=0;j<images.length;j++)newDoc.getBody().appendImage(images[j].blob);newDoc.saveAndClose();}functiondumpAttributes(atts){// Log the paragraph attributes.for(varattinatts){Logger.log(att+":"+atts[att]);}}functionprocessItem(item,listCounters,images){varoutput=[];varprefix="",suffix="";if(item.getType()==DocumentApp.ElementType.PARAGRAPH){switch(item.getHeading()){// Add a # for each heading level. No break, so we accumulate the right number.caseDocumentApp.ParagraphHeading.HEADING6:
prefix="<h6>",suffix="</h6>";break;caseDocumentApp.ParagraphHeading.HEADING5:
prefix="<h5>",suffix="</h5>";break;caseDocumentApp.ParagraphHeading.HEADING4:
prefix="<h4>",suffix="</h4>";break;caseDocumentApp.ParagraphHeading.HEADING3:
prefix="<h3>",suffix="</h3>";break;caseDocumentApp.ParagraphHeading.HEADING2:
prefix="<h2>",suffix="</h2>";break;caseDocumentApp.ParagraphHeading.HEADING1:
prefix="<h1>",suffix="</h1>";break;default:
prefix="<p>",suffix="</p>";}if(item.getNumChildren()==0)return"";}elseif(item.getType()==DocumentApp.ElementType.INLINE_IMAGE){processImage(item,images,output);}elseif(item.getType()===DocumentApp.ElementType.LIST_ITEM){varlistItem=item;vargt=listItem.getGlyphType();varkey=listItem.getListId()+'.'+listItem.getNestingLevel();varcounter=listCounters[key]||0;// First list itemif(counter==0){// Bullet list (<ul>):if(gt===DocumentApp.GlyphType.BULLET||gt===DocumentApp.GlyphType.HOLLOW_BULLET||gt===DocumentApp.GlyphType.SQUARE_BULLET){prefix='<ul class="small"><li>',suffix="</li>";suffix+="</ul>";}else{// Ordered list (<ol>):prefix="<ol><li>",suffix="</li>";}}else{prefix="<li>";suffix="</li>";}if(item!==null&&(item.isAtDocumentEnd()||(item.getNextSibling()!==null&&item.getNextSibling().getType()!=DocumentApp.ElementType.LIST_ITEM))){if(gt===DocumentApp.GlyphType.BULLET||gt===DocumentApp.GlyphType.HOLLOW_BULLET||gt===DocumentApp.GlyphType.SQUARE_BULLET){suffix+="</ul>";}else{// Ordered list (<ol>):suffix+="</ol>";}}counter++;listCounters[key]=counter;}output.push(prefix);if(item!==null&&item.getType()==DocumentApp.ElementType.TEXT){processText(item,output);}else{if(item.getNumChildren){varnumChildren=item.getNumChildren();// Walk through all the child elements of the doc.for(vari=0;i<numChildren;i++){varchild=item.getChild(i);output.push(processItem(child,listCounters,images));}}}output.push(suffix);returnoutput.join('');}functionprocessText(item,output){vartext=item.getText();varindices=item.getTextAttributeIndices();if(indices.length<=1){// Assuming that a whole para fully italic is a quoteif(item.isBold()){output.push('<b>'+text+'</b>');}elseif(item.isItalic()){output.push('<blockquote>'+text+'</blockquote>');}elseif(text.trim().indexOf('http://')==0){output.push('<a href="'+text+'" rel="nofollow">'+text+'</a>');}else{output.push(text);}}else{for(vari=0;i<indices.length;i++){varpartAtts=item.getAttributes(indices[i]);varstartPos=indices[i];varendPos=i+1<indices.length ? indices[i+1]: text.length;varpartText=text.substring(startPos,endPos);Logger.log(partText);if(partAtts.ITALIC){output.push('<i>');}if(partAtts.BOLD){output.push('<b>');}if(partAtts.UNDERLINE){output.push('<u>');}// If someone has written [xxx] and made this whole text some special font, like superscript// then treat it as a reference and make it superscript.// Unfortunately in Google Docs, there's no way to detect superscriptif(partText.indexOf('[')==0&&partText[partText.length-1]==']'){output.push('<sup>'+partText+'</sup>');}elseif(partText.trim().indexOf('http://')==0){output.push('<a href="'+partText+'" rel="nofollow">'+partText+'</a>');}else{output.push(partText);}if(partAtts.ITALIC){output.push('</i>');}if(partAtts.BOLD){output.push('</b>');}if(partAtts.UNDERLINE){output.push('</u>');}}}}functionprocessImage(item,images,output){images=images||[];varblob=item.getBlob();varcontentType=blob.getContentType();varextension="";if(/\/png$/.test(contentType)){extension=".png";}elseif(/\/gif$/.test(contentType)){extension=".gif";}elseif(/\/jpe?g$/.test(contentType)){extension=".jpg";}else{throw"Unsupported image type: "+contentType;}varimagePrefix="Image_";varimageCounter=images.length;varname=imagePrefix+imageCounter+extension;imageCounter++;output.push('<img src="cid:'+name+'" />');images.push({"blob": blob,"type": contentType,"name": name});}
The text was updated successfully, but these errors were encountered:
Tested in complex document and it generated error alerts... Add "if null" corrected,
The text was updated successfully, but these errors were encountered: