gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:01:26 24 4
gpt4 key购买 nike

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

由于 google apps 脚本支持页面说我需要在此站点寻求帮助。

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

最佳答案

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

Google 文档中的表格由填充有 TableCell 对象的 TableRow 对象组成。您可以将表视为数组的数组。事实上,您可以使用 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());
}

关于google-apps-script - 使用谷歌应用程序脚本合并文档中的两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34815573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com