On a Google Doc you can set columns from Format > Columns. Now, though, I want to access those columns from Apps Script to confirm the number of columns. 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. https://docs.google.com/document/d/1MyttroeN4kPUm9PfYZnTe_gJqstM3Gb5q3vS3c84dNw/edit
0
votes
1 Answers
1
votes
Answer
It is possible using the get method of the Google Docs API
How to do it
- In Apps Script, enable the Advanced Docs Service.
- Use the method
get. - Check the array called
content. - Search an object called
sectionBreakin each element ofcontent. - Check that the object has the following data:
sectionBreak>sectionStyle>columnProperties. - Take the length of the array
columnProperties. - (keep in mind that the first occurrence of
columnPropertiesis in the first element ofcontent, skip it and start the loop from the second one.
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) {
}
}
}
ColumBreakis not the thing you want. It's just a break symbol. You can insert it via Insert > Break > Column break. As for API, here is a sample how it can be used: stackoverflow.com/questions/68018349/… - Yuri Khristich