我正在尝试编写一个脚本,以便用他们的小型大写版本替换特定的单词。我在我的文档中创建了一个小型大写样式Heading6,但它只能作为一个整体应用于文本范围。可以部分应用这种风格吗?
这是我的尝试:
function applyHeading(textToFind){
var style = {};
style[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING6;
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText(textToFind);
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
var newText = foundText.getText().substring(0, start) + textToFind.toLowerCase() + foundText.getText().substring(end + 1);
foundText.setText(newText);
// Set style -- does not work
foundText.setAttributes(start, end, style);
// Find the next match
foundElement = body.findText(textToFind, foundElement);
}
}
我可以像上面那样理解。如果我的理解是正确的,那么这个示例脚本怎么样?
不幸的是,在目前阶段,还没有方法将文本部分修改为文件服务中的小写大写字母。我认为这可以通过未来更新的文档服务来实现。但作为当前的解决方法,我认为最近发布的Google Docs API可能可用于实现此目的。所以我挑战了这个。
要在运行脚本之前使用以下示例脚本,请在API控制台中启用Google Docs API,如下所示。
示例脚本的流程如下。
示例脚本用于包含Google Document的容器绑定脚本。请将以下脚本复制并粘贴到脚本编辑器,然后运行sample()
函数。
function applySmallCapital(textToFind) {
var doc = DocumentApp.getActiveDocument();
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());
// In order to use at Google Docs API, create ranges of found texts.
var foundElement = body.findText(textToFind);
var ranges = [];
while (foundElement != null) {
var p = foundElement.getElement();
if (p.getType() == DocumentApp.ElementType.TEXT) p = p.getParent();
if (p.getType() == DocumentApp.ElementType.PARAGRAPH || p.getType() == DocumentApp.ElementType.LIST_ITEM) {
var content = obj.body.content[body.getChildIndex(p) + 1];
ranges.push({
startIndex: content.startIndex + foundElement.getStartOffset(),
endIndex: content.startIndex + foundElement.getEndOffsetInclusive(),
});
}
foundElement = body.findText(textToFind, foundElement);
}
// Modify textStyle of found texts.
if (ranges.length > 0) {
var resource = {requests: ranges.map(function(e) {return {
updateTextStyle: {
textStyle: {smallCaps: true},
range: e,
fields: "smallCaps"}
}
})};
params.method = "post";
params.contentType = "application/json";
params.payload = JSON.stringify(resource);
UrlFetchApp.fetch(baseUrl + id + ":batchUpdate", params);
}
}
function sample() {
applySmallCapital("this should be small caps");
}
使用高级Google服务的Google Docs API时,上述脚本如下所示。使用此功能时,请在高级Google服务中启用Google文档API。在这个脚本中,通过减少一个循环,成本略低于上面的成本。
function applySmallCapital(textToFind) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var id = doc.getId();
// Retrieve all contents from Google Document.
var obj = Docs.Documents.get(id, {fields: "*"})
// In order to use at Google Docs API, create ranges of found texts.
var foundElement = body.findText(textToFind);
var requests = [];
while (foundElement != null) {
var p = foundElement.getElement();
if (p.getType() == DocumentApp.ElementType.TEXT) p = p.getParent();
if (p.getType() == DocumentApp.ElementType.PARAGRAPH || p.getType() == DocumentApp.ElementType.LIST_ITEM) {
var content = obj.body.content[body.getChildIndex(p) + 1];
requests.push({
updateTextStyle: {
textStyle: {smallCaps: true},
range: {
startIndex: content.startIndex + foundElement.getStartOffset(),
endIndex: content.startIndex + foundElement.getEndOffsetInclusive(),
},
fields: "smallCaps"}
});
}
foundElement = body.findText(textToFind, foundElement);
}
// Modify textStyle of found texts.
if (requests.length > 0) {
var resource = {requests: requests};
Docs.Documents.batchUpdate(resource, id);
}
}
function sample() {
applySmallCapital("this should be small caps");
}