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

Sohaib435

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

user2069561

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using Apps Script, how can I display a google doc checklist from information in a google sheet?

How to delete selected text in a Google doc using Google Apps Script

Find specific words on Google Doc using Apps Script

Google Apps Script how to export specific sheet as csv format

How to get specific sheet by id in Google Apps script?

Formatting a table in Google Doc using Apps Script

How to get Google Doc using URL or ID in Apps Script?

How to remove a NamedRange in Google Sheet that contains specific text using Apps Script

Extract multiple links from one Google Sheet cell and then paste them in a Google Doc as hyperlinks using Google Apps Script

How to Preserve Sheet Formats and Translate? ( Google Apps Script, google sheet )

Need to Query Google Sheet for specific data from Google Apps Script

How to extract the pubhtml ID in google sheet using Apps Script?

Downloading a Google Slides presentation as PowerPoint doc using Google Apps Script?

Extract Text from google doc using google apps script

How to run a Google Apps Script on a hidden sheet

Import web data into google sheet using script

How do I add a comment to a Google Doc with Google Apps Script

How do I extract specific columns from google sheets to google doc using Google Script?

Wrap text using input from Google Sheet (Google Apps Script)

Upload Image in google sheet cell using google apps script?

Upload PDF file in Google sheet using Google apps script

How to Create an Array From JSON and Import the Values to Multiple Rows in a Google Sheet Using Google Apps Scripts

How do I change the sharing settings for a series of Google Doc ids in a Spreadsheet using Google Apps Script?

How to print the data of an array into a Google sheet with Google Apps Script

How to add the onOpen trigger to a Google Sheet with Google Apps Script?

Google Apps Script: Google Sheet Grouping

Retrieve the hyperlink on a specific text string within Google Doc using Apps Script

Add text and image to specific cell of a table in Google Doc using Apps Script

How to copy a range of data to another google sheet using google apps script?