المكتبة التقنية ←

إنشاء Word وExcel وPowerPoint من Node.js باستخدام ETCH

English version

Teams often need the same document in different formats: invoices, monthly workbooks, or project update decks. A common approach is to build each document with code. That can make even small layout changes require a developer.

ETCH takes a template-first approach. You design a regular Office document, add placeholders where data should appear, then send the template and a JavaScript object to ETCH from Node.js. The template controls the layout; the data supplies the content.

Start with a Word template

Imagine an invoice template with placeholders such as {{customer.name}}, {{invoiceNumber}}, and {{total}}. Your Node.js code passes the invoice data to ETCH, which returns a completed Word document. A loop such as {{#each items}} can repeat a table row for every line item.

The same approach can place an image, QR code, or barcode in the finished file. If a designer wants to change the colors, spacing, or typography, they can edit the Word template while the data preparation code stays focused on the business logic.

Excel and PowerPoint templates

In Excel, a repeated row can become one row per region, product, or month. The source article also shows formulas and charts adjusting to the generated rows, and a sheet template being copied once for each region.

In PowerPoint, a slide loop can generate a slide for each project, with project details, milestones, and chart data filled from the input. This can help keep recurring reports consistent while the underlying data changes.

A simple Node.js workflow

The article's example uses the @get-etch/etch-client package. At a high level, the program reads a JSON file, creates an ETCH client, processes a template with the data, then saves the resulting document. ETCH also supports PDF output and batch rendering for multiple records.

Before starting, the article lists Node.js 22 or later, an ETCH account, a package token for installing the client, and an API key for requests. Keep these credentials in environment variables and follow the current ETCH setup guide for your package manager.

When this approach fits

Template-based generation is useful when many documents share a stable design but contain different data. Examples include invoices, sales summaries, certificates, and project updates. It is less useful when every document is a one-off with a completely different layout.

The key design choice is separating content from presentation: let application code prepare trustworthy data, and let the Office template define how that data should look.

Read the ETCH article and examples.

NodeJS #JavaScript #DocumentAutomation #Office

النسخة العربية

فرق العمل تحتاج أحيانًا نفس البيانات في فاتورة Word أو ملف Excel شهري أو عرض PowerPoint. بناء كل تصميم داخل الكود يجعل تغييرات الشكل البسيطة محتاجة تدخل من المطور.

القالب يحدد الشكل، والبيانات تحدد المحتوى

ETCH تعتمد على قوالب Office عادية. تجهز المستند وتضع أماكن للبيانات، ثم ترسل القالب وكائن JavaScript إلى الخدمة من Node.js. القالب مسؤول عن التصميم، والكود مسؤول عن تجهيز بيانات صحيحة.

في قالب فاتورة مثلًا، يمكن استخدام {{customer.name}} و{{invoiceNumber}} و{{total}}. حلقة مثل {{#each items}} تكرر صفًا لكل بند. ويمكن للقالب إدراج صور أو QR Codes أو Barcodes. تغيير الألوان والمسافات والخطوط يتم في ملف Word، مع بقاء تجهيز البيانات في الكود.

Excel وPowerPoint

في Excel يمكن تكرار صفوف بحسب المنتجات أو المناطق أو الشهور. المصدر يعرض كذلك تحديث الصيغ والرسوم البيانية مع الصفوف، وتكرار Sheet لكل منطقة. في PowerPoint يمكن تكرار شريحة لكل مشروع وتعبئة التفاصيل والمراحل وبيانات الرسم.

دورة العمل من Node.js

المثال يستخدم الحزمة @get-etch/etch-client: قراءة بيانات JSON، إنشاء العميل، معالجة القالب بالبيانات، ثم حفظ الملف. المصدر يذكر دعم PDF ومعالجة عدة سجلات دفعة واحدة.

متطلبات المقال الأصلي تشمل Node.js 22 أو أحدث، وحساب ETCH، وPackage Token لتثبيت العميل، وAPI Key للطلبات. احفظ بيانات الاعتماد في متغيرات البيئة، وراجع دليل ETCH الحالي عند التنفيذ.

هذا الأسلوب مناسب للفواتير والتقارير والشهادات التي تتشارك تصميمًا ثابتًا وبيانات متغيرة. فائدته أقل عندما يكون لكل ملف تصميم مختلف تمامًا.

الفكرة الأساسية: افصل تجهيز البيانات عن تصميم المستند.

المقال الأصلي وأمثلة ETCH.