跳到主要内容

删除空表格

删除文档中的所有空表格。

(function () {
let doc = Api.GetDocument();
let tables = doc.GetAllTables();

if (!tables || tables.length === 0) {
return;
}

let removedCount = 0;
let hasCellText = (cell) => {
let content = cell.GetContent();
let paragraphs = content.GetAllParagraphs();

for (let para of paragraphs) {
if (para.GetText().trim() !== "") {
return true;
}
}
return false;
};

for (let i = tables.length - 1; i >= 0; i--) {
let table = tables[i];
let hasContent = false;

let rowCount = table.GetRowsCount();
for (let row = 0; row < rowCount && !hasContent; row++) {
let cellCount = table.GetRow(row).GetCellsCount();
for (let cell = 0; cell < cellCount && !hasContent; cell++) {
if (hasCellText(table.GetCell(row, cell))) {
hasContent = true;
}
}
}

if (!hasContent) {
table.Delete();
removedCount++;
}
}
})();

使用的方法:获取文档获取所有表格获取单元格内容获取所有段落获取文本内容获取表格行数获取表格行获取单元格数量获取单元格删除表格

结果

删除空表格 删除空表格