如何使用应用程序脚本查找 google 文档是否为空

How to find whether the google document is empty using apps script

我要查找文档是否为空

我试过

if (DocumentApp.getActiveDocument().getBody().getText().length < 1); {
    showAlert('Document is empty')
    return;
}

但这会绕过文档中的图像或任何其他特殊项目。

如何确定文档为空或完全为空?

请将此视为几个答案之一。如何比较文档和新文档的文件大小?发现从包含部分图片、文字和评论的Document中删除所有图片、文字和评论后,大小变成了新Document的大小。我以为用这个可以知道Document是否为空

示例脚本:

var id = "### Document ID ###"; // Please input ID here.

var tempId = DocumentApp.create("temp").getId();
var newdocument = DriveApp.getFileById(tempId).getBlob().getBytes().length;
var document = DriveApp.getFileById(id).getBlob().getBytes().length;
if (newdocument == document) {
  Logger.log("Empty")
}
DriveApp.removeFile(DriveApp.getFileById(tempId));

注:

  • 因为我担心每个环境的文件大小可能不同,在示例脚本中,我使用 temp 文档检索了大小。此 temp 文档在脚本的最后被删除。

不知道这个方法对你有没有用。如果这没有用,我很抱歉。