开发者

Mac OS X: Merge from Database

I would like to have several custom fields from a (server) database merged into Word Processing documents, beyond the normal "mail merge" fields. I'm not finding anything like an API out of t开发者_如何转开发he box that allows you to do this for custom fields. This seems like it would be pretty common in business software, but I assume that's one more reason why the Mac doesn't excel there.

If I were to write a Mac OS X/Objective-C/Cocoa app for this, would the best way to approach it be finding and replacing strings in an RTF file? Maybe in the raw RTF? Is there a better way or format to work with for this?


hat there isn't a dedicated "mail merge API" isn't a detractor - it would have to be too specific to be generally useful enough to be an API. If you think about this more closely, there's no abstract-enough case to justify a dedicated, built-in API. Most people want to "merge records from a MySQL database into a Word document" or "merge records from an Oracle database into a PDF document".

All the building blocks are there to build it, though, and it's relatively simple (pseudocode):

retrieve interesting records

for each record

  load a new copy of template

  replace strings

  save modified template to a file or do whatever

  next record

Some points:

  1. Cocoa reads and writes RTF/RTFD, and Word Doc formats natively. You'll find this information in the text system documentation.
  2. "Reading fields from a database" is very general. If you don't already have the connectivity to the database sorted out (ie, you already have the merge data), you'll need to be more specific about the database (ie, what you've tried, if anything).
  3. Once you have your data and your template document, "mail merge" is basically just string replacement of a token (like $$!FIRSTNAME!$$ or some other ridiculously-unique combination). This is easily handled with NSMutableString's built-in functions and a for loop.
  4. There are RegEx frameworks out there if you need something more complicated regarding #3.


You might want to check out Matt Gemmell's MGTemplateEngine as it sounds like it might be what you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜