Formatting and image not working
I am utilizing a script I found online to create name cards for our guests to wear on a lanyard. We would be printing these on perforated 3x5 cards. This pulls data from a sheet Link to populate into a document template Link.
I am able to get text fields to work with the exception of being centered, but the inserted image will not copy to the copied tables.
The script is as follows:
const g = {
docId: '14Pa2L0gudkiDbDBQFXztY2Gd5J8u3hlmARaH0F_LIPE',
};
function init_() {
g.doc = DocumentApp.openById(g.docId);
g.body = g.doc.getBody();
g.ss = SpreadsheetApp.getActive();
}
function CreateGuestID() {
init_();
const sh = g.ss.getSheetByName('guests');
[g.guestsHeaders, ...g.guestsData] = sh.getDataRange().getValues();
const templateTable = g.body.getTables()[0];
const templateCell = templateTable.getCell(0, 0).copy();
const numLines = templateCell.getNumChildren();
const templateTexts = [];
const templateAttributes = [];
for (let i = 0; i < numLines; i++) {
const child = templateCell.getChild(i);
templateTexts.push(child.getText());
const sourceAttributes = child.getAttributes();
const atts = {};
Object.entries(sourceAttributes).forEach(([k, v]) => {
if (!(v instanceof Object)) {
atts[k] = v;
}
});
templateAttributes.push(atts);
}
const bodyChildren = g.body.getNumChildren();
for (let i = bodyChildren - 2; i > 1; i--) {
g.body.removeChild(g.body.getChild(i));
}
const numRows = templateTable.getNumRows();
const numCols = templateTable.getRow(0).getNumCells();
let iCol = Infinity;
let iRow = Infinity;
let table;
g.guestsData.forEach((recipient) => {
if (iCol >= numCols) {
iCol = 0;
iRow++;
}
if (iRow >= numRows) {
iRow = 0;
table = g.body.appendTable(templateTable.copy());
table.setBorderWidth(0);
table.getCell(0, 0).clear();
}
const newCell = table.getCell(iRow, iCol);
newCell.clear();
templateTexts.forEach((templateText, i) => {
const newLine = merge_(templateText, recipient);
const par = newCell.insertParagraph(i, newLine);
par.editAsText().setAttributes(templateAttributes[i]);
par.setForegroundColor(templateAttributes[i].FOREGROUND_COLOR);
});
iCol++;
});
g.doc.saveAndClose();
}
function merge_(text, recipient) {
g.guestsHeaders.forEach((header, i) => {
text = text.replace(`%${header}%`, recipient[i]);
});
return text;
}
I would be grateful for any assistance.