Google Docs Apps 脚本获取列数

Google Docs Apps Script get number of columns

在 Google 文档中,您可以从“格式”>“列”中设置列。不过,现在我想从 Apps 脚本访问这些列以确认列数。我什至不需要修改它们,只需访问即可。我没有从文档中发现任何让我印象深刻的东西,所以我不知道是否有允许这样的文档服务的扩展。 很抱歉没有包含任何代码,但我没有明显的代码可以显示。我确实创建了一个包含 2 列的文档以准确了解我在说什么。 https://docs.google.com/document/d/1MyttroeN4kPUm9PfYZnTe_gJqstM3Gb5q3vS3c84dNw/edit

回答

可以使用 get method of the Google Docs API

怎么做

  1. 在 Apps 脚本中,启用 Advanced Docs Service
  2. 使用方法get.
  3. 检查名为 content 的数组。
  4. content 的每个元素中搜索名为 sectionBreak 的对象。
  5. 检查对象是否有以下数据:sectionBreak>sectionStyle>columnProperties.
  6. 取数组长度columnProperties.
  7. (记住第一次出现的columnProperties在[=12=的第一个元素中,跳过它并从第二个开始循环。

代码

function myFunction() {
  var id = DocumentApp.getActiveDocument().getId()
  var result = Docs.Documents.get(id)
  var content = result["body"]["content"]
  for (var i = 1; i < 20; i++) {
    try {
      var cols = content[i]["sectionBreak"]["sectionStyle"]["columnProperties"]
      console.log('number of columns: ' + cols.length)
    } catch (error) {
    }

  }
}

参考