在 Google 文档中通过标签附加图像

Attach images through tags in a Google Doc

我想通过 Google Drive 生成​​的 ID 将不同的 JPG 图像附加到 Google 文档中,该文档包含来自 Sheets 的数据,其结果类似于 this one or this one。为了实现这一点,我想到了以下算法:

  1. 创建条件 (if/else),根据 sheet

    的单元格中显示的型号和颜色更改图像
  2. 浏览云端硬盘并找到图片的 ID

  3. 通过标签替换 Google 文档中的图像

我开发了以下代码,但我卡在了这一点上,我试图避免为每种颜色创建多个模板:

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName');
const rows = sheet.getDataRange().getValues();
var googleDocTemplate = DriveApp.getFileById('ID') //Generic doc template

 rows.forEach(function(row,index) {
  if (index === 0) return;
  if (row[15]) return;
  if (row[3]==='4Runner Limited' && row[21]===0 && row[5]==='White Pearl'){
    googleDocTemplate = DriveApp.getFileById('ID'); //specific template related to the 4Runner Model 
   }

const destinationfolder = DriveApp.getFolderById('ID');
const destinationfolderPDF = DriveApp.getFolderById('ID');
copy = googleDocTemplate.makeCopy(`${row[2]} ${row[3]} ${row[4]} ${row[5]} - ${row[1]}`,destinationfolder);
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();

body.replaceText('{{Client}}',row[1]);
body.replaceText('{{Brand}}',row[2]);
body.replaceText('{{Model}}',row[3]);
body.replaceText('{{Year}}',row[4]);
body.replaceText('{{Color}}',row[5]);
.
.
.
//other body.replaceText items

如有任何帮助,我们将不胜感激!

const img = {
  'Model1' : {
    'Color1': 'id1',
    'Color2': 'id2'
  },
  'Model2': {
    'Color1': 'id3'
  }
};
const imgId = img[row[3]][row[5]];
const image = DriveApp.getFileById(imgId).getBlob();
body.insertImage(0, image);