-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailMergeToDocs.js
101 lines (88 loc) · 3.79 KB
/
mailMergeToDocs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function mailMergeToDocs() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName(active_sheet);
//getting count of rows
var lastRow = ss.getLastRow();
var dataRange = ss.getRange(1, 1, lastRow, 3).getValues();
var rowsData = [];
dataRange.forEach(function(row){
var rowNumberTotal = Object.keys(rowsData).length;
rowsData.push([row[0],row[1],row[2]]);
});
for (var row in dataRange)
{
var length = Object.keys(rowsData).length;
}
//define header cell style which we will use while adding cells in header row
//Backgroud color, text bold, white
var headerStyle = {};
headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#59b300';
headerStyle[DocumentApp.Attribute.BOLD] = true;
headerStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
//Style for the cells other than header row
var cellStyleDark = {};
cellStyleDark[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ddefcc';
cellStyleDark[DocumentApp.Attribute.BOLD] = false;
cellStyleDark[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
var cellStyleLight = {};
cellStyleLight[DocumentApp.Attribute.BACKGROUND_COLOR] = '#eef7e5';
cellStyleLight[DocumentApp.Attribute.BOLD] = false;
cellStyleLight[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
//By default, each paragraph had space after, so we will change the paragraph style to add zero space
var paraStyle = {};
paraStyle[DocumentApp.Attribute.SPACING_AFTER] = 0;
paraStyle[DocumentApp.Attribute.LINE_SPACING] = 1;
//get the document
var doc = DocumentApp.openById(fchCommunicatorTemplateId); //or
//deletes the existing text of the file so that it can be a blank slate for this document merge
doc.setText('');
//get the body section of document
var body = doc.getBody();
body.setMarginLeft(50)
body.setMarginRight(50)
body.setMarginBottom(50)
body.setMarginTop(50)
//Add a table in document
var table = body.appendTable();
//Create 5 rows and 4 columns
for(var i=0; i<lastRow; i++){
var tr = table.appendTableRow();
//add 4 cells in each row
for(var j=0; j<3; j++){
//var td = tr.appendTableCell('Cell '+i+j);
//getRange needs to have numbers beginning with 1 not 0
var Team = ss.getRange(i+1,4).getValue();
var Name = ss.getRange(i+1,2).getValue();
//DocumentApp.getActiveDocument().getBody().insertImage(0, img);
var Message = ss.getRange(i+1,5).getValue();
if(j == 0) var td = tr.appendTableCell(Team);
if(j == 1) var td = tr.appendTableCell(Name);
if(j == 2) var td = tr.appendTableCell(Message);
if(i == 0 || j == 0) td.setAttributes(headerStyle);
else if (isOdd(i) == 1) td.setAttributes(cellStyleDark);
else td.setAttributes(cellStyleLight);
//Apply the para style to each paragraph in cell
var paraInCell = td.getChild(0).asParagraph();
paraInCell.setAttributes(paraStyle);
//Setting alignment
paraInCell.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
td.setVerticalAlignment(DocumentApp.VerticalAlignment.CENTER);
if( i > 0 && j == 2)
paraInCell.setAlignment(DocumentApp.HorizontalAlignment.LEFT);
}
}
//get the current date written out for title
var date = new Date(),
locale = "en-us",
month = date.toLocaleString(locale, { month: "short" });
var monthReal = month.match(/^\w+ \d+, \d+/g);
//removing stray double spaces //removing double commas (breaks importing pictures)
body.replaceText(" +", " ").replaceText("FCH Communicator dated:", "\t\tNewsletter for Acme Company");
//setting the width of columns
table.setColumnWidth(0,55)
table.setColumnWidth(1,80)
table.setColumnWidth(2,400)
table.setBorderColor('#ffffff');
//Save and close the document
doc.saveAndClose();
}