I would like to attach a different JPG images through the ID's generated by Google Drive into a Google Doc that contains data from Sheets having results like this one or this one. To accomplish this I've thought the following algorithm:
Create a conditional (if/else) that changes the image depending on the model and color displayed in the cells of the sheet
Go through Drive and find the image's ID
Substitute the image in the Google Doc via tags
I've developed the following code, but I'm stucked at this point and I'm trying to avoid creating multiple templates for every color:
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
Any help would be appreciated!
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);