To create a Google Apps Script that changes paragraphs styled as Heading 3 (H3) to regular paragraphs in a Google Doc, unless they end with a question mark (in which case they are left as H3), you can follow these steps. This script uses Google Apps Script, a Javascript-based platform that lets you extend Google Apps and create custom functionality.
- Open your Google Doc: Open the Google Doc you want to modify.
- Open Script Editor: Go to
Extensions
>Apps Script
. This opens the Apps Script editor in a new tab. - Replace the Code: In the Apps Script editor, delete any code in the editor and replace it with the following script:
- Save and Name Your Project: Click on
File
>Save
, and give your project a name. - Run the Script: Click on the play/run button (▶️) next to the
changeH3ToNormal
function in the Apps Script editor to run your script. You might need to authorize the script to run under your Google account the first time. - Authorize the Script: Follow the on-screen instructions to grant the necessary permissions. You might see a warning that the script isn’t verified. This is normal for scripts you’ve created yourself. Click on
Advanced
and thenGo to [YourProjectName] (unsafe)
to proceed with the authorization.
After running the script, all paragraphs in your document that were styled as Heading 3 and do not end with a question mark will be changed to regular paragraphs. Paragraphs styled as Heading 3 that end with a question mark will remain unchanged.
function changeH3ToNormal() {
var doc = DocumentApp.openById(DOCID);
var body = doc.getBody();
var paragraphs = body.getParagraphs();
paragraphs.forEach(function(paragraph) {
// Check if the paragraph's heading is HEADING3
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
var text = paragraph.getText();
// Check if the text ends with a question mark
if (text.endsWith('?')) {
// If it ends with a question mark, do nothing (keep it as HEADING3)
} else {
// If it does not end with a question mark, change it to normal text
paragraph.setHeading(DocumentApp.ParagraphHeading.NORMAL);
}
}
});
}