I am trying to get specific text/line that is under the heading or subheading using the google apps script. Here is the screenshot of the Google doc:
I am looking to export the Question text/line from every heading to google sheets like this:
So far I have been able to get all the headings from the google doc by this code snippet:
function getHeadings() {
var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
var hdg = [];
for(var i=0; i<pars.length; i++) {
var par = pars[i];
hdg.push(par.getHeading());
}
Logger.log(hdg)
}
But I am unable to get the Question text under all these headings, kindly can you guide me in the right direction? (I apologize for my question formatting, I am a newbie on this website). Thank you
There are a couple of assumptions for this script:
In that case, the below code will work:
function getHeadings() {
var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
var currentHdg = "";
var questions = [];
for(var i=0; i<pars.length; i++) {
var par = pars[i];
var text = par.getText();
var hdg = par.getHeading();
if (hdg.toString().indexOf("HEADING") > -1){
currentHdg = text;
}
else if(text.indexOf("QUESTION") > -1){
questions.push([currentHdg,text.replace("QUESTION","").trim()]);
}
}
Logger.log(questions);
}
You can then format questions
into the table output format you need.
Edit: I have updated my answer to cover all heading types.