forked from DidierStevens/DidierStevensSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailmerge.js
112 lines (95 loc) · 3.18 KB
/
Mailmerge.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
102
103
104
105
106
107
108
109
110
111
112
/*
Substract.js version 0.0.1
UltraEdit script to mailmerge
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2014/08/08: start
2014/08/09: added input validation
Todo:
-
*/
function TestInput(sInput, iCountOpenFiles)
{
return sInput == "" || isNaN(sInput) || +sInput < 0 || +sInput >= iCountOpenFiles;
}
function replaceAll(sData, sFind, sReplace)
{
return sData.replace(new RegExp(sFind, 'g'), sReplace);
}
function GetDocument(documentIndex)
{
UltraEdit.document[documentIndex].selectAll();
return UltraEdit.document[documentIndex].selection;
}
function Document2Dictionary(documentIndex)
{
var dDoc = [];
UltraEdit.document[documentIndex].gotoLine(1, 1);
for (var iIter = 1; !UltraEdit.document[documentIndex].isEof(); iIter++)
{
UltraEdit.document[documentIndex].gotoLine(iIter, 1);
UltraEdit.document[documentIndex].selectLine();
var sCurrentLine = UltraEdit.document[documentIndex].selection;
dDoc[sCurrentLine] = 1;
}
return dDoc;
}
function GenerateDocument(sLine, sTemplate, sPath)
{
var aItems = sLine.trim().split("\t");
if (aItems.length > 1)
{
var sDocument = sTemplate;
for (var iIter = 1; iIter < aItems.length; iIter++)
sDocument = replaceAll(sDocument, "{TEMPLATEVARIABLE:" + iIter + "}", aItems[iIter]);
UltraEdit.newFile();
UltraEdit.activeDocument.write(sDocument);
UltraEdit.saveAs(sPath + aItems[0]);
}
}
function Main()
{
var sMessageHelp = "The template document must contain {TEMPLATEVARIABLE:#} where # is a number.\nThe list document must contain records with fields separated by tabs.\nThe first field is the name of the file, the following fields are the template variables.\n{TEMPLATEVARIABLE:1} is the second field, {TEMPLATEVARIABLE:2} is the third field, ...";
var sMessageTitle = "Mailmerge";
var sMessageNumber = "Please enter a valid document number!";
var iCountOpenFiles = UltraEdit.document.length;
if (iCountOpenFiles < 2)
{
UltraEdit.messageBox("At least 2 files need to be open", sMessageTitle);
return;
}
UltraEdit.outputWindow.clear();
UltraEdit.outputWindow.showWindow(true);
for (var iIter = 0; iIter < UltraEdit.document.length; iIter++)
UltraEdit.outputWindow.write(iIter + ": " + UltraEdit.document[iIter].path);
var sDocTemplate = UltraEdit.getString("Select template document (0-" + (iCountOpenFiles - 1) + ", see Output Window),\ntype h for help.", 1);
if (sDocTemplate == "h")
{
UltraEdit.messageBox(sMessageHelp, sMessageTitle);
return;
}
if (TestInput(sDocTemplate, iCountOpenFiles))
{
UltraEdit.messageBox(sMessageNumber, sMessageTitle);
return;
}
var sDocList = UltraEdit.getString("Select list document (0-" + (iCountOpenFiles - 1) + ", see Output Window),\ntype h for help.", 1);
if (sDocList == "h")
{
UltraEdit.messageBox(sMessageHelp, sMessageTitle);
return;
}
if (TestInput(sDocList, iCountOpenFiles))
{
UltraEdit.messageBox(sMessageNumber, sMessageTitle);
return;
}
var sFilename = UltraEdit.document[+sDocTemplate].path;
var sTemplate = GetDocument(+sDocTemplate);
var dDocList = Document2Dictionary(+sDocList);
for (sLine in dDocList)
GenerateDocument(sLine, sTemplate, sFilename.replace(/[^\\]*$/, ''));
}
Main();