Currency In A Mail Merge

While verifying the validity of a mail merge, one of my coworkers ran into the issue of extracting a currency value from a spreadsheet and correctly formatting it in Microsoft Word. The issue stemmed from using this numeric picture field switch which is suggested by Microsoft:
\# $#,###.00

If you have a speadsheet full of large numbers, the above code will probably work. On the other hand, if you have a one or two digit number, a few spaces will exist between the dollar sign and the number. Perhaps this is desirable to people at Microsoft, but in many other places, this isn’t what people want. Instead, after doing a little research and trial runs, I found that this numeric picture field switch will display currency with the leading spaces for small numbers:
\# $,0.00


I personally love both JavaScript and Python. For this reason, from time to time I like to port functions that are available in Python, over to JavaScript. The following is a port of the expandTabs function.

String.prototype.expandTabs = function(tabSize) {
  tabSize = Math.min(Math.max(parseInt(+tabSize || 8), 1), 32);
  return this.replace(/^.*\t.*$/gm, function(line) {
    var allBefore = "";
    return line.replace(/(.*?)(\t+)/g, function(match, before, tabs) {
      allBefore += before;
      tabs = new Array(1 + tabSize * tabs.length - allBefore.length % tabSize).join(" ");
      allBefore += tabs;
      return before + tabs;

The biggest difference between this JavaScript version and the Python version is the limitation of a 32 character tab size. If you want to increase the maximum tab size, simply change 32 to something else.


One of the functions that didn’t make the cut for jPaq is String.prototype.toCharCodes().  The reason is I don’t feel that it is a function that many people are looking for.  Still, if you are looking for a good implementation for it, you can use this:

String.prototype.toCharCodes = function() {
  for(var arr = this.split(""), i = this.length - 1; i >= 0; i--)
    arr[i] = arr[i].charCodeAt(0);
  return arr;