📋Build a Charts Dashboard with Google Sheets and HTML Service

1. Overview

\[ \begin{array}{l} \textbf{You can automatically generate personalized documents from Google Sheets --} \\ \text{by using Google Apps Script to merge spreadsheet data into a Docs template.} \\ \textbf{This enables quick creation of certificates, letters, or invoices --} \\ \text{in bulk, with each document tailored to individual spreadsheet rows.} \end{array} \]

2. Core Steps

\[ \begin{array}{ll} \mathbf{Step\ 1:} & \text{Prepare a Google Spreadsheet with columns for personalization (e.g., Name, Date).} \\ \mathbf{Step\ 2:} & \text{Create a Google Docs template with placeholder tags (e.g., {{Name}}, {{Date}}).} \\ \mathbf{Step\ 3:} & \text{Open Apps Script from Google Sheets (Extensions} \rightarrow \text{Apps Script).} \\ \mathbf{Step\ 4:} & \text{Write a script to loop through rows in the sheet.} \\ \mathbf{Step\ 5:} & \text{For each row, replace placeholders in the Docs template with cell values.} \\ \mathbf{Step\ 6:} & \text{Save each personalized document to Google Drive.} \\ \mathbf{Step\ 7:} & \text{(Optional) Share documents automatically via email.} \end{array} \]

3. Sample Google Apps Script


// Code.gs
function createPersonalizedDocs() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var data = sheet.getDataRange().getValues();
  
  var templateId = 'YOUR_TEMPLATE_DOC_ID';
  var folderId = 'YOUR_DESTINATION_FOLDER_ID';
  
  for (var i = 1; i < data.length; i++) { // Skip header
    var row = data[i];
    var name = row[0];
    var date = row[1];
    
    var copy = DriveApp.getFileById(templateId).makeCopy(name + " Document", DriveApp.getFolderById(folderId));
    var doc = DocumentApp.openById(copy.getId());
    var body = doc.getBody();
    
    body.replaceText('{{Name}}', name);
    body.replaceText('{{Date}}', date);
    
    doc.saveAndClose();
  }
}

4. Important Notes

\[ \begin{array}{l} \text{• Placeholders in the Docs template must match exactly with replaceText() calls.} \\ \text{• Store your template and output files in organized Google Drive folders.} \\ \text{• Ensure the script has permissions to access both Docs and Drive.} \\ \text{• Use try/catch blocks to handle errors for missing or invalid data.} \\ \text{• For large datasets, consider adding Utilities.sleep() to avoid execution timeouts.} \end{array} \]

5. Conceptual Flow (in LaTeX)

The process can be described as:

\[ \text{Google Sheets Row Data} \xrightarrow{\text{Apps Script Merge}} \text{Personalized Google Docs Output} \]

Where: \[ \text{Generated Document} = \{\text{Template with Placeholders} \rightarrow \text{Replaced with Row Values}\} \]