如何从 DocumentApp、apps 脚本中的 getParagraphs() 中排除 table 个单元格段落
How to exclude table cells paragraphs from getParagraphs() in DocumentApp, apps script
我需要从 google 文档中获取所有段落。为此,我使用 body.getParagraphs()
。
问题是,在任何文档中,如果存在任何 table,则 body.getParagraphs()
方法会将所有 table 单元格作为一个段落,并将 returns 作为一个段落。现在我想要的是仅从文档中获取所有段落,并从中排除 table 和 table 单元格。我怎样才能做到这一点?
谢谢!
我相信你的目标如下。
- 您想使用 Google Apps 脚本检索除 table 单元格之外的段落。
在这种情况下,如何检查元素类型?当这反映到脚本中时,它变成如下。
示例脚本:
请将以下脚本复制并粘贴到 Google 文档的脚本编辑器中。
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
const res = paragraphs.filter(p => p.getParent().getType() != DocumentApp.ElementType.TABLE_CELL);
// do something
}
- 在此示例脚本中,
paragraphs
的值包括 table 个单元格的段落。
- 通过过滤元素类型,
res
可以只包含段落 table 单元格。
参考文献:
我需要从 google 文档中获取所有段落。为此,我使用 body.getParagraphs()
。
问题是,在任何文档中,如果存在任何 table,则 body.getParagraphs()
方法会将所有 table 单元格作为一个段落,并将 returns 作为一个段落。现在我想要的是仅从文档中获取所有段落,并从中排除 table 和 table 单元格。我怎样才能做到这一点?
谢谢!
我相信你的目标如下。
- 您想使用 Google Apps 脚本检索除 table 单元格之外的段落。
在这种情况下,如何检查元素类型?当这反映到脚本中时,它变成如下。
示例脚本:
请将以下脚本复制并粘贴到 Google 文档的脚本编辑器中。
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
const res = paragraphs.filter(p => p.getParent().getType() != DocumentApp.ElementType.TABLE_CELL);
// do something
}
- 在此示例脚本中,
paragraphs
的值包括 table 个单元格的段落。 - 通过过滤元素类型,
res
可以只包含段落 table 单元格。