Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script for Google Docs, gently turning white text to black in the first paragraph of a document.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraph = body.getChild(0);
var txt = paragraph.asParagraph().getChild(0); //This returns the text in the first paragraph.
var len = txt.asText().getText().length; //This returns the number of characters in the first paragraph.
Logger.log(len);
var red = 250;
var green = 250;
var blue = 250;
var incrementVal = 250/len;
var x = 0;
var rgbHex = rgbToHex(red, green, blue);
for (var i = 0; i<len; i++) {
string = txt.asText().getText().charAt(i);
if (red>0 && string!=" ") {
rgbHex = rgbToHex(red, green, blue);
txt.asText().setForegroundColor(i,i,rgbHex);
x = x + incrementVal
if (x>1) {
red = red-1;
green = green-1;
blue = blue-1;
x = 0
};
};
};
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment