I have a document that I extract text from to insert into my webpage. This document is getting larger as it covers more areas, so I want to streamline the process for my single-page application. Currently, I retrieve the entire document using the code snippet below:
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");
});
}
After retrieving the document, I use JavaScript to sort and display the content on the webpage.
To enhance performance, I would like to extract specific sections of text based on their headings using the Google API, like so:
async function printGoogleDoc(docID, apiKey, heading)
This function would return the text under the specified heading and stop at the next heading.
My question is, how can I achieve this functionality? Is it possible?
Here is a sample layout of the potential Google Doc for further clarification:
h1 Some Title
h2 sub title
content...
end of content
h2 second sub title
other contentI am using fetch for this task as there is no need for user authentication or authorization since the content is fetched and displayed on the webpage. Is there a way to selectively retrieve only the content under "h2 sub title" and ignore the rest?