Google Apps Script that removes a number from a page element with specific identifiers


To create a Google Apps Script that removes a number located directly after the word “Question” and before a colon (“:”) in a Google Docs document, follow these steps:

  1. Open your Google Docs document.
  2. Click on “Extensions” in the menu.
  3. Go to “Apps Script”.
  4. Delete any code in the script editor and paste the following script:

function removeQuestionNumber() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.getText();
var paragraphs = body.getParagraphs();

for (var i = 0; i < paragraphs.length; i++) {
var paragraphText = paragraphs[i].getText();
// Check if the paragraph starts with “Question ” followed by a number and then a colon
if (/^Question \d+:/.test(paragraphText)) {
// Remove the number and the space after “Question”
var updatedText = paragraphText.replace(/Question \d+:/, ‘Question:’);
paragraphs[i].setText(updatedText);
}
}
}

  1. Save the script with a name, for example, “Remove Question Number”.
  2. To run the script, click on the play/triangle button next to the function removeQuestionNumber in the Apps Script editor. You may need to give permissions to the script to access your Google Docs document the first time you run it.

This script goes through each paragraph in your document, checks if it starts with the word “Question” followed by a number and then a colon (e.g., “Question 1:”). If such a pattern is found, it removes the number and the space after “Question”, leaving just “Question:” followed by the rest of the original paragraph text.

Remember, you need to grant the necessary permissions for the script to run the first time. The script might not work perfectly with very complex documents or if the text pattern is different than expected. Adjustments may be needed depending on the exact format of your document.

This code will remove the number value between the word question and the colon

Question 20: What method is used to translate the origin of the canvas context?

will become

Question: What method is used to translate the origin of the canvas context?