I’m working on a Google Docs project where I need to populate tables with data from a spreadsheet. The fourth column is reserved for images, but not every row includes an image, so some image placeholders like {{Threshold gap photo}} remain after the data is inserted.
I want to remove these placeholders and merge the fourth column with the third if there isn’t an image. I discovered a solution that merges columns; however, it only works if the fourth column is empty initially.
Here’s the updated code I’m using:
const doc = DocumentApp.openById(docId);
const body = doc.getBody();
const tables = body.getTables();
// Remove placeholders
placeholdersToFind.forEach(ph => {
const pattern = `{{${ph}}}`;
const entry = body.findText(pattern);
if (entry) entry.getElement().removeFromParent();
});
// Merge cells starting from the fourth table
const requests = tables.slice(3).flatMap((t, i) => {
const tidx = body.getChildIndex(t);
return Array.from({length: t.getNumRows()}, (_, r) => {
if (t.getCell(r, 3).getText().trim() === '') {
return {
mergeTableCells: {
tableRange: {
columnSpan: 2,
rowSpan: 1,
tableCellLocation: {
tableStartLocation: { index: tidx + i + 1 },
rowIndex: r,
columnIndex: 2
}
}
}
};
}
}).filter(Boolean);
});
Docs.Documents.batchUpdate({ requests }, docId);
How can I modify this code to merge cells even when they initially contain placeholder text?