删除空表格
删除文档中的所有空表格。
(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++;
}
}
})();
使用的方法:获取文档,获取所有表格,获取单元格内容,获取所有段落,获取文本内容,获取表格行数,获取表格行,获取单元格数量,获取单元格,删除表格
结果