समझिए
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 पर काम करें।replaceTextregular 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।
replaceTextpatterns में{और}escape करें।- Duplicates से बचने के लिए PDF link save करें।