从单元格中删除占位符文本并使用 MergeTableCellsRequest 合并到 google 文档文档中

问题描述 投票:0回答:1

我有一个包含多个表格的文档。表中的内容是根据电子表格中的数据填充的,占位符文本将替换为相应电子表格列中的文本或图像。图像将始终位于第四列,但并非每行都包含图像。因此,在将所有数据插入谷歌文档后,文档中会留下一些图像占位符文本。请参阅here(例如{{阈值间隙照片}})

如果第四列中没有图像,我的脚本应该删除占位符文本并将第四列与第三列合并。喜欢so

此解决方案可以合并两列,但前提是第四列一开始是空白的(即第四列中没有文本,然后将其删除)。所以,我确信问题是我如何从单元格中删除占位符文本。

  var templateFile = DriveApp.getFileById('1MKshF2xmQvU26hG6S5jqFeynZsLM6iSFr4y-vf1cAx0'); //Document Template ID
  var destinationFolder = DriveApp.getFolderById('1-OCG7h5m6yU5hBqsnUOJADeL8-7gXrld'); //folder where reports will go
  var fileName = "merge-test-doc";
  var newFile = templateFile.makeCopy(fileName, destinationFolder);
  var placeholdersToFind = ['Threshold gap photo', 'Third-party photo', 'Door thickness photo', 'Leaf to frame photo','Meeting stile photo']
  const docId = newFile.getId();
  const doc = DocumentApp.openById(docId);
  const body = doc.getBody();
  const tables = body.getTables();
  const obj = Docs.Documents.get(docId).body.content;

  for (var j = 1; j <= placeholder.length; j++) {
    var currPH = placeholdersToFind [j - 1]; //get the current placeholder to be found in report template to replace with image
    var patternToFind = Utilities.formatString('{{%s}}', currPH); //format it to match how it looks in report template
    var entry = body.findText(patternToFind) // find the placeholder in the template
    if (entry != null) {
      r = entry.getElement().removeFromParent() //remove the placeholder text
    }
  }

//ignore first 3 tables 
  const requests = tables.slice(3).reduce((ar, t) => {
    const tidx = body.getChildIndex(t);
    const rows = t.getNumRows();
    for (let r = 0; r < rows; r++) {
      const col4 = t.getCell(r, 3).getChild(0).asParagraph().getNumChildren();
      if (col4 == 0) {
        ar.push({ mergeTableCells: { tableRange: { columnSpan: 2, rowSpan: 1, tableCellLocation: { tableStartLocation: { index: obj[tidx + 1].startIndex }, rowIndex: r, columnIndex: 2 } } } });
      }
    }
    return ar;
  }, []);
  Docs.Documents.batchUpdate({ requests }, docId);
google-apps-script google-docs google-docs-api
1个回答
0
投票

在这个答案中,为了实现您的目标,我修改了我的脚本

修改后的脚本:

function myFunction() {
  var templateFile = DriveApp.getFileById("###");  // Please set your value.
  var fileName = "merge-test-doc"; // Please set your value.
  var destinationFolder = DriveApp.getFolderById("###"); // Please set your value.

  var newFile = templateFile.makeCopy(fileName, destinationFolder);

  const docId = newFile.getId();
  const doc = DocumentApp.openById(docId);
  const body = doc.getBody();
  const tables = body.getTables();
  const obj = Docs.Documents.get(docId).body.content;
  const requests = tables.slice(3).reduce((ar, t) => {
    const tidx = body.getChildIndex(t);
    const rows = t.getNumRows();
    for (let r = 2; r < rows; r++) {
      const col4 = t.getCell(r, 3).findElement(DocumentApp.ElementType.INLINE_IMAGE);
      if (!col4) {
        ar.push({ mergeTableCells: { tableRange: { columnSpan: 2, rowSpan: 1, tableCellLocation: { tableStartLocation: { index: obj[tidx + 1].startIndex }, rowIndex: r, columnIndex: 2 } } } });
        const cell = obj[tidx + 1].table.tableRows[r].tableCells[3];
        if (cell.content.some(e => e.paragraph.elements.some(f => f.textRun.content.trim()))) {
          ar.push({ deleteContentRange: { range: { startIndex: cell.startIndex + 1, endIndex: cell.endIndex - 1 } } });
        }
      }
    }
    return ar;
  }, []).reverse();
  if (requests.length == 0) return;
  Docs.Documents.batchUpdate({ requests }, docId);
}

测试:

运行该脚本,得到以下结果。

注:

  • 从您的显示图像来看,我认为第四个表之后的表有 2 个标题行。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.