Deleting placeholder content and combining cells in Google Docs tables using MergeTableCellsRequest

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?

I’ve encountered a similar challenge and discovered an alternative solution that might work. Instead of checking if the fourth column is empty, you can use a regular expression to flag cells containing placeholders. For instance, you could define a regex pattern such as /^{{.*}}$/ to match any placeholder, then verify if a cell’s content matches this pattern before merging. This technique not only covers different placeholder formats but also makes your code more adaptable to future changes. Make sure to test your implementation thoroughly and handle errors appropriately.

hey SwiftCoder42, have u tried modifying ur merge condition? instead of checking if the cell is empty, u could check if it contains a placeholder. something like:

if (hasPlaceholder(t.getCell(r, 3))) {
// merge cells logic here
}

this way, you’ll catch cells with placeholders too. just make sure to define that hasPlaceholder function somewhere in ur code!

One approach is to stop relying on the cell being empty and instead detect if it contains a placeholder. You can do this by creating a helper function that checks whether the cell’s text starts and ends with the expected delimiters, such as ‘{{’ and ‘}}’.

For example, define a function like:

function hasPlaceholder(cell) {
  const text = cell.getText().trim();
  return text.startsWith('{{') && text.endsWith('}}');
}

Then, modify your merging logic to call this function when deciding whether to merge the cell with its neighbor. This should allow you to merge cells even when they initially contain placeholder text.