To select the first sentence of each paragraph and check if it ends with a question mark, you can modify the script as follows:
This script now splits each paragraph into sentences and selects the first sentence only. Then, it checks if the first sentence ends with a question mark and converts the entire paragraph to an H3 heading if it does.
function convertQuestionToH3() {
var body = DocumentApp.getActiveDocument().getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var text = paragraph.getText();
// Split the paragraph into sentences
var sentences = text.split(/[.!?]/);
// Get the first sentence
var firstSentence = sentences[0].trim();
// Check if the first sentence ends with a question mark
if (firstSentence.slice(-1) === '?') {
// Convert the paragraph to H3 element
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
}
}
}