Using Google API, can I retrieve sections of a doc based on headings?

I have a doc that I read the text from and insert to my webpage. This doc is expanding to cover multiple areas and I would like to try and make the process more efficient since my webpage is a single page application. Currently I get the entire doc using this

async function printGoogleDoc(docID, apiKey){
await fetch(`https://www.googleapis.com/drive/v3/files/${docID}/export?mimeType=text/plain&key=${apiKey}`)
.then(function(res) {
    return res.text();
}).then(function(text) {
    console.log(text);
}).catch(function() {
    console.log("error");
});
}

Then I read through it and sort the content for the webpage with JavaScript.

I would like to improve performance a little by just using google api to get sections of text based on their headings if possible.

Something like

async function printGoogleDoc(docID, apiKey, heading)

and it would return all text under that heading and stopping at the next.

But how would I do this? Is there a way?

  • I have to apologize for my poor English skill. Unfortunately, I cannot imagine your situation. In order to correctly understand your question, can you provide the sample input and output values you expect? First, I would like to correctly understand your question.

    – 




Google recommends using their libraries (code) to access the Google Docs Service.

we recommend that you use the Google-provided client libraries.

You are not using Google code to interact with the Docs Service. You are using the standard REST API.

Assuming you want to use the REST API, there does exist the concept of a document that is returned. This is not the Document Object Model (DOM) document but is a Google crafted “document” that is explained in Google documentation which includes a header property. However this header refers to header and footer sections of a page and not heading styled content (or HTML header elements H1-H6).

Long story short, it’s doable. I would “download as” webpage from Google Docs using the REST API. Do this by setting the mimetype to text/html in your REST call.

Then, use a basic tool such as JSDOM’s CSS selectors (via document.querySelectorAll) to get at the headings and adjacent content with something like nextSibling.

Leave a Comment