RingDotNetfor Ring

A project: mail merge

A mail merge makes many letters from one template and a table of data. This project finds the {fields} of a template with a regular expression, replaces each one through a callback that .NET calls, formats numbers and dates, and reports missing data before any letter is made.

What you will learn#

The same pattern makes e-mails, invoices and reports: a template, data, and a function that fills the holes.

The program#

samples/UsingDotNet/tutorials/ch02_mailmerge.ring
# a project: mail merge - a template with {fields}, rows of data, Regex.Replace with a callback, formats, missing fields and the letters
load "dotnet.ring"

using {
	"System"
	"System.Globalization"
	"System.Text.RegularExpressions"
}

aFields = []                     # the data of the row that is being merged (the callback reads it)
aMissing = []                    # the names of the fields that a row did not have

func main

	# A MAIL MERGE makes many letters from one template and a table of data. The template has FIELDS in braces: {name} is replaced by the
	# value of the column name, and {amount:N2} is replaced by the value written with a format (the tutorial about formatting).
	cTemplate = "Dear {name}," + nl + "Your order {order} of {amount:N2} is ready." + nl + "It will be sent to {city} on {date:d MMMM yyyy}." + nl + "Thank you, {shop}"

	# The data is a CSV text: the first line has the names of the columns, and each other line is one letter. (A real program reads a file;
	# this text is the same as the content of such a file.)
	cCsv = "name,order,amount,city,date" + nl + "Ann,A-101,1234.5,Cairo,2030-03-09" + nl + "Bob,A-102,80,Oslo,2030-03-10" + nl + "Cy,A-103,19.99,,2030-03-11"
	aRows = readCsv(cCsv)

	? "1. the template has these fields: " + joinList(fieldNames(cTemplate))
	? "   the data has " + len(aRows) + " rows and the columns: " + joinList(columnNames(cCsv))

	# 2. One letter: Regex.Replace finds each {field} and asks a function (a CALLBACK) for the text that replaces it
	? nl + "2. the first letter:"
	? indent(merge(cTemplate,aRows[1]))

	# 3. All the letters
	? nl + "3. all the letters, the first line of each:"
	for i = 1 to len(aRows)
		cLetter = merge(cTemplate,aRows[i])
		? "   " + i + ". " + first(cLetter)
	next

	# 4. Missing data. The third row has no city, and the template has {shop}, which is not a column. A letter with a hole must not be
	#    sent, so the program collects the problems and reports them before any letter is written.
	? nl + "4. check before sending:"
	for i = 1 to len(aRows)
		aMissing = []
		merge(cTemplate,aRows[i])
		if len(aMissing) > 0
			? "   row " + i + " (" + aRows[i][:name] + "): missing " + joinList(aMissing)
		ok
	next

	# 5. Add the column shop to every row and give Cy a city: now every letter is complete
	aRows[3][:city] = "Giza"
	? nl + "5. after the repair:"
	nProblems = 0
	for i = 1 to len(aRows)
		aRows[i] + [ "shop","The Tea House" ]                # a new column: a new pair
		aMissing = []
		merge(cTemplate,aRows[i])
		nProblems += len(aMissing)
	next
	? "   problems: " + nProblems
	? indent(merge(cTemplate,aRows[3]))

# the names of the fields in a template: the first part of each {name} or {name:format}
func fieldNames cTemplate

	aNames = []
	aMatches = (new Regex).Matches([ :input = cTemplate, :pattern = cPattern() ]).toList()
	for oMatch in aMatches
		aNames + oMatch.Groups.Item(1).Value
	next
	return aNames

# the pattern of a field: { a name, and then perhaps a colon and a format } . The name is in group 1 and the format in group 2.
func cPattern

	return "\{(\w+)(?::([^}]+))?\}"

# the letter of one row: every field is replaced by what the callback returns
func merge cTemplate,aRow

	aFields = aRow
	return (new Regex).Replace([ :input = cTemplate, :pattern = cPattern(), :evaluator = dnCallback("replaceField") ])

# The callback. A callback that is the NAME of a function gets the arguments as a list: aArgs[1] is the Match. Group 1 is the name of the
# field and group 2 the format (empty when there is none). The text that the function RETURNS replaces the field in the letter.
func replaceField aArgs

	oMatch = aArgs[1]
	cName = oMatch.Groups.Item(1).Value
	cFormat = oMatch.Groups.Item(2).Value
	if find(aFields,cName,1) = 0 or trim(aFields[cName]) = ""
		aMissing + cName
		return "[" + cName + "?]"                           # a visible mark in the letter
	ok
	cValue = aFields[cName]
	if cFormat != ""
		return formatValue(cValue,cFormat)
	ok
	return cValue

# a value with a format: a number with N2, a date with d MMMM yyyy (the invariant culture: the same text on every computer)
func formatValue cValue,cFormat

	oInvariant = new CultureInfo("en-US").InvariantCulture
	if isDate(cValue)
		oDate = (new DateTime).Parse([ :s = cValue, :provider = oInvariant ])
		return oDate.Format([ :format = cFormat, :provider = oInvariant ])
	ok
	return new String().Format([ :provider = oInvariant, :format = "{0:" + cFormat + "}", :arg0 = 0 + cValue ])

func isDate cText

	return len(cText) = 10 and substr(cText,5,1) = "-" and substr(cText,8,1) = "-"

# a CSV text as a list of rows; each row is a list of [name, value] pairs, found by the name of the column
func readCsv cCsv

	aLines = str2list(substr(cCsv,char(13),""))
	aNames = splitKeep(aLines[1])
	aRows = []
	for i = 2 to len(aLines)
		if trim(aLines[i]) = ""
			loop
		ok
		aValues = splitKeep(aLines[i])
		aRow = []
		for j = 1 to len(aNames)
			aRow + [ aNames[j], aValues[j] ]
		next
		aRows + aRow
	next
	return aRows

# split a line at the commas and keep the empty values
func splitKeep cLine

	aParts = []
	cPart = ""
	for i = 1 to len(cLine)
		if substr(cLine,i,1) = ","
			aParts + cPart
			cPart = ""
		else
			cPart += substr(cLine,i,1)
		ok
	next
	aParts + cPart
	return aParts

func columnNames cCsv

	return splitKeep(str2list(substr(cCsv,char(13),""))[1])

func first cLetter

	return str2list(cLetter)[1]

func indent cText

	cOut = ""
	aTextLines = str2list(cText)
	for cLine in aTextLines
		cOut += "   | " + cLine + nl
	next
	return left(cOut,len(cOut) - 1)

func joinList aList

	cText = ""
	for item in aList
		if cText != ""
			cText += ", "
		ok
		cText += "" + item
	next
	return cText
Output
1. the template has these fields: name, order, amount, city, date, shop
   the data has 3 rows and the columns: name, order, amount, city, date

2. the first letter:
   | Dear Ann,
   | Your order A-101 of 1,234.50 is ready.
   | It will be sent to Cairo on 9 March 2030.
   | Thank you, [shop?]

3. all the letters, the first line of each:
   1. Dear Ann,
   2. Dear Bob,
   3. Dear Cy,

4. check before sending:
   row 1 (Ann): missing shop
   row 2 (Bob): missing shop
   row 3 (Cy): missing city, shop

5. after the repair:
   problems: 0
   | Dear Cy,
   | Your order A-103 of 19.99 is ready.
   | It will be sent to Giza on 11 March 2030.
   | Thank you, The Tea House
Note

This program uses a generic function (dnStatic) where a class cannot be used. The lesson The generic functions explains them.

How it works#

The template and the data#

The template is a text with fields in braces. A field is {name} or {name:format}: the part after the colon is a format string for a number (N2 gives 1,234.50) or a date (d MMMM yyyy gives 9 March 2030). The data is a CSV text: the first line has the names of the columns and each other line is one letter. readCsv makes a list of rows, and each row is a list of [name, value] pairs that is read with the name of the column: aRow["city"]. splitKeep splits a line at the commas and keeps the empty values, so Cy's empty city is an empty value and not a missing one.

The pattern#

\{(\w+)(?::([^}]+))?\} means: an opening brace, then group 1 (one or more letters, digits or underscores: the name), then an optional part (a colon and group 2, which is everything up to the closing brace: the format), and the closing brace. The tutorial Regular expressions in depth explains groups. The first step uses the same pattern with Regex.Matches to list the fields of the template.

The callback#

Regex.Replace(template, pattern, dnCallback("replaceField")) calls the Ring function once for each match. A callback that is the name of a function gets the .NET arguments as a list: aArgs[1] is the Match, and Groups.Item(1).Value and Groups.Item(2).Value are the name and the format. The function returns the text that replaces the field. It looks in aFields, the row that is being merged: a global, because the callback has no other way to receive it. The callback runs while Regex.Replace is running, on the Ring thread, which is the case that callbacks support (the tutorial Callbacks and events).

Formats#

formatValue turns the value into a date when it has the form 2030-03-09, and into a number otherwise, and writes it with String.Format and the invariant culture. The same template gives the same letters on every computer. A field without a format is replaced by the value as it is.

Missing data#

A letter with a hole in it must not be sent. When a field has no column, or the value is empty, the callback adds the name to aMissing and puts a visible mark ([shop?]) in the letter. Step 4 runs every row first, only to collect the problems: Cy has no city and every row lacks the column shop, which only the template knows. Step 5 repairs the data and shows that there are no problems left, and then the letters can be written.

Note

The callback must not raise an error: an error inside it stops the whole Replace. It returns a mark for a missing field instead, and the program decides afterwards what to do with the problems.

What you learned#

Exercises#

  1. Write each letter into a file named after the person (File.WriteAllText).
  2. Add the field {amount:C} and look at the result with a culture of your own.
  3. Read the data from a real CSV file, with a column of text that has a comma inside quotes (the tutorial Tabular data).
  4. Count how many times each field was used in all the letters.

Next#

A project: a syntax highlighter

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.