Add Text as Italics to Google Doc - google-apps-script

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?

The Text class has a setItalics() method that can be used to make text italicized. You will need to break up your larger body of text into smaller parts (one before the title, the title, and one after) to ensure that only the title is in italics.
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);
}

Related

setHeadingAttributes to redefine a heading style in Documents w/ GAS

I am hoping to re-define the heading styles for a document. My understanding is that getHeadingAttributes and setHeadingAttributes would let me do this
https://developers.google.com/apps-script/reference/document/body#setheadingattributesparagraphheading-attributes
This is how I'm going about testing this --
function main() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1);
Logger.log("Before: \n" + JSON.stringify(styledef) );
styledef[DocumentApp.Attribute.UNDERLINE] = true;
styledef[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffff00';
body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, styledef);
styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1);
Logger.log("After: \n" + JSON.stringify(styledef));
}
This Body has its headings attributes modified, as revealed by the Logger output:
[17-10-04 09:20:53:379 MDT] Before:
{"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":false,"BACKGROUND_COLOR":null,"INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}}
[17-10-04 09:20:53:382 MDT] After:
{"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":true,"BACKGROUND_COLOR":"#ffff00","INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}}
UNDERLINE changed from false to true
BACKGROUND_COLOR changed from null to #ffff00
But in my document to which this script is bound, the styling of Heading 1 has not changed for existing paragraphs with that Heading. Nor are new paragraphs marked Heading 1 styled with the changes I've tried to make.
Incidentally, when I run that same function on NORMAL instead of HEADING1, the changes are instantly visible: Every Heading (Normal, Title, Subtitle, Heading 1, etc...) has the underline and background color applied.
I feel like I've fundamentally misunderstood the purpose of these methods (getHeadingAttributes, setHeadingAttributes). If not these methods, I'd like to find the correct way to re-define Heading stylings for a document. Can you point me in the right direction?
Many Thanks,
Haven't played around much of this yet, but check the Enum ParagraphHeading as it seems close to what you're getting at.
Use the ParagraphHeading enumeration to configure the heading style for ParagraphElement.
var body = DocumentApp.getActiveDocument().getBody();
// Append a paragraph, with heading 1.
var par1 = body.appendParagraph("Title");
par1.setHeading(DocumentApp.ParagraphHeading.HEADING1);
// Append a paragraph, with heading 2.
var par2 = body.appendParagraph("SubTitle");
par2.setHeading(DocumentApp.ParagraphHeading.HEADING2);
// Append a paragraph, with normal heading.
var par3 = body.appendParagraph("Text");
par3.setHeading(DocumentApp.ParagraphHeading.NORMAL);

How to show html style text in excel cell with SpreadsheetGear

I get a string that contains html content, like this:
"i am headhello"
now, i want to write this string into excel cell, and let these html tags render the bold style.
How can i do this with spreadsheetgear?
SpreadsheetGear does not support parsing and rendering HTML. If you put this type of content in a cell, the raw markup will be displayed instead.
SpreadsheetGear does support adding rich-text (RTF) to a cell, but you would need to do this with SpreadsheetGear API using:
IRange.GetCharacters(...)
ICharacters interface
The following example code would render something similar to this:
// Create new workbook.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
// Add text to A1 which we'll format below...
cells["A1"].Value = "This Is My Header\n\nHello World!";
// Format "header" as bold and with a larger font size.
ICharacters charsHeader = cells["A1"].GetCharacters(0, 17);
charsHeader.Font.Bold = true;
charsHeader.Font.Size = 18;
// Format "Hello" text.
ICharacters charsHello = cells["A1"].GetCharacters(19, 5);
charsHello.Font.Italic = true;
charsHello.Font.Color = SpreadsheetGear.Colors.DarkRed;
// Format "World" text.
ICharacters charsWorld = cells["A1"].GetCharacters(25, 5);
charsWorld.Font.Underline = UnderlineStyle.Single;
charsWorld.Font.Color = SpreadsheetGear.Colors.DarkBlue;
// Expand column width to accommodate header text
cells["A:A"].ColumnWidth = 30;
// Save and view in Excel...
workbook.SaveAs(#"c:\temp\rtf.xlsx", FileFormat.OpenXMLWorkbook);
// ...or attach to SpreadsheetGear's WPF WorkbookView to
// confirm RTF is displaying as expected (NOTE: the WinForms
// WorkbookView does not support rendering RTF).
workbookView.ActiveWorkbook = workbook;

Changing the color of particular words in Textareafield in Sencha Touch

I want to change the font color of the misspelled words in a Textareafield in Sencha Touch2. I have the array of misspelled words.
This is the textareafield.
{
xtype:'textareafield',
label:'Note',
id:'txt',
scroll:'vertical',
},
MisspeltArr will have the wrongly spelt words.
var misspeltArr =[];
for(i = 0;i<txtArr.length;i++){
var spellCheck = dictionary.check(txtArr[i]);
console.log("spellCheck : "+spellCheck);
if(spellCheck == false){
misspeltArr.push(txtArr[i]);
}
}
Say i have typed the below text in the textareafield,
Thisss is a mobile application screennn
Now in txt I have the text entered
var txt = Ext.getCmp('txt').getValue();
I want to change the color or underline the wrong words - Thisss and screennn (words in the array) in the textareafield. I have achieved spell check and all I want to do is Highlighting the wrong words. Any help is appreciated.
it seems you have to use something more advanced than textarea. Something such as iframe may do the job

How to split line of text (first half bold, second half not bold) when creating document using Google Script

Instead of:
doc.appendparagraph();
how do you add a line of text to your document without it returning to the next line?
I'm trying to break up a line of text and bold the first half the sentence with .setAttributes(bold);
bold which I've defined using....
var boldpl = {};
boldpl[DocumentApp.Attribute.BOLD] = true;.
Thank you.
I've figured out how to do this in a roundabout way using a merge feature, but how do you go about adding a new line instead of a new paragraph? There is no doc.appendText(); feature is there?
Also is there an easy way to make my document single spaced?
You've asked a lot of different questions there!
Given an existing paragraph, how can some portion of it be rendered in
bold while the rest remains normal?
A Document contains a Body, which can contain Paragraphs, each of which can contain other elements, including Text. You can get that Text either as a string - which you can't do much with - or as a Text Object. Here's an example of using the Text.setBold() method to change just some text in a paragraph to bold:
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var firstParagraph = body.getParagraphs()[0];
textElement = firstParagraph.editAsText();
textElement.setBold(0,6,true); // Set the 0th to 6th characters bold
Alternatively, you could use the Text.setAttributes() method with your custom attribute boldpl:
textElement.setAttributes(0,6, boldpl)
Starting with that building block, you could do things like:
Apply bold from the start of a paragraph until the first occurrence of a colon (:).
Apply bold to the first few words.
...or even apply bold to the first half!
textElement.setBold(0,
Math.floor((textElement.getText().length)/2),
true);
There is no doc.appendText(); feature is there?
No... but there is a Paragraph.appendText() method.
Also is there an easy way to make my document single spaced?
You can control the line spacing of text within paragraphs, by setting attributes on individual paragraphs. See Paragraph.setLineSpacing(). Here's a function that sets every paragraph in your document to single-spacing:
function singleSpace() {
var doc = DocumentApp.getActiveDocument();
var bodyElement = DocumentApp.getActiveDocument().getBody();
var paragraphs = bodyElement.getParagraphs();
// Set each paragraph to single-spaced
paragraphs.forEach( function( paragraph ) {
paragraph.setLineSpacing( 1.0 );
});
}

JSFL: convert text from a textfield to a HTML-format string

I've got a deceptively simple question: how can I get the text from a text field AND include the formatting? Going through the usual docs I found out it is possible to get the text only. It is also possible to get the text formatting, but this only works if the entire text field uses only one kind of formatting. I need the precise formatting so that I convert it to a string with html-tags.
Personally I need this so I can pass it to a custom-made text field component that uses HTML for formatting. But it could also be used to simply export the contents of any text field to any other format. This could be of interest to others out there, too.
Looking for a solution elsewhere I found this:
http://labs.thesedays.com/blog/2010/03/18/jsfl-rich-text/
Which seems to do the reverse of what I need, convert HTML to Flash Text. My own attempts to reverse this have not been successful thus far. Maybe someone else sees an easy way to reverse this that I’m missing? There might also be other solutions. One might be to get the EXACT data of the text field, which should include formatting tags of some kind(XML, when looking into the contents of the stored FLA file). Then remove/convert those tags. But I have no idea how to do this, if at all possible. Another option is to cycle through every character using start- and endIndex, and storing each formatting kind in an array. Then I could apply the formatting to each character. But this will result in excess tags. Especially for hyperlinks! So can anybody help me with this?
A bit late to the party but the following function takes a JSFL static text element as input and returns a HTML string (using the Flash-friendly <font> tag) based on the styles found it its TextRuns array. It's doing a bit of basic regex to clear up some tags and double spaces etc. and convert /r and /n to <br/> tags. It's probably not perfect but hopefully you can see what's going on easy enough to change or fix it.
function tfToHTML(p_tf)
{
var textRuns = p_tf.textRuns;
var html = "";
for ( var i=0; i<textRuns.length; i++ )
{
var textRun = textRuns[i];
var chars = textRun.characters;
chars = chars.replace(/\n/g,"<br/>");
chars = chars.replace(/\r/g,"<br/>");
chars = chars.replace(/ /g," ");
chars = chars.replace(/. <br\/>/g,".<br/>");
var attrs = textRun.textAttrs;
var font = attrs.face;
var size = attrs.size;
var bold = attrs.bold;
var italic = attrs.italic;
var colour = attrs.fillColor;
if ( bold )
{
chars = "<b>"+chars+"</b>";
}
if ( italic )
{
chars = "<i>"+chars+"</i>";
}
chars = "<font size=\""+size+"\" face=\""+font+"\" color=\""+colour+"\">"+chars+"</font>";
html += chars;
}
return html;
}