fx Gmail, Drive, Calendar और Docs
ENहिन्दी

Docs टेम्पलेट से PDF इनवॉइस बनाएं

⏱ 22 मिनट

आप क्या सीखेंगे

  • placeholders वाला Google Docs template बनाना
  • template copy करना, placeholders बदलना, PDF export करना
  • PDF का link शीट में वापस रखना और उसे email करना

समझिए

Step 1: template

Invoice Template नाम का Google Doc बनाइए और अपनी पसंद से design कीजिए (logo, पता, table)। जहाँ data आना है वहाँ double curly braces में placeholder लिखिए:

INVOICE {{InvoiceNo}}                     Date: {{Date}}

Bill to: {{Customer}}
Phone:   {{Phone}}

Item: {{Item}}
Amount: ₹{{Amount}}
GST:    ₹{{Gst}}
Total:  ₹{{Total}}

Doc का ID उसके URL से copy करें: docs.google.com/document/d/<TEMPLATE_ID>/edit।

Step 2: शीट

Tab Invoices:

A InvoiceNo B Date C Customer D Phone E Email F Item G Amount H PDF Link

Step 3: script

const TEMPLATE_ID = 'PASTE_TEMPLATE_DOC_ID';
const OUTPUT_FOLDER_ID = 'PASTE_FOLDER_ID';
const GST_RATE = 0.03;

function createInvoices() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Invoices');
  const lastRow = sheet.getLastRow();
  if (lastRow < 2) return;

  const rows = sheet.getRange(2, 1, lastRow - 1, 8).getValues();
  const folder = DriveApp.getFolderById(OUTPUT_FOLDER_ID);
  const tz = Session.getScriptTimeZone();

  rows.forEach((r, i) => {
    const [invoiceNo, date, customer, phone, email, item, amount, link] = r;
    if (!invoiceNo || link) return; // skip blank rows and rows already done

    const gst = Math.round(amount * GST_RATE * 100) / 100;
    const total = amount + gst;
    const fmt = n => Number(n).toLocaleString('en-IN', { minimumFractionDigits: 2 });

    // 1. Copy the template
    const copy = DriveApp.getFileById(TEMPLATE_ID).makeCopy(`Invoice ${invoiceNo}`, folder);

    // 2. Replace placeholders
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();
    const values = {
      InvoiceNo: invoiceNo,
      Date: Utilities.formatDate(new Date(date), tz, 'dd-MMM-yyyy'),
      Customer: customer,
      Phone: phone,
      Item: item,
      Amount: fmt(amount),
      Gst: fmt(gst),
      Total: fmt(total)
    };
    Object.entries(values).forEach(([key, val]) => {
      body.replaceText(`\\{\\{${key}\\}\\}`, String(val));
    });
    doc.saveAndClose();

    // 3. Export as PDF and remove the temporary Doc
    const pdf = folder.createFile(copy.getAs(MimeType.PDF)).setName(`Invoice ${invoiceNo}.pdf`);
    copy.setTrashed(true);

    // 4. Save the link back to the sheet
    sheet.getRange(i + 2, 8).setValue(pdf.getUrl());

    // 5. Email it (optional)
    if (email) {
      GmailApp.sendEmail(email, `Invoice ${invoiceNo}`,
        `Dear ${customer},\n\nPlease find your invoice attached.\n\nThank you.`,
        { attachments: [pdf.getBlob()], name: 'Accounts Team' });
    }
  });
}

हर step क्यों ज़रूरी है

  • makeCopy — template को कभी edit न करें; हमेशा copy पर काम करें।
  • replaceText regular expression इस्तेमाल करता है। वहाँ curly braces का ख़ास मतलब होता है, इसलिए हम उन्हें escape करते हैं: \\{\\{Date\\}\\}। Escape न करें तो कुछ placeholders नहीं मिलेंगे।
  • saveAndClose() — export से पहले ज़रूरी है, वरना PDF में पुराने placeholders दिख सकते हैं।
  • getAs(MimeType.PDF) — Doc को PDF blob में बदलता है; createFile उसे save करता है।
  • setTrashed(true) — temporary Doc को bin में डालता है ताकि folder में सिर्फ़ PDFs रहें।
  • Link column "done" marker का काम भी करता है, इसलिए दोबारा चलाने पर सिर्फ़ नए invoices बनते हैं।

और रूप

  • तस्वीरों वाले designs के लिए Google Slides template लें (SlidesApp, replaceAllText)।
  • Drive lesson के साल/महीने वाले folder helper के साथ जोड़ें।
  • इसे menu item से चलाएँ, या form-submit trigger से ताकि हर नए order का invoice अपने-आप बने।

मुख्य बातें

  • Template + placeholders + copy + replace + export = अपने-आप बनने वाले documents।
  • replaceText patterns में { और } escape करें।
  • Duplicates से बचने के लिए PDF link save करें।

अभ्यास

अभ्यासअभ्यास 1

{{AmountInWords}} placeholder जोड़िए। एक helper लिखिए जो number को भारतीय तरीके के शब्दों में बदले (जैसे "One Lakh Twenty Thousand Rupees Only")।

प्रश्नोत्तरी

getAs(MimeType.PDF) से पहले doc.saveAndClose() क्यों ज़रूरी है? a) file का नाम बदलने के लिए b) ताकि PDF में बदला हुआ text आए c) share करने के लिए
ताकि PDF में बदला हुआ text आए
replaceText में curly braces escape क्यों करते हैं? a) हिंदी text के लिए b) pattern एक regular expression है c) Docs braces की इजाज़त नहीं देता
pattern एक regular expression है
Docs टेम्पलेट से PDF इनवॉइस बनाएं · हिंदी | ExcelWalaa