【发布时间】:2016-01-15 16:27:58
【问题描述】:
在开始之前,我在互联网上搜索过这个主题,但很多信息都是旧的,以前不可能使用 UI 在文档中创建合并单元格,但现在可以了。
由于谷歌应用脚本支持页面说我需要在这个网站寻求帮助。
我想知道是否可以将两个单元格(colspan)与谷歌文档中的最新更改合并。
【问题讨论】:
标签: google-apps-script google-docs
在开始之前,我在互联网上搜索过这个主题,但很多信息都是旧的,以前不可能使用 UI 在文档中创建合并单元格,但现在可以了。
由于谷歌应用脚本支持页面说我需要在这个网站寻求帮助。
我想知道是否可以将两个单元格(colspan)与谷歌文档中的最新更改合并。
【问题讨论】:
标签: google-apps-script google-docs
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());
}
【讨论】:
merge() 应用于像my_table.getCell(0,5).merge(); 这样的行的最后一个单元格当前会导致相关文档崩溃并显示错误消息Unable to load file。