How can I add/insert html text (contains italic/bold tag & some other tags for text formatting) in Google docs using Google Apps Script?
2 Answers
You can use the in-built Utilities method to get the formatted text
function makeNewDoc()
{
var html = '<html><body><h1><i><b>HEADING</b></i></h1></body></html>';
var blob1 = Utilities.newBlob("").setDataFromString(html, "UTF-8").setContentType("text/html");
var newFileId = Drive.Files.insert({title: "TEST"}, blob1, {convert: true});
}
2 Comments
Serge insas
note that this code requires to enable the Drive API (advanced Google Service in the "ressource" tab) , see doc here : developers.google.com/apps-script/advanced/drive
Sagar Thoriya
@Sergeinsas This seems to be working but what if we want to replace the existing text with rich text content (HTML String)? Is there any other way to do this?
You can use setFontStyle method. Check the example bellow.
function myFunction() {
var sheet = SpreadsheetApp.openById("0AkkQD4hSd4KAdDk0aUM5VUVDQnJXdE5odzcxalNXdGc").getSheets()[0];
sheet.getRange(1, 1).setValue("Hello World").setFontStyle("italic");
}
Live example here.
2 Comments
Serge insas
No offence but this is not answering the question. Question is about docs and multiple HTML tags included in the text to add. Please review.
exe
@br araujo, i knew that, but its difficult to parse string, where to italicize & where to bold etc!