Everytime I've shared this with someone I always left something out of it so I decided to keep it all in one place and this is the one place. It wouldn't fit on stackoverflow.com so I put it on mywebsite instead. Codes.gs function onOpen() { DocumentApp.getUi().createMenu('My Tools') .addItem('lookinInside','lookinInside') .addItem('highLightCurrentSelection','highLightCurrentSelection') .addItem('removeAllBlankLines','removeAllBlankLines') .addItem('addRowToTable','addRowToTable') .addItem('getBodyText','getBodyText') .addItem('getAllText','getAllText') .addItem('setTableCellText','setTableCellText') .addItem('setListItemText','setListItemText') .addSeparator() .addItem('loadParagraphSidebar','loadParagraphSidebar') .addItem('getRevisionHistory','getRevisionHistory') .addItem('Setup as Envelope #10', 'setupDocument') .addItem('Insert Return Address', 'insertReturnAddress') .addItem('Insert Recipient Address', 'insertRecipientAddress') .addToUi(); } function dispStuff(stuff,title,width,height) { var width=(typeof(width)!='undefined')?width:500; var height=(typeof(height)!='undefined')?height:500; var title=(typeof(title)!='undefined')?title:'Displaying Stuff'; DocumentApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(stuff).setWidth(width).setHeight(height), title); } function removeAllBlankLines() { var doc = DocumentApp.getActiveDocument(); var content = doc.getBody().editAsText().getText(); //Logger.log('typeof content: ' + typeof content) if (!content || content === " ") {return " "}; content = content.replace(/^\s*\n/gm,""); doc.getBody().setText(content); } function addRowToTable() { var doc = DocumentApp.getActiveDocument(); var body = doc.getBody(); var tablerow = body.getChild(11).asTable().appendTableRow(); var a=['Left Side','Right Side']; for(var i=0;i<2;i++) { tablerow.appendTableCell(a[i]); } } function getBodyText() { var doctext=DocumentApp.getActiveDocument().getBody().editAsText().getText(); var txt=HtmlService.createHtmlOutput(doctext); DocumentApp.getUi().showModelessDialog(txt, 'Body Text') } function getAllText() { var doc=DocumentApp.getActiveDocument(); var hdr=doc.getHeader().editAsText().getText(); var bdy=doc.getBody().editAsText().getText(); var ftr=doc.getFooter().editAsText().getText(); var html=HtmlService.createHtmlOutput(hdr + bdy + ftr); DocumentApp.getUi().showModelessDialog(html, 'Document Text') } function setTableCellText() { var doc=DocumentApp.getActiveDocument(); var resp=DocumentApp.getUi().prompt('Get childIndex,rowIndex,cellIndex and new text', 'Enter childIndex,rowIndex,cellIndex,newText' , DocumentApp.getUi().ButtonSet.OK); var a=resp.getResponseText().split(','); var childIndex=a[0]; var rowIndex=a[1]; var cellIndex=a[2]; var newText=a[3]; doc.getBody().getChild(childIndex).asTable().getCell(rowIndex, cellIndex).editAsText().setText(newText); } function setListItemText() { var doc=DocumentApp.getActiveDocument(); var resp=DocumentApp.getUi().prompt('childIndex,newText', 'Enter childIndex,New Text', DocumentApp.getUi().ButtonSet.OK) var a=resp.getResponseText().split(','); var childIndex=a[0]; var newText=a[1]; doc.getBody().getChild(childIndex).asListItem().editAsText().setText(newText); } function idImageTest() { idImage(0,true); } function idImage(idx, up) { var up=(typeof(up)!='undefined')?up:true; var doc=DocumentApp.getActiveDocument(); var images=doc.getBody().getImages(); var img=images[idx]; var parent=img.getParent(); var position=doc.newPosition(parent, 0); doc.setCursor(position); var w=img.getWidth(); var h=img.getHeight(); if(up) { img.setWidth(w/2); img.setHeight(h/2); } else { Utilities.sleep(1000); img.setWidth(w); img.setHeight(h); } return idx; } function highLightBodyChildText(idx) { var doc=DocumentApp.getActiveDocument(); var child=doc.getBody().getChild(idx); var position=doc.newPosition(child, 0); doc.setCursor(position); child.asText().editAsText().setBackgroundColor('#ffff00'); return idx; } function highLightHeaderChildText(idx) { var doc=DocumentApp.getActiveDocument(); var child=doc.getHeader().getChild(idx); var position=doc.newPosition(child, 0); doc.setCursor(position); child.asText().editAsText().setBackgroundColor('#ffff00'); return idx; } function lowLightBodyChildText(idx) { Utilities.sleep(1000); DocumentApp.getActiveDocument().getBody().getChild(idx).asText().editAsText().setBackgroundColor('#ffffff'); } function lowLightHeaderChildText(idx) { Utilities.sleep(1000); DocumentApp.getActiveDocument().getHeader().getChild(idx).asText().editAsText().setBackgroundColor('#ffffff'); } function saveChildTableCellChanges(idx,rowidx,cellidx,text) { var doc=DocumentApp.getActiveDocument(); var child=doc.getBody().getChild(idx); child.asTable().getRow(rowidx).getCell(cellidx).editAsText().setText(text); } function testnext() { setChildAttribute('FOREGROUND_COLOR',0,'#ff0000') } function setChildAttribute(key,idx,text){ var doc=DocumentApp.getActiveDocument(); var child=doc.getBody().getChild(idx); var type=child.getType(); switch(type) { case DocumentApp.ElementType.PARAGRAPH: var childattrs=child.asParagraph().getAttributes(); break; case DocumentApp.ElementType.LIST_ITEM: var childattrs=child.asListItem().getAttributes(); break; case DocumentApp.ElementType.TEXT: var childattrs=child.asText().getAttributes(); break; case DocumentApp.ElementType.TABLE: var childattrs=child.asTable().getAttributes(); break; case DocumentApp.ElementType.TABLE_CELL: var childattrs=child.asTableCell().getAttributes(); break; default: var childattrs=child.asText().getAttributes(); } childattrs[key]=text; switch(type) { case DocumentApp.ElementType.PARAGRAPH: child.asParagraph().setAttributes(childattrs); break; case DocumentApp.ElementType.LIST_ITEM: child.asListItem().setAttributes(childattrs); break; case DocumentApp.ElementType.TEXT: child.asText().setAttributes(childattrs); break; case DocumentApp.ElementType.TABLE: child.asTable().setAttributes(childattrs); break; case DocumentApp.ElementType.TABLE_CELL: child.asTableCell().setAttributes(childattrs); break; default: child.asText().setAttributes(childattrs); } var id='#childAttr' + key + idx; var a=[]; a.push(id); a.push(text); a.push(type); return a; } function setThisElementAttribute(key,idx,rowidx,cellidx,text) { var doc=DocumentApp.getActiveDocument(); var child=doc.getBody().getChild(idx); var cellAttrs=child.asTable().getRow(rowidx).getCell(cellidx).editAsText().getAttributes(); cellAttrs[key]=text; child.asTable().getRow(rowidx).getCell(cellidx).editAsText().setAttributes(cellAttrs); var id='#Attr' + key + idx + rowidx + cellidx; var a=[]; a.push(id); a.push(text); return a; } function setSelectedElementAttribute(key, idx, text) { var doc=DocumentApp.getActiveDocument(); var selection=doc.getSelection(); var selectedElements=selection.getRangeElements(); var el=selectedElements[idx].getElement(); var eltype=el.getType(); switch(eltype) { case DocumentApp.ElementType.TEXT: var elattrs=el.asText().getAttributes(); break; default: var elattrs=el.asText().getAttributes(); break; } elattrs[key]=text; switch(eltype) { case DocumentApp.ElementType.TEXT: el.asText().setAttributes(elattrs); break; default: el.asText().setAttributes(elattrs); break; } var id='#elattr' + key + idx; var a=[]; a.push(id); a.push(text); a.push(eltype); return a; } function loadFile(filename,folderID) { var filename = (typeof(filename) !== 'undefined')? filename : 'SampleData'; var folderID = (typeof(folderID) !== 'undefined')? folderID : '0Bx2ds1-a4A5nMS1GRHh6N3NEQXc'; var fldr = DriveApp.getFolderById(folderID); var file = fldr.getFilesByName(filename); var s = ''; while(file.hasNext()) { var fi = file.next(); var target = fi.getName(); if(target == filename) { s = fi.getBlob().getDataAsString(); } } return s; } function getRevisionHistory() { var s=''; var br='
'; var doc=DocumentApp.getActiveDocument(); var id=doc.getId(); var revisions=Drive.Revisions.list(id); if (revisions.items && revisions.items.length > 0) { for (var i = 0; i < revisions.items.length; i++) { var revision = revisions.items[i]; var date = new Date(revision.modifiedDate); s+=br + Utilities.formatString('ID: %s, Date: %s Link', revision.id, date.toLocaleString(), revision.selfLink); //Logger.log('ID: %s, Date: %s', revision.id, date.toLocaleString()); } } else { //Logger.log('No revisions found.'); s+=br + 'No revisions found.'; } if(s) { DocumentApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(s), 'Document History for ' + doc.getName()); } } lookinInside.gs: function lookinInside() { var br = '
'; var s=''; var doc = DocumentApp.getActiveDocument(); var header = doc.getHeader(); if(header) { s+=br + '

Document Header

'; var headerchildren = header.getNumChildren(); if(headerchildren) { var headerattrs = header.getAttributes(); s+=br + 'Header Attributes'; s+=br + 'Show/Hide'; var headertxt=header.editAsText().getText(); var headertype=header.getType(); if(headertxt) { s+=br + 'Header Type:' + headertype; s+=br + 'Header Text:'; s+=br + '' + headertxt + ''; s+=br + 'Text Length:' + headertxt.length + ''; } else { s+=br + 'Header Type: ' + headertype; s+=br + 'No Header Text for this document.'; } s+=br + 'Number of Children = ' + headerchildren + ''; s+=br + 'Header Children Types'; s+='
'; for(var i=0;i' + 'Child[' + i + ']= ' + type + ''; s+=br + '' + 'Text = ' + '' + '' + childtxt + ''; s+=br + '' + 'Text Length = ' + childtxt.length + ''; s+=br + ''; } else { s+=br + '' + 'Child[' + i + ']= ' + type + ''; s+=br + 'No Header Child[' + i + '] text.'} var childattrs = child.getAttributes(); if(childattrs) { s+= br + 'Child[' + i + '] Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No Header Child[' + i + '] attributes.';} s+='
'; }else{s+=br + 'No ' + i + 'th Header Child';} } }else{s+=br + 'No Header Children in this header.';} }else{s+=br + '

No Header Element in this document

';} var body = doc.getBody(); if(body) { s+=br + '

Document Body

'; var bodychldrn = body.getNumChildren(); if(bodychldrn) { var bodyattrs = body.getAttributes(); if(bodyattrs) { s+=br + 'Body Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No Body Attributes';} var bodytxt = body.editAsText().getText(); if(bodytxt) { s+=br + 'Body Text:'; s+=br + '' + body.editAsText().getText() + ''; s+=br + 'Text Length = ' + body.editAsText().getText().length; s+=br + 'Number of Children = ' + bodychldrn + ''; s+=br + 'Body Children Types'; }else{s+=br + 'No Body Text';} s+='
'; for(var i=0;i' + 'Child[' + i + ']= ' + type + ''; s+=br + '' + 'Text = ' + '' + '' + childtxt + ''; s+=br + '' + 'Text Length = ' + childtxt.length + ''; s+=br + ''; } else { s+=br + '' + 'Child[' + i + ']= ' + type + ''; s+=br + 'No text in body child[' + i + '].'; } switch(type) { case DocumentApp.ElementType.PARAGRAPH: var childattrs=child.asParagraph().getAttributes(); break; case DocumentApp.ElementType.LIST_ITEM: var childattrs=child.asListItem().getAttributes(); break; case DocumentApp.ElementType.TEXT: var childattrs=child.asText().getAttributes(); break; case DocumentApp.ElementType.TABLE: var childattrs=child.asTable().getAttributes(); break; case DocumentApp.ElementType.TABLE_CELL: var childattrs=child.asTableCell().getAttributes(); break; default: var childattrs=child.asText().getAttributes(); } if(childattrs) { s+= br + 'Child[' + i + '] Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No child attributes for child[' + i + '].';} s+='
'; if(type==DocumentApp.ElementType.TABLE) { var numrows = child.asTable().getNumRows(); for(var j=0;jChild[' + i + '] ' + 'Row[' + j + ']' + ' Cell[' + k + ']' + ' Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No child attributes for cell' + Number(i) + Number(j) + Number(k) + '.';} //s+=br + 'Cell[' + j + '][' + k + ']= ' + '' + celltxt + ''; s+=br + 'Cell[' + j + '][' + k + '] ' + ''; s+=br + ''; s+='
'; } } s+='
'; } }else{s+=br + 'No ' + i + 'th Child in this document body.';} } }else{s+=br + 'No Body Children in this document.';} var images=body.getImages(); if(images) { s+=br + 'Body Images' + br + 'Number of Images: ' + images.length; s+='
'; for(var i=0;iImage[' + i + ']: Attributes'; for(var key in imgattrs) { s+=br + '' + key + '= ' + imgattrs[key]; } }else{s+=br + 'No image attributes for Image[' + i + ']';} s+=br + ''; s+='
'; } }else{s+=br + 'No Images in this Body element.';} }else{s+=br + 'No body in this document.';} var footer = doc.getFooter(); if(footer) { s+=br + '

Document Footer

'; var footerattrs = footer.getAttributes(); if(footerattrs) { s+=br + 'Footer Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No footer attributes for the footer in this document.';} var footertxt=footer.editAsText().getText(); var footertype=footer.getType(); if(footertxt) { s+=br + 'Footer Type: ' + footertype; s+=br + 'Footer Text: '; s+=br + '' + footertxt + ''; } else { s+=br + 'Footer Type: ' + footertype; s+=br + 'No Footer Text'; } var footerchildren = footer.getNumChildren(); s+='
'; if(footerchildren) { s+=br + 'Number of Children = ' + footerchildren + ''; s+=br + 'Footer Children Types'; for(var i=0;i' + 'Child[' + i + ']= ' + type + '
'; s+=br + '' + 'Text = ' + '' + '' + childtxt + ''; s+=br + '' + 'Text Length = ' + childtxt.length + ''; } else { s+=br + '' + 'Child[' + i + ']= ' + type + ''; s+='No Footer Child[' + i + '] text.'; } var childattrs = child.getAttributes(); if(childattrs) { s+= br + 'Child[' + i + '] Attributes'; s+=br + 'Show/Hide'; }else{s+=br + 'No Footer Child[' + i + '] attributes.';} s+='
'; } }else{s+=br + 'No Footer children for the footer in this document.';} }else{s+=br + '

No Footer Element in this Document

';} s+=''; var html=HtmlService.createHtmlOutputFromFile('htmlToBody').setTitle('Looking Inside').setWidth(500).setHeight(450); DocumentApp.getUi().showSidebar(html.append(s)); } Highlight.gs: function highLightCurrentSelection() { var conclusionStyle = {}; conclusionStyle[DocumentApp.Attribute.BACKGROUND_COLOR]='#ffffff'; conclusionStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000'; conclusionStyle[DocumentApp.Attribute.FONT_FAMILY]='Calibri'; conclusionStyle[DocumentApp.Attribute.FONT_SIZE]=20; conclusionStyle[DocumentApp.Attribute.BOLD]=false; conclusionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.LEFT; conclusionStyle[DocumentApp.Attribute.VERTICAL_ALIGNMENT]=DocumentApp.VerticalAlignment.BOTTOM; conclusionStyle[DocumentApp.Attribute.LINE_SPACING]=1.5; conclusionStyle[DocumentApp.Attribute.HEIGHT]=2; conclusionStyle[DocumentApp.Attribute.LEFT_TO_RIGHT]=true; var br = '
'; var selection = DocumentApp.getActiveDocument().getSelection(); var s=''; if(selection) { s+=br + 'Elements in Current Selection'; var selectedElements = selection.getRangeElements(); for(var i=0;iTEXT'; s+=br + 'Text:' + txt + ''; s+=br + 'Length: ' + txt.length; s+=br + 'Show/Hide'; } } if(elType==DocumentApp.ElementType.PARAGRAPH) { var txt = selElem.getElement().asParagraph().getText(); var elattrs = el.getAttributes(); if(elattrs) { s+=br + 'PARAGRAPH Attributes'; s+=br + 'Text:' + txt + ' Text Length= ' + txt.length; for(var key in elattrs) { s+= br + key + ' = ' + elattrs[key]; } } } s+='
'; } var finalP=DocumentApp.getActiveDocument().getBody().appendParagraph('Total Number of Elements: ' + Number(selectedElements.length)); finalP.setAttributes(conclusionStyle); } else { s+= br + 'No Elements found in current selection'; } s+=''; //s+=''; DocumentApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('htmlToBody').append(s).setWidth(800).setHeight(450).setTitle('Selected Elements')); } temp.gs function dispImage() { var html=HtmlService.createHtmlOutputFromFile('image'); DocumentApp.getUi().showModelessDialog(html, 'Display Image') } image.html htmlToBody.html test.gs function findMyTables() { var doc=DocumentApp.getActiveDocument(); var body=doc.getBody(); var br='
'; var s=''; if(body) { var numChildren=body.getNumChildren(); for(var i=0;iCell[' + j + '][' + k + ']= ' + '' + celltxt + ''; } } } } } var html=HtmlService.createHtmlOutput(s).setWidth(600).setHeight(450); DocumentApp.getUi().showModelessDialog(html, 'My Tables') } style.gs var conclusionStyle = {}; conclusionStyle[DocumentApp.Attribute.BACKGROUND_COLOR]='#ffffff'; conclusionStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000'; conclusionStyle[DocumentApp.Attribute.FONT_FAMILY]='Calibri'; conclusionStyle[DocumentApp.Attribute.FONT_SIZE]=20; conclusionStyle[DocumentApp.Attribute.BOLD]=false; conclusionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.LEFT; conclusionStyle[DocumentApp.Attribute.VERTICAL_ALIGNMENT]=DocumentApp.VerticalAlignment.BOTTOM; conclusionStyle[DocumentApp.Attribute.LINE_SPACING]=1.5; conclusionStyle[DocumentApp.Attribute.HEIGHT]=2; conclusionStyle[DocumentApp.Attribute.LEFT_TO_RIGHT]=true; InsertParagraph.html
Index:
Text:

insertParagraph.gs function putParagraph(paraObject) { var text=paraObject.text; var index=paraObject.childIndex; if(text && index) { DocumentApp.getActiveDocument().getBody().insertParagraph(index, text); return true; } else { DocumentApp.getUi().alert('Either the paragraph contents or the child Index was missing'); return false; } } function loadParagraphSidebar() { var html=HtmlService.createHtmlOutputFromFile('InsertParagraph').setTitle('Insert Paragraph'); DocumentApp.getUi().showSidebar(html); } function getNumChildren() { return DocumentApp.getActiveDocument().getBody().getNumChildren(); }