Have you even wondered if it Is possible to change the appearance of text in Google Docs from a specific color to another color in one go. Well, the answer is yes and, in this article, we will teach you how to get this done using a Google Apps script.
The script looks like follows:
var ui = DocumentApp. getUi();
ui. createMenu(‘Replace’)
. addItem(‘Character conversion (red→blue)’, ‘replaceTextColor’)
. addToUi();
}
function replaceTextColor(){
const org_color = “#ff0000”; // red
const replace_color = “#1d33f1” // blue
let app = DocumentApp. getUi();
let body = DocumentApp. getActiveDocument(). getBody();
let obj = body. editAsText();
const text = body. getText();for(let i=0; i < text. length; i++){
if( obj. getForegroundColor(i) == org_color){
obj. setForegroundColor(i, i, replace_color);
Logger. log(“Change color: “+i);
}
}
}You can modify the source color (Red: #ff0000) on Line no 8 of the script and the Replacement color on the next line (Blue: #1d33f1) to whatever you require by changing the respective values.
How it Works:
1. Login to your Google Account and create a new Google Doc. Select some text in the document and change it to Red.
2. Click on Extension | App Script and this automatically navigates you to Google Apps Script.
3. In the Editor, delete any existing code and paste the Script that I have provided above.
4. Click on ‘Save Project’ and assign a name to it.
5. Now click on the ‘onOpen’ function in the top bar and then click on ‘Run’ and provide the necessary permissions for its execution (if prompted).
6. Verify if the script has executed by checking for the messages in the Execution Log.
7. Return to your Google Doc and you will fine a ‘Replace’ option added to the menu bar at the extreme right.
8. Click on Replace | Character conversion (red→blue) and you will find that all the text in Red color is converted to Blue color automatically in a single go.
The above process is very simple and easy and anyone can try it out. Go ahead and check out yourselves. Try changing the Hex values of the Source and Replacement color and see how it works.