我有一個Google Apps腳本,它從Google作業表中獲取資料并將其復制到帶有表格的Google Slides模板中。在某些情況下,作業表中的資料行數與模板表中的資料行數不一樣,因此,表中的一些行被留成部分空白(見下面的表格截圖)。我寫了一個函式,可以從Google Slides表格中移除空行(見下面的預期結果截圖),它在其他沒有任何合并單元格的表格中也很好用(在for回圈中使用rowIndex 而不是rowIndex = rowIndex 3的修改)。
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)。
//一個行被認為是空的,直到證明是空的。
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--;
}
}
但是當我在下圖的表格上運行這個函式時,我得到了錯誤的提示 uj5u.com熱心網友回復: 我相信你的目標是如下的。
在這種情況下,下面的修改如何呢?在這個修改中,"HEAD "和 "MERGED "是使用
標籤: 下一篇:沒有懸停的懸停效果
Exception: 這個操作只允許在合并的單元格的頭部(左上方)進行。我很困惑,因為我覺得將rowIndex初始化為1并增量為3應該只適用于三個合并單元格集合的左上角。當我將rowIndex初始化為0和/或增量為1時,我得到了同樣的錯誤。 有誰知道我怎樣才能避免這個錯誤并洗掉表格底部的空行?
getMergeState的方法來檢查。使用了反向回圈。修改后的腳本:
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()。
// I modified below script.
var checkCols = [0, 2, 3, 4] 。
for (var r = numberOfRows - 1; r > 0; r-) {
if (table.getRow(r).getCell(0) 。 getMergeState() == SlidesApp.CellMergeState.HEAD) {
var checkEmpty = checkCols.filter(span class="hljs-params">e => table.getRow(r) 。 getCell(e).getText()。 asString().trim() !="")。
if (checkEmpty.length == 0) {
for (var dr = 3; dr > 0; dr-) {
table.getRow(r dr - 1).remove()。
}
}
}
}
}
參考資料:



