简体   繁体   English

Google Docs Apps Script 获取列数

[英]Google Docs Apps Script get number of columns

On a Google Doc you can set columns from Format > Columns.在 Google Doc 上,您可以从“格式”>“列”中设置列。 Now, though, I want to access those columns from Apps Script to confirm the number of columns.不过,现在我想从 Apps 脚本访问这些列以确认列数。 I don't even need to modify them, just access.我什至不需要修改它们,只需访问即可。 I haven't found anything that jumps out at me from the documentation, so I didn't know if there's an extension of the Document service that would allow for such.我没有发现任何从文档中跳出来的东西,所以我不知道是否有允许这样的文档服务的扩展。 I'm sorry not to include any code, but I have no code obvious to show.很抱歉没有包含任何代码,但我没有明显的代码可以显示。 I did create a document with 2 columns to see exactly what I'm talking about.我确实创建了一个包含 2 列的文档以确切了解我在说什么。 https://docs.google.com/document/d/1MyttroeN4kPUm9PfYZnTe_gJqstM3Gb5q3vS3c84dNw/edit https://docs.google.com/document/d/1MyttroeN4kPUm9PfYZnTe_gJqstM3Gb5q3vS3c84dNw/edit

Answer回答

It is possible using the get method of the Google Docs API可以使用Google Docs APIget方法

How to do it怎么做

  1. In Apps Script, enable the Advanced Docs Service .在 Apps 脚本中,启用高级文档服务
  2. Use the method get .使用方法get
  3. Check the array called content .检查名为content的数组。
  4. Search an object called sectionBreak in each element of content .content每个元素中搜索一个名为sectionBreak的对象。
  5. Check that the object has the following data: sectionBreak>sectionStyle>columnProperties .检查对象是否具有以下数据: sectionBreak>sectionStyle>columnProperties
  6. Take the length of the array columnProperties .取数组columnProperties的长度。
  7. (keep in mind that the first occurrence of columnProperties is in the first element of content , skip it and start the loop from the second one. (请记住,第一次出现columnProperties是在content的第一个元素中,跳过它并从第二个元素开始循环。

Code代码

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) {
    }

  }
}

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM