Reputation: 457
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
Upvotes: 0
Views: 989
Reputation: 1
You can do this without the advanced service using try-catch:
function numCols(table) {
cols = 0;
table = table.asTable();
while (true) {
try {
table.getColumnWidth(cols);
cols++;
} catch {break;}
}
return cols;
}
Upvotes: 0
Reputation: 1461
It is possible using the get method of the Google Docs API
get
.content
.sectionBreak
in each element of content
.sectionBreak>sectionStyle>columnProperties
.columnProperties
.columnProperties
is in the first element of content
, skip it and start the loop from the second one.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) {
}
}
}
Upvotes: 2