Google Sheets to Docs: Easy and Free Mail Merged Documents
Automate Mail Merge Using Apps Script
Transforming data into readable documents is a challenge
many people face, especially when working with spreadsheets full of information. That’s where Google Apps Script becomes a powerful ally. With just a few lines of code, you can automate the process of turning rows of spreadsheet data into neatly formatted, styled pages in a Google Document—each row becoming its own section, cleanly separated and easy to read.
With this script, I created a script that takes values from columns A to D in a Google Sheet and generates a full-page entry in a Google Doc for each row. Styled with Calibri, 16-point bold text, and organized into clearly labeled sections, the resulting document isn’t just functional—it’s presentation-ready. The document is also automatically saved in a specific Google Drive folder, keeping your workflow organized without lifting a finger.
With tools like Google Apps Script, even non-developers can build real solutions that save time, reduce errors, and bring clarity to repetitive documentation tasks.
I hope you find this useful.
function createDocFromSheetRows() {
// === CONFIGURATION ===
const sheetId = "YOUR_SHEET_ID_HERE"; // Replace with your actual Google Sheet ID
const folderId = "ADD YOUR FOLDER ID HERE"; // Target folder ID where the document will be stored
const docName = "Generated Document"; // Name of the document to be created
// === ACCESSING THE SPREADSHEET AND FOLDER ===
const sheet = SpreadsheetApp.openById(sheetId).getSheets()[0]; // Open the first sheet in the spreadsheet
const data = sheet.getRange("A2:D18").getValues(); // Read values from range A2 to D18
const folder = DriveApp.getFolderById(folderId); // Get the target folder by ID
// === CREATING THE GOOGLE DOC ===
const doc = DocumentApp.create(docName); // Create a new Google Doc
folder.addFile(DriveApp.getFileById(doc.getId())); // Move the new Doc to the target folder
// Optional: remove from root "My Drive" to keep things clean
DriveApp.getRootFolder().removeFile(DriveApp.getFileById(doc.getId()));
const body = doc.getBody(); // Get the document body where we'll insert content
// === LOOP THROUGH EACH ROW AND ADD TO DOCUMENT ===
data.forEach((row, index) => {
// Create a section header for each row
body.appendParagraph(`Row ${index + 1}`)
.setFontFamily("Calibri")
.setFontSize(16)
.setBold(true);
// Add each cell from the row to the document
row.forEach((cell, colIndex) => {
body.appendParagraph(`Column ${String.fromCharCode(65 + colIndex)}: ${cell}`)
.setFontFamily("Calibri")
.setFontSize(16)
.setBold(true);
});
// Add a page break after each row, except the last one
if (index < data.length - 1) {
body.appendPageBreak();
}
});
// === SAVE CHANGES ===
doc.saveAndClose(); // Finalize the document
}