【发布时间】:2021-09-17 17:18:05
【问题描述】:
我有一个 Google Apps 脚本,它从 Google 表格中获取数据并将其复制到带有表格的 Google 幻灯片模板中。在某些情况下,工作表中的数据行数不如模板表中的行数多,因此表中的某些行会部分留空(请参见下表的屏幕截图)。我编写了一个从 Google 幻灯片表中删除空行的函数(请参见下面的所需结果屏幕截图),并且它在没有任何合并单元格的其他表上运行良好(修改为使用 rowIndex++ 而不是 rowIndex = rowIndex + 3在 for 循环中)。
function removeEmptyRowsTest(removeId) {
var slides = SlidesApp.openById(removeId).getSlides();
var metricExcepSlide = slides[0];
var metricExcepTables = metricExcepSlide.getTables();
var table = metricExcepTables[0];
var numberOfRows = table.getNumRows();
for (var rowIndex = 1; rowIndex < numberOfRows; rowIndex = rowIndex+3) {
var nextRow = table.getRow(rowIndex);
// A row is assumed empty until proven otherwise
var foundEmptyRow = false;
//Logger.log(numberOfColumns)
Logger.log(foundEmptyRow)
Logger.log(nextRow.getCell(0).getText().asString())
Logger.log(nextRow.getCell(0).getText().asString().length)
if (nextRow.getCell(0).getText().asString().length == 1) {
foundEmptyRow = true;
}
if (foundEmptyRow) {
table.getRow(rowIndex).remove()
numberOfRows--;
rowIndex--;
}
}
}
但是当我在下图中运行该函数时,我收到错误Exception: This operation is only allowed on the head (upper left) cell of the merged cells.。我很困惑,因为我觉得将 rowIndex 初始化为 1 并递增 3 应该只适用于三个合并单元格的左上角单元格。当我将rowIndex 初始化为 0 和/或将其递增 1 时,我得到了同样的错误。有人知道我怎样才能避免这个错误并删除表格底部的空行吗?
【问题讨论】:
-
为了复制您的问题,您能否提供包含示例表的示例幻灯片?另外,您能否从示例输入表中显示您的目标图像?
-
嗨@Tanaike,我已对问题进行了要求的编辑。如果您在访问示例幻灯片时遇到任何问题,请告诉我
-
感谢您的回复和提供更多信息。根据您的附加信息,我提出了一个答案。你能确认一下吗?如果我误解了您的问题,我深表歉意。
标签: javascript google-apps-script google-slides