简体   繁体   English

编写Doc Google Table中的Google Merge单元格脚本

[英]Script Google Merge cells in a Doc Google Table

I try to do a script in order to merge 2 cells(or several) in a Google Document.Anyway the result is not so good as I expected because I have an error to refresh the page and visually the merged cell is not so long as the table The result of the merge and the initial table is enter image description here 我试图做一个脚本来合并一个Google文档中的2个(或几个)单元格,但是结果却不如我预期的那样好,因为刷新页面时出现错误并且视觉上合并的单元格没有该表的合并和初始表的结果是在 此处输入图像描述

My Google script is : 我的Google脚本是:

 function onOpen() { // Add a menu with some items, some separators, and a sub-menu. DocumentApp.getUi().createMenu('Sample') .addItem('Fusion cellules tableau', 'mergeCells') .addToUi(); } function mergeCells() { var body = DocumentApp.getActiveDocument().getBody(); for (var p= 0; p< body.getNumChildren(); p++) { var child = body.getChild(p); if (child.getType() == DocumentApp.ElementType.TABLE){ // Assume we've already located our table var table = child; var tableRow = table.getChild(2); // gets third row var tableCell = tableRow.getChild(1); // gets second cell in row tableCell.merge(); // Merges seconde cell with first cell. } } } 

So if you have an idea I will be very happy :) 因此,如果您有想法,我会很高兴的:)

Sorry, that seems very frustrating. 抱歉,这似乎很令人沮丧。 I was able to reproduce your issue, and it looks like a bug with the way the .merge() method works with table cells. 我能够重现您的问题,并且.merge()方法与表单元格一起工作的方式看起来像是个错误。 The Scripts .merge() combines an element's contents with the preceding element's and then deletes that second element. 脚本.merge()将元素的内容与前一个元素的内容合并,然后删除该第二个元素。 This is actually different from how merging cells (in the Document) works. 这实际上与(文档中)合并单元格的工作方式不同。 When you merge cells from the document it doesn't actually delete any elements, just sort of changes the display so that you can't access the "hidden" cell. 当您合并文档中的单元格时,它实际上不会删除任何元素,只需对显示进行某种更改,以使您无法访问“隐藏”的单元格。 If you call getNumChildren() on a row with a merged cell it still has the full number of columns. 如果您在具有合并单元格的行上调用getNumChildren(),它仍然具有完整的列数。

Running .merge() on a cell deletes the second child and seems to create an unallowed table structure, which is probably why it throws an error. 在单元格上运行.merge()会删除第二个子级,似乎会创建一个不允许的表结构,这可能是它引发错误的原因。

By the way there is a .getRowSpan() method you can call on a cell, which returns 1 for normal cells and 0 for merged cells, however, inconveniently, there is no "setRowSpan()", so it doesn't look like you can do the regular style merge from a script. 顺便说一下,您可以在一个单元格上调用.getRowSpan()方法,对于普通单元格它返回1,对于合并单元格它返回0,但是,不方便的是,没有“ setRowSpan()”,所以它看起来不像您可以通过脚本进行常规样式合并。

There is a similar question here from a couple years ago. 几年前这里也有类似的问题

Also here is the bug listed with Google. 这也是 Google列出的错误 It is from 5(!) years ago. 它是从5(!)年前开始的。 You can try to post there. 您可以尝试在那里发布。

Maybe you can try to copy the contents over with the script and change the border style/width in between the cells but that seems like a huge pain. 也许您可以尝试使用脚本来复制内容并更改单元格之间的边框样式/宽度,但这似乎是一个巨大的痛苦。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM