This is a script for modifying the shading color of paragraph on Google Document using Google Apps Script.
Recently, by releasing Google Docs API, the shading color got to be able to be modified using the script. Here, I would like to introduce a sample script for modifying the shading color of the paragraph on Google Document. At the current Document Service, the shading color cannot be modified yet. I think that this will be achieved in the future update. At that time, I think that a new property might be added to Enum Attribute.
In order to use the following sample script, before you run the script, please enable Google Docs API at API console as follows.
Enable Google Docs API at API console:
- On script editor
- Resources -> Cloud Platform project
- View API console
- At Getting started, click “Explore and enable APIs”.
- At left side, click Library.
- At “Search for APIs & services”, input “Docs”. And click “Google Docs API”.
- Click Enable button.
- If API has already been enabled, please don’t turn off.
Flow:
The flow of sample script is as follows.
- You select a paragraph on Google Document.
- Retrieve all contents from Google Document using the get method of Google Docs API.
- Each content is corresponding to each paragraph of Document.
- Retrieve the selected content using the getSelection method of Document Service.
- Modify shading color of selected content using the batchUpdate method of Google Docs API.
Sample script:
The sample script is for the container-bound script with Google Document. Please copy and paste the following script to the script editor, and please select a text on Google Document run myFunction()
after Google Docs API was enabled.
function myFunction() {
var color = "#39b966"; // Please set a color with hex.
var doc = DocumentApp.getActiveDocument();
var selection = doc.getSelection();
if (selection) {
var hex2rgb = function(hex) {
var str = hex[0] === "#" ? hex.slice(1) : hex;
var rgb = str.split(/(.{2})/).reduce(function(ar, e) {
if (e) ar.push(Math.round(1000 * parseInt(e, 16) / 255) / 1000);
return ar;
}, []);
return {red: rgb[0], green: rgb[1], blue: rgb[2]};
}(color);
var body = doc.getBody();
var id = doc.getId();
var baseUrl = "https://docs.googleapis.com/v1/documents/";
var headers = {"Authorization": "Bearer " + ScriptApp.getOAuthToken()};
var params = {headers: headers};
// Retrieve all contents from Google Document.
var res = UrlFetchApp.fetch(baseUrl + id + "?fields=*", params);
var obj = JSON.parse(res.getContentText());
// Retrieve selected content.
var rangeElements = selection.getRangeElements();
var selectedContents = rangeElements.reduce(function(ar, e) {
var p = e.getElement();
if (p.getType() == DocumentApp.ElementType.TEXT) p = p.getParent();
if (p.getType() == DocumentApp.ElementType.PARAGRAPH || p.getType() == DocumentApp.ElementType.LIST_ITEM) ar.push(obj.body.content[body.getChildIndex(p) + 1]);
return ar;
}, []);
// Modify shading of selected content.
if (selectedContents.length > 0) {
var range = selectedContents.length == 1 ? {startIndex: selectedContents[0].startIndex, endIndex: selectedContents[0].endIndex} : {startIndex: selectedContents[0].startIndex, endIndex: selectedContents[selectedContents.length - 1].endIndex};
var resource = {requests: [{updateParagraphStyle: {
paragraphStyle: {shading: {backgroundColor: {color: {rgbColor: hex2rgb}}}},
range: range,
fields: "shading.backgroundColor",
}}]};
params.method = "post";
params.contentType = "application/json";
params.payload = JSON.stringify(resource);
UrlFetchApp.fetch(baseUrl + id + ":batchUpdate", params);
}
}
}
Result:
Scopes:
In this sample script, the following 2 scopes are used. But when you copy and paste the above sample script to the script editor and save it, the following scopes are automatically detected. So you are not required to modify the Manifest file.
https://www.googleapis.com/auth/documents
https://www.googleapis.com/auth/script.external_request
References:
Motivation:
Although I had been looking for the script for modifying the shading color of the paragraph on Google Document using a script before, at that time, I had never found it. The reason of this had been to be able not to be achieved by the Document Service of Google Apps Script. But I thought that Google Docs API released recently by the recent Google’s update might be able to be achieved this, because I noticed that the property of shading
was included in the request of updateParagraphStyle
in the batchUpdate method. And also, I found this thread at Stackoverflow. By this, I could understand that other users also want the script for modifying the shading color of the paragraph. So I challenged to achieve this.