如何使用 MergeTableCellsRequest 根据 google docs 文档中的单元格内容合并表格单元格

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

我有一个包含多个表格的文档,每个表格都有两个标题行和四列。每个表都有不同数量的内容行,第四列可能包含也可能不包含照片(文档的内容是从 appsheet 上提交的表单导入的)。 Here is an example 我想做的就是循环遍历文档中的每个表格,如果第四列不包含照片,则将第四列与前一列合并,like so

我最初尝试使用 merge(),并得到了here描述的相同结果。我尝试实现该帖子中的代码来创建一个非常基本的函数来执行我需要的操作,但我不断收到错误“GoogleJsonResponseException:对 docs.documents.batchUpdate 的 API 调用失败并出现错误:无效的请求[0].mergeTableCells:提供的表起始位置无效。'。任何人都可以帮忙如何确定每个表的起始位置吗?

//make a copy of the template report to work on
var newFile = templateFile.makeCopy(fileName, destinationFolder);
var fileToEdit = DocumentApp.openById(newFile.getId()); // get the file
var body = fileToEdit.getBody(); // get the file body

function mergeCells() {
  var tabnum = 0;
  body.getTables().forEach(callback); // Iterate over all tables

  function callback(table) {

    if (tabnum > 2) { // only do this for table 3 and beyond
      var i = 2; // set start row to 2 (as there are 2 header rows for each table)
      // for each table, loop through each row 
      for (i; i < table.getNumRows(); i++) {
        if (table.getCell(i, 3).getText() == "") { //if last column is blank - merge with preceding column
          var currTableInd = body.getChildIndex(table); //get the index of the current table to create the mergeTableCellsRequest
          var requests = [
            {
              mergeTableCells: {
                tableRange: {
                  tableCellLocation: {
                    tableStartLocation: {
                      index: currTableInd,
                    },
                    rowIndex: i,
                    columnIndex: 3,
                  },
                  "rowSpan": 1,
                  "columnSpan": 2
                },
              },
            },
          ];

          Docs.Documents.batchUpdate({ requests }, newFile.getId());
        }
      }
    }
    tabnum++; // add 1 to table index as loop through to next table
  }

}
google-apps-script google-docs google-docs-api
1个回答
0
投票

修改要点:

  • 从您显示的图像来看,当表格第4列中的图像是内嵌图像时,我认为
    table.getCell(i, 3).getText() == ""
    不能用于检查图像。
  • 我认为
    var currTableInd = body.getChildIndex(table)
    不能用于
    index
    tableStartLocation
  • 当循环使用batchUpdate方法时,过程成本会变高。

当这些要点反映在示例脚本中时,下面的示例脚本怎么样?

示例脚本:

在运行此脚本之前,请在高级 Google 服务中启用 Google Docs API

请设置您的变量

templateFile
fileName
destinationFolder

function myFunction() {
  var templateFile = DriveApp.getFileById("###");  // Please set your value.
  var fileName = "###"; // 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.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);
}

测试:

使用该脚本时,得到以下结果。

注:

  • 从您显示的图像来看,我认为您第 4 列中的图像是内联图像。请注意这一点。

参考资料:

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