2017-01-18 31 views
1

你好,我尝试检测我的谷歌文档中的特定字符串,并将其设置为“粗体”。 香港专业教育学院尝试这样:如何使用谷歌脚本替换字体重量?

function bold() { 
var body = DocumentApp.getActiveDocument().getBody(); 
var foundElement = body.findText("Câbles"); 

while (foundElement != null) { 
    // Get the text object from the element 
    var foundText = foundElement.getElement().asText(); 

    // Change the weight 
    foundText.setFontWeight("bold"); 

    // Find the next match 
    foundElement = body.findText("Câbles", foundElement); 
} 
} 

脚本返回我一个错误:类型错误:Fonction setFontWeight不是在文本对象中找到。

你能帮我吗?谢谢。

编辑>

香港专业教育学院尝试使用这个,但现在的大胆风格被应用到整个段落不仅文本字符串...

function bold() { 
var body = DocumentApp.getActiveDocument().getBody(); 
var foundElement = body.findText("test"); 

while (foundElement != null) { 
    // Get the text object from the element 
    var foundText = foundElement.getElement().asText(); 

    // Set Bold 
    foundText.setBold(true); 

    // Find the next match 
    foundElement = body.findText("test", foundElement); 
} 
} 

回答

1

它看起来就像是通过支持sheets-不是文档

在文档中,您可以尝试更改字体大小?

// Change the weight 
    foundText.setFontSize(99); 
+0

我用尽。这项工作,并改变字体大小。 –

+0

我也尝试过“foundText.setBold(bold);”它返回我“ReferenceError:”粗体“未定义”:/ –

+0

你会注意到它是文档类的一部分:https://developers.google.com/apps-script/reference/document/document – OblongMedulla

1

好吧,我这样做:

function bold() { 
var body = DocumentApp.getActiveDocument().getBody(); 
var foundElement = body.findText("test"); 

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(); 

    // Set Bold 
    foundText.setBold(start, end, true); 

    // Find the next match 
    foundElement = body.findText("test", foundElement); 
    } 
} 
+0

不错,很好用! – OblongMedulla