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

基于单元格值选择不同Google Doc模板生成文档的Apps Script实现

根据单元格值选择对应Google Doc模板的代码修改

核心逻辑是在处理每行数据时,读取指定单元格的判断值,根据值选择对应的模板ID,再执行后续的文档生成和内容替换操作(因为两个模板占位符一致,替换逻辑无需修改)。

修改后的完整代码如下:

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs', 'createNewDocument');
  menu.addToUi();
}

function createNewDocument() {
  // 定义两个模板的ID,替换成你自己的模板文件ID
  const TEMPLATE_ID_1 = '你的第一个模板ID';
  const TEMPLATE_ID_2 = '你的第二个模板ID';
  const destinationFolder = DriveApp.getFolderById('REDACTED');
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Currently Processing');
  const rows = sheet.getDataRange().getValues();
  
  rows.forEach(function(row, index){
    if(index === 0) return; // 跳过表头
    if(row[30]) return; // 已有文档链接的行跳过
    if(!row[3]) return; // 无姓名的行跳过

    // 根据指定单元格值选择模板
    let selectedTemplateId;
    // 这里用第6列(索引5,列从0开始计数)作为判断依据,可改成你实际需要的列索引
    const templateCondition = row[5];
    // 自定义判断规则,比如单元格值为"TypeA"用模板1,"TypeB"用模板2
    if(templateCondition === "TypeA") {
      selectedTemplateId = TEMPLATE_ID_1;
    } else if(templateCondition === "TypeB") {
      selectedTemplateId = TEMPLATE_ID_2;
    } else {
      // 不符合条件的行跳过,可根据需求调整处理逻辑
      console.log(`第${index+1}行模板条件不匹配,已跳过`);
      return;
    }
    const googleDocTemplate = DriveApp.getFileById(selectedTemplateId);

    var fullName = row[3];
    var date = Utilities.formatDate(new Date(row[0]), "PST", "MMMM dd, yyyy");
    var bothNames = fullName.split(" ");
    var firstName = bothNames[0];
    var lastName = bothNames[1];
    var abbreviatedName = firstName.substring(0, 1) + ". " + lastName;

    var fileName = abbreviatedName + ", " + date + " ESA Housing Letter";
    
    const copy = googleDocTemplate.makeCopy(fileName, destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();

    // 以下替换逻辑保持不变,两个模板占位符一致
    body.replaceText(`{{Name}}`, row[3]);
    body.replaceText(`{{state}}`, row[6]);
    body.replaceText(`{{one/two}}`, row[10]);
    body.replaceText(`{{animalname1}}`, row[11]);
    body.replaceText(`{{animalbreed1}}`, row[12]);
    body.replaceText(`{{age1}}`, row[13]);
    body.replaceText(`{{weight1}}`, row[14]);
    body.replaceText(`{{chartnote1}}`, row[15]);
    body.replaceText(`{{animalname2}}`, row[16]);
    body.replaceText(`{{animalbreed2}}`, row[17]);
    body.replaceText(`{{age2}}`, row[18]);
    body.replaceText(`{{weight2}}`, row[19]);
    body.replaceText(`{{chartnote2}}`, row[20]);
    body.replaceText(`{{fullstate}}`, row[23]);
    body.replaceText(`{{title}}`, row[24]);
    body.replaceText(`{{fulltitle}}`, row[25]);
    body.replaceText(`{{licensenumber}}`, row[26]);
    body.replaceText(`{{fulladdress}}`, row[27]);
    body.replaceText(`{{her/him}}`, row[28]);
    body.replaceText(`{{her/his}}`, row[29]);
    body.replaceText(`{{grammatics}}`, row[31]);
    body.replaceText(`{{grammatical count}}`, row[32]);
    body.replaceText(`{{animalXs}}`, row[33]);
    body.replaceText(`{{ASx}}`, row[34]);
    body.replaceText(`{{havehas}}`, row[31]);
    body.replaceText(`{{XXX}}`, Utilities.formatDate(new Date(row[4]), "PST", "MMMM dd, yyyy"));
    body.replaceText(`{{DOS}}`, Utilities.formatDate(new Date(row[0]), "PST", "MMMM dd, yyyy"));

    const url = doc.getUrl();
    sheet.getRange(index + 1, 31).setValue(url);
  })
}

需要自定义的部分:

  1. 替换模板ID:把TEMPLATE_ID_1和TEMPLATE_ID_2的值改成你两个Google Doc模板的实际ID
  2. 调整判断列:把row[5]改成你用来区分模板的单元格所在列的索引(列从0开始计数,比如A列是0,B列是1)
  3. 修改判断条件:把"TypeA"和"TypeB"改成你实际的判断值,比如单元格内容为"Single"和"Multiple"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:41:28