Google Apps 脚本:利用不同的 Google 文档模板根据单元格值生成文档

问题描述 投票:0回答:1

这里绝对是菜鸟,所以请原谅我......我什至不知道我是否可以解释我在寻找什么

我使用两个不同的 Google 文档文件作为信函模板,并希望我的脚本根据单元格值选择适当的模板。

我能够成功利用这些模板之一,但不确定如何/在哪里添加代码以使其根据单元格值选择不同的模板文件。

两个模板都利用电子表格中的相同信息,但周围的正文文本不同。

这是我当前的代码:

function onOpen() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs', 'createNewDocument');
  menu.addToUi();

}

function createNewDocument() {

  const googleDocTemplate = DriveApp.getFileById('169hjL8sY6De5v4uKwzG0Aua3yUY3_2ZoESwpuHwzFJk');
  const destinationFolder = DriveApp.getFolderById('1k6dPK2m6hJ7HlXMM4BLISEspoVJLQPyT');
  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;
    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);

  })





}
google-sheets google-apps-script google-docs
1个回答
0
投票

这个怎么样:

function createNewDocument() {
  rows.forEach(function (row, index, templateid) {
    const googleDocTemplate = DriveApp.getFileById('templateid');
    const destinationFolder = DriveApp.getFolderById('1k6dPK2m6hJ7HlXMM4BLISEspoVJLQPyT');
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Currently Processing');
    const rows = sheet.getDataRange().getValues();
    if (index === 0) return;
    if (row[30]) return;
    if (!row[3]) return;
    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);
  })
}
© www.soinside.com 2019 - 2024. All rights reserved.