You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于Google Apps Script实现Google Docs表格单元格条件格式

Conditional Formatting Google Docs Tables Using Mail Merge Values

Great to hear you're expanding from Sheets scripts to Docs—let's break this down simply. The core steps are: accessing your Docs table, pulling in your mail merge values (likely from a Google Sheet), then applying conditional styles based on those values.

Here's a practical starting point with a clear if statement example, tailored to common mail merge scenarios:

Step 1: Access Your Document and Table

First, target the specific table in your Google Doc. Most docs use one main table for mail merge, so we'll grab the first table by default:

function formatDocTable() {
  // Get the active Google Document
  const doc = DocumentApp.getActiveDocument();
  // Get the first table in the document's body
  const table = doc.getBody().getTables()[0];
  
  if (!table) {
    Logger.log("No table found in the document!");
    return;
  }

Step 2: Pull Mail Merge Data from Your Sheet

Assuming your mail merge values are stored in a Google Sheet, retrieve the data (we'll skip the header row here):

// Replace with your Sheet ID and tab name
  const sheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getSheetByName("MailMergeData");
  const mergeData = sheet.getDataRange().getValues();
  // Skip the header row (index 0) to get only the merge values
  const rowData = mergeData.slice(1);

Step 3: Apply Conditional Formatting with an if Statement

Let's say your table has a column where you've merged a "Status" value (e.g., "Approved" or "Rejected"). We'll loop through the table rows, check the value, and apply styling:

// Loop through each row in the table (skip header row if needed)
  for (let i = 1; i < table.getNumRows(); i++) {
    const row = table.getRow(i);
    // Get the cell containing the merged value (e.g., column 2, index starts at 0)
    const statusCell = row.getCell(1);
    const cellText = statusCell.getText().trim();

    // First conditional check: highlight "Approved" in green
    if (cellText === "Approved") {
      statusCell.setBackgroundColor("#b7e1cd"); // Soft green background
      statusCell.setFontWeight("bold"); // Bold text for emphasis
    } 
    // Add a secondary condition for rejected entries
    else if (cellText === "Rejected") {
      statusCell.setBackgroundColor("#f8cbad"); // Soft red background
    }
  }

  // Save changes to the document
  doc.saveAndClose();
}

Key Adaptations for Your Use Case:

  • Adjust cell indices: Change row.getCell(1) to match the column in your table that holds the merged value (remember, columns start at 0).
  • Modify merge data logic: If your mail merge uses a different source or structure, tweak how you pull rowData to align with your setup.
  • Custom styles: Use methods like setFontColor(), setFontSize(), or setBorderColor() to apply the exact formatting you need.

Once you have this base, you can easily extend it with more if/else conditions, loop through multiple tables, or add logic to handle dynamic merge values.

内容的提问来源于stack exchange,提问作者Shaun McGrail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:09:39