Search code examples
google-apps-scriptgoogle-sheetsgoogle-docs

How to import specific text/line under headings in Google doc to Google sheet using google apps script?


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:

Google sheets

I am looking to export the Question text/line from every heading to google sheets like this:

Google sheet

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


Solution

  • There are a couple of assumptions for this script:

    • There is a finite number of header styles you're using (e.g. 'Header 1' and 'Header 2' in my example below)
    • Your questions always contain 'QUESTION'
    • There is no other text apart from the headers and the question lines (but if there is, in principle it will be skipped)

    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.