[Solved] Changing the formatting for part of a cell

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
dwayner79
Posts: 2
Joined: Sat Feb 04, 2012 4:43 am

[Solved] Changing the formatting for part of a cell

Post by dwayner79 »

Cell contains the following text all bolded, but I want to only bold part of the title:
Cell A1 wrote:My Book Title: My cute subtitle
Unfortunately, I cannot find anything that says how to edit only part of a cell's string.

So I figure loop through each cell get the string. Find the position of the ":" but then I cannot find anything.

I tried recording a Macro but the recorder output did not make any sense.

Code: Select all

For iRow = 1 To numOfRows
  	oCell = oSheet.getCellByreference(1,iRow)
  	sText = oCell.getValue()
  	iPos = instr(sText, ":")
  	
  	//WHAT GOES HERE?
  	
  Next

Appreciate any help/
Last edited by dwayner79 on Tue Feb 07, 2012 4:34 am, edited 1 time in total.
Win7, Open Office 3.3 ( & libreOffice 3.4.5)
User avatar
kingfisher
Volunteer
Posts: 2127
Joined: Tue Nov 20, 2007 10:53 am

Re: Changing the formatting for part of a cell

Post by kingfisher »

It would be better to use getString. I am fairly certain that you cannot format part of the text in the way you wish. I did find this thread: Character formatting Macro for spreadsheet but I doubt that it will take you any further.
Apache OpenOffice 4.1.12 on Linux
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Changing the formatting for part of a cell

Post by Charlie Young »

dwayner79 wrote:Cell contains the following text all bolded, but I want to only bold part of the title:
Cell A1 wrote:My Book Title: My cute subtitle
Unfortunately, I cannot find anything that says how to edit only part of a cell's string.

So I figure loop through each cell get the string. Find the position of the ":" but then I cannot find anything.

I tried recording a Macro but the recorder output did not make any sense.

Code: Select all

For iRow = 1 To numOfRows
  	oCell = oSheet.getCellByreference(1,iRow)
  	sText = oCell.getValue()
  	iPos = instr(sText, ":")
  	
  	//WHAT GOES HERE?
  	
  Next

Appreciate any help/
kingfisher is right about your attempt to find the colon in that way, and there is no getCellByreference.

But you're on the right track.

This does the deed to the current selection:

Code: Select all

Sub BoldTitles
	Dim oDoc As Object
	Dim oSelection As Object
	Dim oCell As Object
	Dim oCursor As Object
	Dim oText As Object
	Dim colonLoc As Long
	Dim i As Long
	
	oDoc = ThisComponent
	oSelection = oDoc.CurrentController.Selection
	
	for i = 0 to oSelection.Rows.Count - 1
		oCell = oSelection.getCellByPosition(0,i)
		oText = oCell.getText()
		colonLoc = InStr(oText.String,":")
		if colonLoc > 0 then
			oCursor = oText.createTextCursor
			oCursor.gotoStart(False)
			oCursor.goRight(colonLoc,True)
			oCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
		endif
	next i 
End Sub

Apache OpenOffice 4.1.1
Windows XP
User avatar
kingfisher
Volunteer
Posts: 2127
Joined: Tue Nov 20, 2007 10:53 am

Re: Changing the formatting for part of a cell

Post by kingfisher »

Interesting, thank you, Charlie. I had forgotten all about 'text'.
Apache OpenOffice 4.1.12 on Linux
dwayner79
Posts: 2
Joined: Sat Feb 04, 2012 4:43 am

Re: Changing the formatting for part of a cell

Post by dwayner79 »

You guys are brilliant!

I did change it a bit. Since I am not sure what formatting will be dropped off, I Set the cell to Normal before your code. If there are no Colons, then the whole thing should be bold as it is all the main title. It works as expected.

Thank you for your help!

Code: Select all

sub BoldTitles

   Dim oDoc As Object
   Dim oSelection As Object
   Dim oCell As Object
   Dim oCursor As Object
   Dim oText As Object
   Dim colonLoc As Long
   Dim i As Long
   
   oDoc = ThisComponent
   oSelection = oDoc.CurrentController.Selection
   
   for i = 0 to oSelection.Rows.Count - 1
      oCell = oSelection.getCellByPosition(0,i)
      oCell.CharWeight = com.sun.star.awt.FontWeight.NORMAL
      oText = oCell.getText()
      colonLoc = InStr(oText.String,":")
      if colonLoc > 0 then
         oCursor = oText.createTextCursor
         oCursor.gotoStart(False)
         oCursor.goRight(colonLoc,True)
         oCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
      else
      	 oCell.CharWeight = com.sun.star.awt.FontWeight.BOLD  
      endif
   next i 
End Sub
Win7, Open Office 3.3 ( & libreOffice 3.4.5)
Post Reply