【问题标题】:Merge two cell in Docs with google apps script将文档中的两个单元格与谷歌应用程序脚本合并
【发布时间】:2016-01-15 16:27:58
【问题描述】:

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

由于谷歌应用脚​​本支持页面说我需要在这个网站寻求帮助。

我想知道是否可以将两个单元格(colspan)与谷歌文档中的最新更改合并。

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    Apps 脚本文档中有一个pretty good explanation,但该示例处理的是段落。

    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”效果。例如,我想要一行有一个单元格但使用两个单元格的空间。合并功能将删除行中的第二个单元格,将内容与第一个单元格合并,但在下一行中,我将有两个单元格,表格看起来不对称,因为前一行仅包含一个单元格,使用一个空间细胞。
    • 我明白你的意思。预期的行为将与 Google Docs GUI 相同:一个单元格,跨越两列。我会将此报告为错误:Google Apps Script Issues.
    • 在文档绑定脚本中将merge() 应用于像my_table.getCell(0,5).merge(); 这样的行的最后一个单元格当前会导致相关文档崩溃并显示错误消息Unable to load file
    • 它仍然像@user1063287提到的那样崩溃。
    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多