Description
This report introduces a sample script for changing the glyph colors of bullets in lists on Google Documents using Google Apps Script. Currently, there are no built-in methods for achieving this using either the Document service (DocumentApp) or the Docs API. However, this can be accomplished through a specific process. This report will introduce that process using Google Apps Script.
Principle
When the foreground color of the text within a list is changed using the Google Document service (DocumentApp), the bullet glyphs are not affected. However, when the foreground color of all text within a list is changed using the Google Docs API, the bullet glyphs are changed. This script leverages this behavior. The detailed process is as follows:
- Retrieve lists from the Google Document using the Google Docs API.
- Retrieve the text ranges of each list.
- Retrieve the foreground colors of the text within each list using the Document service (DocumentApp).
- Set the foreground color of all text in each list to the desired color using the Docs API. This will also change the bullet glyph colors.
- Restore the foreground color of all text in each list to its original color using the Document service.
Usage
In order to test the sample script, please do the following steps.
1. Create a new Google Document
Create a new Google Document and prepare a sample list as follows.
2. Enable Docs API
Open the script editor on the Google Document and enable Google Docs API at Advanced Google services. Ref
3. Sample script
Copy and paste the following script to the script editor and save the script.
/**
* Change the Glyph color of lists on Google Documents.
*
* @param {Array} listNumbers Glyph color of 1st list is changed. The 1st list number is 0.
* @param {String} newColor Change Glyph color as HEX.
* @return {void}
*/
function changeGlyphColor_(listNumbers, newColor) {
// Retrieve list object using Docs API.
const doc = DocumentApp.getActiveDocument();
const docId = doc.getId();
const listObj = Docs.Documents.get(docId).body.content.filter(
(e) => e.paragraph && e.paragraph.bullet
);
const cLists = listNumbers.reduce((ar, e) => {
const l = listObj[e];
if (l) {
ar.push(l);
}
return ar;
}, []);
if (cLists.length == 0) {
console.warn("No lists");
return;
}
// Retrieve list object using Document service.
const body = doc.getBody();
const lists = body.getListItems();
const dLists = listNumbers.map((e) => {
const t = lists[e].editAsText();
return [...Array(t.getText().length)].map(
(_, f) => t.getForegroundColor(f) || "#000000"
);
});
// Create request body and call the batchUpdate method for Docs API.
const color = SpreadsheetApp.newColor().setRgbColor(newColor).asRgbColor();
const red = color.getRed() / 255;
const green = color.getGreen() / 255;
const blue = color.getBlue() / 255;
const textStyle = {
foregroundColor: { color: { rgbColor: { red, green, blue } } },
};
const reqs1 = cLists.map(({ startIndex, endIndex }) => ({
updateTextStyle: {
textStyle,
fields: "foregroundColor",
range: { startIndex, endIndex },
},
}));
Docs.Documents.batchUpdate({ requests: reqs1 }, docId);
// Back the font color to the original one.
listNumbers.forEach((e, i) => {
const t = lists[e].editAsText();
dLists[i].forEach((f, j) => t.setForegroundColor(j, j, f));
});
}
function main() {
const listNumbers = [0, 2]; // Glyph color of 1st list is changed. The 1st list number is 0.
const newColor = "#FF0000"; // Change Glyph color to red.
changeGlyphColor_(listNumbers, newColor);
}
4. Testing
Run the function main
. This will change the glyph colors of the bullets to red, as shown below.
5. Appendix
As shown in the image above, when you want to change the glyph colors of deeply nested lists, you need to specify the list numbers. For example, using the image above, the list numbers for “sample1a” and “sample2” would be 1 and 2, respectively.