我正在尝试编写脚本,以用小写字母版本替换特定单词。我在文档中创建了一个小写的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);
}
}
答案 0 :(得分:0)
我可以像上面那样理解。如果我的理解是正确的,那么该示例脚本如何?
不幸的是,在当前阶段,文档服务中尚无方法将文本部分修改为小写大写字母。我认为将来的更新可能会通过文档服务来实现。但是,作为当前的解决方法,我认为最近发布的Google Docs API可能可以用来实现这一目标。所以我向这个挑战。
为了使用以下示例脚本,在运行脚本之前,请按照以下说明在API控制台上启用Google Docs API。
示例脚本的流程如下。
该示例脚本适用于带有Google文档的容器绑定脚本。请复制以下脚本并将其粘贴到脚本编辑器,然后运行功能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 Docs 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");
}