Scroll to Top
๐Ÿ’ป
Free Code
Users get ready-to-use code at no cost.
๐Ÿ“‹
Easy Copy
Copy and use the code instantly.
โšก
Quick Learning
Understand concepts fast and clearly.
๐Ÿ“
Step-by-Step
Follow simple instructions to implement.
๐Ÿ“… September 2, 2024 ๐Ÿ’ป Tutorial โญ Beginner Friendly

V5- Autofill Google Doc

Author Avatar

Ashish Dwivedi

Editorial Team โ€ข Tech Writer

About This Tutorial

Effortlessly autofill Google Docs with dynamic data pulled from forms, spreadsheets, or databases. Create personalized documents like invoices, letters, or reports in seconds. This automation saves time, reduces errors, and ensures consistencyโ€”ideal for teams handling repetitive document generation tasks across HR, sales, education, or client communications.
  1. Step 1: Click on the Copy button to copy the code snippet.
  2. Step 2: Paste the copied code into your project’s script editor.

Apps Scripts Blog

Read Blog

๐Ÿ“‚ javascript
โšก script1.js
โšก script1.js
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs', 'createNewGoogleDocs');
  menu.addToUi();
}

function createNewGoogleDocs() {
  // โœ… Define template and destination folder IDs
  const googleDocTemplateId = '1G-qP5xoZAdjp3d1LAW9zk5tbum4vBS-GP9B51MTbDW8'; // replace with your template ID
  const destinationFolderId = '1P_S897kbbqcNPYLCoQ-MOqjK-E7oVudQ'; // replace with your destination folder ID

  // โœ… Get the Google Doc template and destination folder
  const googleDocTemplate = DriveApp.getFileById(googleDocTemplateId);
  const destinationFolder = DriveApp.getFolderById(destinationFolderId);

  // โœ… Get the active spreadsheet and target sheet
  const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = activeSpreadsheet.getSheetByName('Sheet1'); // replace with your sheet name

  // โœ… Get all data from the sheet
  const rows = sheet.getDataRange().getValues();

  // โœ… Loop through each row in the sheet
  rows.forEach(function(row, index) {
    // Skip the header row (index 0) and rows that already have a document URL (column F = index 5)
    if (index === 0 || row[5]) return;

    // Extract data from the row
    const fullName = row[0];
    const address = row[1];
    const city = row[2];

    // โš ๏ธ Mobile number column is being formatted as a date โ€” fixing this:
    const mobileNumber = row[3] ? row[3].toString() : '';  // keep original value

    const email = row[4];

    // โœ… Make a copy of the Google Doc template
    const copy = googleDocTemplate.makeCopy(`${fullName}, ${address} Employee Details`, destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();

    // โœ… Replace placeholders in the copied document
    body.replaceText('{{Full Name}}', fullName || '');
    body.replaceText('{{Your Address}}', address || '');
    body.replaceText('{{Your City}}', city || '');
    body.replaceText('{{Mobile Number}}', mobileNumber || '');
    body.replaceText('{{Your Email}}', email || '');

    // โœ… Save and close the document
    doc.saveAndClose();

    // โœ… Get the URL of the copied document
    const docUrl = doc.getUrl();

    // โœ… Update the corresponding cell in the sheet with the document URL (column F = 6)
    sheet.getRange(index + 1, 6).setValue(docUrl);
  });
}