Mac OS X:从数据库合并

发布于 2024-08-21 17:26:14 字数 242 浏览 2 评论 0原文

我希望将(服务器)数据库中的几个自定义字段合并到字处理文档中,超出正常的“邮件合并”字段。我没有找到类似开箱即用的 API 之类的东西,可以让您对自定义字段执行此操作。这似乎在商业软件中很常见,但我认为这也是 Mac 在这方面表现不佳的又一个原因。

如果我要为此编写一个 Mac OS X/Objective-C/Cocoa 应用程序,那么最好的方法是在 RTF 文件中查找和替换字符串吗?也许在原始 RTF 中? 有没有更好的方法或格式来实现这一点?

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 the 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2024-08-28 17:26:14

没有专用的“邮件合并 API”并不是一个缺点——它必须过于具体才能成为一个普遍有用的 API。如果您更仔细地考虑这一点,就会发现没有足够抽象的案例来证明专用的内置 API 的合理性。大多数人想要“将 MySQL 数据库中的记录合并到 Word 文档中”或“将 Oracle 数据库中的记录合并到 PDF 文档中”。

不过,构建它的所有构建块都已存在,并且相对简单(伪代码):

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

要点:

  1. Cocoa 本身可以读取和写入 RTF/RTFD 以及 Word Doc 格式。您可以在文本系统文档中找到此信息。
  2. “从数据库读取字段”非常笼统。如果您还没有整理出与数据库的连接(即,您已经有了合并数据),则需要更具体地了解数据库(即,您尝试过什么,如果有的话)。
  3. 一旦您有了数据和模板文档,“邮件合并”基本上只是令牌的字符串替换(例如 $$!FIRSTNAME!$$ 或其他一些荒谬的独特组合)。这可以通过 NSMutableString 的内置函数和 for 循环轻松处理。
  4. 如果您需要关于#3 的更复杂的东西,可以使用正则表达式框架。

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.
夜灵血窟げ 2024-08-28 17:26:14

您可能想看看 Matt Gemmell 的 MGTemplateEngine ,因为它听起来就像它可能就是您正在寻找的一样。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文