I volunteer a lot at stackoverflow.com and occassional answer question at Google Apps Script Community on Google +. I noticed that several people had a lot of problems getting an Envelope Printer working so I thought I'd give it a shot. I had the basics worked out in about 4 hours and now two days later I have a tool that will help me to send my clients their invoices and the end of the quarter. In this document I will provide you with a video of how to install the scripts which I will also provide to you free of any charge all of the scripts required to run the program.
There are two code files. One is Google Apps Script and the other is html. The google apps script file can be copied into the Code.gs file and the other file which must be copied into a new html file named "Envelope.html". By the way even though it looks like the spacing is wierd, you can just copy this code and paste it right into the Google Apps Script Editor and it works fine.
var DSSID=getDSSID();
function onOpen()
{
makeEnvelopeMenu()
}
function makeEnvelopeMenu()//Rename this to onOpen if this is all you have in this document
{
DocumentApp.getUi().createMenu('Envelope Tools')
.addItem('Show Envelope Sidebar', 'displayEnvelopeSidebar')
.addToUi();
displayEnvelopeSidebar();
}
function setupEnvelopeApp()//To make the connection between this document and the spreadsheet you created to store mailing addresses run this script from Script Editor
{
var resp=DocumentApp.getUi().prompt('Mailing Address SpreadsheetId', 'Create a spreadsheet to store mailing address and enter the id of that spreadsheet here', DocumentApp.getUi().ButtonSet.OK);
var dssid=resp.getResponseText();
var props=PropertiesService.getScriptProperties();
props.setProperty('DSSID', dssid);
}
function getDSSID()
{
return PropertiesService.getScriptProperties().getProperty('DSSID');
}
function setupDocument()
{
var doc=DocumentApp.getActiveDocument();
var envelope10={};
envelope10[DocumentApp.Attribute.PAGE_HEIGHT]=296;
envelope10[DocumentApp.Attribute.PAGE_WIDTH]=684;
envelope10[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
envelope10[DocumentApp.Attribute.FONT_SIZE] = 14;
envelope10[DocumentApp.Attribute.BOLD] = true;
envelope10[DocumentApp.Attribute.LINE_SPACING]=1;
doc.getBody().clear().setAttributes(envelope10);
}
function insertReturnAddress(retaddr)
{
var retaddr=(typeof(retaddr)!='undefined')?retaddr:'No return address selected.';
var retAddrStyle={};
retAddrStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
retAddrStyle[DocumentApp.Attribute.FONT_SIZE] = 14;
retAddrStyle[DocumentApp.Attribute.BOLD] = true;
retAddrStyle[DocumentApp.Attribute.LINE_SPACING]=1;
retAddrStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.LEFT;
var doc=DocumentApp.getActiveDocument();
doc.getBody().getChild(0).asParagraph().setAttributes(retAddrStyle).setText(retaddr);
}
function insertRecipientAddress(recaddr)
{
var recaddr=(typeof(recaddr)!='undefined')?recaddr:'No Recipient Address selected.';
var retAddrStyle={};
retAddrStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
retAddrStyle[DocumentApp.Attribute.FONT_SIZE] = 14;
retAddrStyle[DocumentApp.Attribute.BOLD] = true;
retAddrStyle[DocumentApp.Attribute.LINE_SPACING]=1;
retAddrStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.CENTER;
var doc=DocumentApp.getActiveDocument();
doc.getBody().appendParagraph('').setAttributes(retAddrStyle);
doc.getBody().appendParagraph('').setAttributes(retAddrStyle);
doc.getBody().appendParagraph('').setAttributes(retAddrStyle);
doc.getBody().appendParagraph('').setAttributes(retAddrStyle);
doc.getBody().appendParagraph(recaddr).setAttributes(retAddrStyle);
}
function prepareEnvelope(retaddr,recaddr)
{
setupDocument();
insertReturnAddress(retaddr);
insertRecipientAddress(recaddr)
}
function displayEnvelopeSidebar()
{
var userInterface=HtmlService.createHtmlOutputFromFile('Envelope').setWidth(300).setHeight(500).setTitle('Printing Envelopes');
DocumentApp.getUi().showSidebar(userInterface);
}
function savAddress(addr)
{
var ss=SpreadsheetApp.openById(DSSID);
var sh=ss.getSheetByName('Addresses');
if(sh.appendRow(addr))
{
return true;
}
else
{
return false;
}
}
function getAllAddresses()
{
var ss=SpreadsheetApp.openById(DSSID);
var sh=ss.getSheetByName('Addresses');
var rg=sh.getRange(2,1,sh.getLastRow(),sh.getLastColumn());
var vA=rg.getValues();
return vA;
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
clearAddressFields();
google.script.run
.withSuccessHandler(putNamesAndAddresses)
.getAllAddresses();
});
function putNamesAndAddresses(data)
{
var select1=document.getElementById("sel1");
var select2=document.getElementById("sel2");
var lf='\n';
select1.options.length=0;
select2.options.length=0;
for(var i=0;i<data.length;i++)
{
var name=data[i][0];
var addr='';
for(var j=0;j<data[i].length;j++)
{
if(j>0 && data[i][j]){addr+=lf;}
addr+=data[i][j];
}
select1.options[i]=new Option(name,addr);
select2.options[i]=new Option(name,addr);
}
}
function saveAddress()
{
$('.addrinp').css('background-color','#ffff00');
var line0=$('#adr0').val();
var line1=$('#adr1').val();
var line2=$('#adr2').val();
var line3=$('#adr3').val();
var addr=[line0,line1,line2,line3];
google.script.run
.withSuccessHandler(clearAddressFields)
.savAddress(addr)
}
function clearAddressFields()
{
$('.addrinp').css('background-color','#ffffff');
$('#adr0').val('');
$('#adr1').val('');
$('#adr2').val('');
$('#adr3').val('');
google.script.run
.withSuccessHandler(putNamesAndAddresses)
.getAllAddresses();
}
function prepareEnvelope()
{
var recaddr=$('#sel1').val();
var retaddr=$('#sel2').val();
google.script.run.prepareEnvelope(retaddr,recaddr);
}
console.log('My Code');
</script>
</head>
<body>
<div id="envprep" style="border-style: double;">
<h3>Recipient Address:</h3>
<select id="sel1" size="5">
</select>
<h3>Return Address:</h3>
<select id="sel2" size="5">
</select>
<br /><br /><input type="button" id="btn2" value="Address Envelope" onClick="prepareEnvelope();" />
</div>
<div id="newaddr" style="border-style: double;">
<h3>Add Address:</h3>
<input class="addrinp" id="adr0" type="text" size="30" placeholder="1st Line of Address" />
<br /><input class="addrinp" id="adr1" type="text" size="30" placeholder="2nd Line of Address" />
<br /><input class="addrinp" id="adr2" type="text" size="30" placeholder="3rd Line of Address" />
<br /><input class="addrinp" id="adr3" type="text" size="30" placeholder="4th Line of Address" />
<br /><br /><input id="btn1" type="button" value="Save Address" onClick="saveAddress();" />
</div>
<br /><br /><br /><input id="btn0" type="button" value="Close" onClick="google.script.host.close();" />
</body>
</html>