Add Text as Italics to Google Doc

时间:2017-06-12 16:42:47

标签: google-apps-script google-docs

I have an addon that I am working on for Google Docs, and I am trying to insert text as italics. I am trying to insert a Works Cited (MLA Formatting, as seen here1), and a title needs to be in italics, and the rest of the line needs to be in regular formatting. The methods I've tried either puts the whole document or the entire line in italics, which obviously doesn't work. Anyone have a solution?

1 个答案:

答案 0 :(得分:0)

Text类有一个setItalics()方法,可用于使文本斜体化。您需要将较大的文本分成较小的部分(标题之前,标题之后,标题之后),以确保只有标题以斜体显示。 Text Class setItalic

function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.editAsText();
var textBeforeItalic = 'I wrote a book called ';
var title = 'Great Book';
var textAfterItalic = ', it is really good.';
text.insertText(0, textBeforeItalic+title+textAfterItalic);
text.setItalic(textBeforeItalic.length,textBeforeItalic.length+title.length,true);
}