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 + '