Google apps script 使用google应用程序脚本合并文档中的两个单元格

Google apps script 使用google应用程序脚本合并文档中的两个单元格,google-apps-script,google-docs,Google Apps Script,Google Docs,在开始之前,我已经在互联网上搜索过这个主题,但很多信息都是旧的,当时无法使用UI在文档中创建合并单元格,但现在可以了 因为谷歌应用脚本支持页面说我需要在这个网站上寻求帮助 我想知道是否有可能将两个单元格(colspan)与google文档中最近的更改合并。应用程序脚本文档中有一个示例,但该示例涉及段落 Google文档中的表是由TableRow对象和TableCell对象组成的。可以将表看作是数组的数组。实际上,您可以使用一个数组数组来使用Body.appendTable()方法创建一个表 Ta

在开始之前,我已经在互联网上搜索过这个主题,但很多信息都是旧的,当时无法使用UI在文档中创建合并单元格,但现在可以了

因为谷歌应用脚本支持页面说我需要在这个网站上寻求帮助

我想知道是否有可能将两个单元格(colspan)与google文档中最近的更改合并。

应用程序脚本文档中有一个示例,但该示例涉及段落

Google文档中的表是由TableRow对象和TableCell对象组成的。可以将表看作是数组的数组。实际上,您可以使用一个数组数组来使用Body.appendTable()方法创建一个表

TableCells有一个merge()方法,用于将单元格与其前一个同级单元格合并。文本以换行符连接

合并两个单元格的代码如下所示:

function mergeCellsExample() {
  //We can use an array of arrays to populate our table.
  var cells = [
    ['First cell', 'Second cell'],
    ['First cell second row', 'Second cell second row']
  ];

  //Creates an example document in your Google Drive Root Folder.
  //It will be titled "Cell merge example"
  var table = DocumentApp.create('Cell merge example').getBody().appendTable(cells);

  // Get the first row in the table.
  var row = table.getRow(0);

  // Get the two cells this row.
  var cell1 = row.getCell(0);
  var cell2 = row.getCell(1);

  // Check the contents of cells 1 and 2
  Logger.log('Cell1 contents: %s', cell1.getText());
  Logger.log('Cell2 contents: %s', cell2.getText());

  // TableCell.merge() merges the current cell into its preceding sibling element.
  var merged = cell2.merge();  
  Logger.log('Merged cell contents: %s', merged.getText());

  // Cell1 and merged are the same cell
  Logger.log('Cell1 == Merged cell: %s', cell1.getText() == merged.getText());
  // Cell2 no longer exists
  Logger.log('Cell2 contents: %s', cell2.getText());
}

你好我已经试过了。事实上,它合并了细胞,但不会产生“colspan”效应。例如,我希望一行有一个单元格,但使用两个单元格的空间。merge函数将删除行中的第二个单元格,将内容与第一个单元格合并,但在下一行中,我将有两个单元格,表格将看起来不对称,因为前一行仅包含一个单元格,使用一个单元格的空间。我明白你的观点。预期的行为将与GoogleDocsGUI相同:一个单元格,跨越两列。我会将此报告为一个错误:。将
merge()
应用于类似于
my_table.getCell(0,5).merge()的行的最后一个单元格当前导致相关文档崩溃,并显示错误消息
无法加载文件