使用Google Apps脚本在文档中合并两个单元格 [英] Merge two cell in Docs with google apps script

查看:80
本文介绍了使用Google Apps脚本在文档中合并两个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Before start, i've searched about this topic on internet but many of the information is old, when was impossible to create a merged cell in Docs using the UI, however now it possible.

由于google apps脚本支持页面说我需要在该网站上寻求帮助.

Since the google apps script support page said that i need to ask for help in this site.

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

I want to know if is possible to merge two cell (colspan) with the recent changes in google Docs.

推荐答案

有一个,但该示例处理的是段落.

There's a pretty good explanation in the Apps Script Documentation, but the example deals with paragraphs.

Google文档中的表由填充了TableCell对象的TableRow对象组成.您可以将表视为数组的数组.实际上,您可以使用Body.appendTable()方法使用数组数组创建表.

Tables in Google Documents are made of TableRow objects filled with TableCell objects. You can think of a table as an array of arrays. In fact, you can use an array of arrays to create a table using the Body.appendTable() method.

TableCells具有一个merge()方法,该方法将一个单元格与其之前的同级单元格合并.文本以换行符开头.

TableCells have a merge() method that merges a cell with its preceding sibling cell. The text is joined with a newline.

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

The code for merging two cells would look something like this:

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());
}

这篇关于使用Google Apps脚本在文档中合并两个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆