3

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 2

3

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});
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
@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?
-2

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

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.
@br araujo, i knew that, but its difficult to parse string, where to italicize & where to bold etc!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.