[Solved] Hide image with cell

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

[Solved] Hide image with cell

Post by bartjeman »

 Edit: Split from Hiding Jpeg with cell to give new poster with new question (printing) in 3-year-old topic their own. 
I created a macro that serves my needs. But the images are hidden on my screen only, they are visible when printing :(

Code: Select all

Sub HideRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage

oSheets = ThisComponent.getSheets()
oSheet1 = oSheets.getByName("Sheet1")
oRows = oSheet1.getRows()

for i = 5 to 29
   oRow = oRows.getByIndex(i)
   oCell = oSheet1.getCellByPosition(0,i) 'First row has index 0
   If oCell.Value = 0 Then
   
      For k = 0 To oDP.Count - 1
         oShape = oDP(k)
         oAnchor = oShape.Anchor
         AnchorRow = oAnchor.CellAddress.Row
         If AnchorRow = i Then
            oShape.Visible = False
         End If
      Next k

      oRow.isVisible = False

   End If
next i

End Sub

Sub ShowRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage

oSheets = ThisComponent.getSheets()
oSheet1 = oSheets.getByName("Sheet1")
oRows = oSheet1.getRows()

for i = 5 to 29
   oRow = oRows.getByIndex(i)
   oRow.isVisible = True
next i

For k = 0 To oDP.Count - 1
   oShape = oDP(k)
   oShape.Visible = True
Next k

End Sub
Attachments
imagesnothidden.jpg
Last edited by Hagar Delest on Sat Jul 31, 2021 9:59 am, edited 1 time in total.
Reason: tagged solved.
OpenOffice 4.1.7 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hiding Jpeg with cell

Post by Villeroy »

Select one of the pics.
menu:Tools>AddOns>Mri (selection)
Method getByIndex --> 0 (the only selected element)
browse the properties of that element.

Villeroy wrote:A database could do what you want.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Thank you for the reply Villeroy :)
This is the output:

Code: Select all

(Name)                 (Value Type)     (Value)                      (Info.)   (Attr.)    (Handle)  
Count                  long             1                            Pseud     Read_Only       
Delegator              .uno.XInterface                               Pseud     WriteOnly       
ElementType            type             com.sun.star.drawing.XShape  Pseud     Read_Only       
ImplementationId       []byte           -SEQUENCE-                   Pseud     Read_Only       
ImplementationName     string           com.sun.star.dr...           Pseud     Read_Only       
SupportedServiceNames  []string         -Sequence-                   Pseud     Read_Only       
Types                  []type           -Sequence-                   Pseud     Read_Only    
More weirdness
When I re-opened the doc, the hidden images were visible on my screen. Is the .Visible = False attribute reset when the doc is opened?
Attachments
imagesnothidden2.jpg
OpenOffice 4.1.7 on Windows 10
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

The way the images jump up on the page I don't think this is going to work. I did mark the check box Protect Position for all the images.

I did read that LibreOffice will hide the image if it is anchored to a cell in the hidden row. But I'm not sure I want to change all my oo installations to address this one issue
OpenOffice 4.1.7 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hiding Jpeg with cell

Post by Villeroy »

bartjeman wrote:This is the output:
Click "Methods"
Double-click getByIndex() and choose 0
Now you see the methods of the first (and only) selected element, the picture.
Go back to "Properties".
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Thanks Villeroy
This is the output of a selected image. I can see that there is a printable property. I placed it into my macro like oShape.Printable = False, which has fixed the printing issue
There is still the issue of when I show all the rows and images, the images do not remember which cell they were anchored to, so they appear in the wrong place. I suppose I could create an array of points and use that to locate the images, but now my little macro is getting quite complex ;-)

Code: Select all

(Name)                 (Value Type)                (Value)                      (Info.)   (Attr.)    (Handle)  
Anchor                 .uno.XInterface             -INTERFACE-                                         0  
Bitmap                 .awt.XBitmap                -INTERFACE-                            Read_Only  3920  
BoundRect              .awt.Rectangle              -STRUCT-                               Read_Only  3929  
Count                  long                        3                            Pseud     Read_Only       
Description            string                      ""                                                3982  
ElementType            type                        com.sun.star.drawing.XShape  Pseud     Read_Only       
Events                 .container.XNameReplace     -INTERFACE-                  Pseud     Read_Only       
FrameRect              .awt.Rectangle              -STRUCT-                                          3925  
GluePoints             .container.XIndexContainer  -INTERFACE-                  Pseud     Read_Only       
HoriOrientPosition     long                        28                                                  0  
ImageMap               .container.XIndexContainer  -INTERFACE-                                         0  
ImplementationId       []byte                      -SEQUENCE-                   Pseud     Read_Only       
ImplementationName     string                      com.sun.star.co...           Pseud     Read_Only       
IsFontwork             boolean                     False                                  Read_Only  3928  
LayerID                short                       0                                                 1129  
LayerName              string                      vorne                                             1130  
LinkDisplayBitmap      .awt.XBitmap                -INTERFACE-                            Read_Only  3931  
LinkDisplayName        string                      ""                                     Read_Only  3930  
MetaFile               []byte                      -SEQUENCE-                             Read_Only  3927  
MoveProtect            boolean                     False                                             1126  
Name                   string                      ""                                                1131  
Parent                 .uno.XInterface             -INTERFACE-                  Pseud                     
Position               .awt.Point                  -STRUCT-                     Pseud                     
Printable              boolean                     True                                              1128  
PropertiesToDefault    []string                                                 Pseud     WriteOnly       
PropertySetInfo        .beans.XPropertySetInfo     -INTERFACE-                  Pseud     Read_Only       
PropertyToDefault      string                                                   Ignored                   
RotateAngle            long                        0                                                 1142  
ShapeType              string                      com.sun.star.dr...           Pseud     Read_Only       
ShearAngle             long                        0                                                 1143  
Size                   .awt.Size                   -STRUCT-                     Pseud                     
SizeProtect            boolean                     True                                              1127  
SupportedServiceNames  []string                    -Sequence-                   Pseud     Read_Only       
Title                  string                      ""                                                3981  
Transformation         .drawing.HomogenMatrix3     -STRUCT-                                          3946  
Types                  []type                      -Sequence-                   Pseud     Read_Only       
UINamePlural           string                      group objects                          Read_Only  3976  
UINameSingular         string                      group object                           Read_Only  3975  
VertOrientPosition     long                        674                                                 0  
Visible                boolean                     True                                              1161  
ZOrder                 long                        0                                                 3939  
OpenOffice 4.1.7 on Windows 10
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Rather than use .awt.point, would it be simpler to use the Anchor property? Can it be used to anchor the image to a particular cell? If so, would you be able to provide an example of how the Anchor property is used (.uno.XInterface) is used?
OpenOffice 4.1.7 on Windows 10
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Hiding Jpeg with cell

Post by John_Ha »

Be creative.

1. Use linked images
2. When you want to hide an image, delete the image in the folder containing the linked images, or replace it with a 1 x 1 pixel image.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hiding Jpeg with cell

Post by Villeroy »

The image has property "Printable" and its current value is True. I would suspect that it will be invisible on the print-out when you set that property to False. You can check this directly in MRI. Choose Mode>Set in the MRI menu, double-click the "Printable" line and set the value to False or 0. Then have another look at the print preview.

And yes, with a database all that trouble would fall off.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Wow this MRI thing is amazing.
Villeroy, Printable = False is working in my script. To place the image back where it belongs what do you think of using something like oShape.Anchor.AbsoluteName ?
John_Ha, I see your logic but I think programmatically it is not any simpler, and I will still have the issue of the image loosing it's anchor position
OpenOffice 4.1.7 on Windows 10
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Ok I have this code. Not sure if it will work but shows promise :)
I can record the anchor position of the images into an array in Sub HideRows, but when I call the Sub ShowRows the array info is not there. What is the method to save the array so it can be used by another Sub? Thanks!

Code: Select all

Sub HideRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage

Dim AnchorAddress(30) as String

oSheets = ThisComponent.getSheets()
oSheet1 = oSheets.getByName("Sheet1")
oRows = oSheet1.getRows()

'record the anchor position of all images
      For k = 0 To oDP.Count - 1
         oShape = oDP(k)
         AnchorAddress(k) = oShape.Anchor.AbsoluteName()
          msgbox AnchorAddress(k)
       Next k

'check rows for zero quantity
for i = 5 to 29
   oRow = oRows.getByIndex(i)
   oCell = oSheet1.getCellByPosition(0,i) 'First row has index 0
   If oCell.Value = 0 Then
   
      'loop thru all the images to find the one in this row
      For k = 0 To oDP.Count - 1
         oShape = oDP(k)
         oAnchor = oShape.Anchor
         AnchorRow = oAnchor.CellAddress.Row
         If AnchorRow = i Then
            oShape.Visible = False
            oShape.Printable = False
         End If
      Next k
   
     ' oRow.isVisible = False

   End If
next i

End Sub

Sub ShowRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage

oSheets = ThisComponent.getSheets()
oSheet1 = oSheets.getByName("Sheet1")
oRows = oSheet1.getRows()

for i = 5 to 29
   oRow = oRows.getByIndex(i)
   oRow.isVisible = True
next i

For k = 0 To oDP.Count - 1
   oShape = oDP(k)
   oShape.Visible = True
   oShape.Printable = True
   'oShape.Anchor.AbsoluteName = AnchorAddress(k)  --> ERROR ON THIS LINE
Next k

End Sub
OpenOffice 4.1.7 on Windows 10
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hiding Jpeg with cell

Post by bartjeman »

Answering my own question...
I am writing the anchor position of each image to a cell in the sheet
When I try to assign the anchor address to the image with

Code: Select all

oShape.Anchor.AbsoluteName = AddressString
I get a read only error
Looks like setting the anchor position is a no-go
OpenOffice 4.1.7 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hide image with cell

Post by Villeroy »

The "Anchor" of your shape is a sheet object. Its property set includes the same properties as a huge cell range plus some sheet specific properties (e.g. its "Name"). The "AbsoluteName" of a cell range is the string representation of its absolute address. This string depends on the size and position of the cell range but not vice versa. You can not replace the object by giving it another address. You have to exchange the actual object, something like oShape.Anchor = objCell but there you have some other problem: Can you tell which is the cell below a shape?

How to anchor many shapes to their top-left cells:
Get the drawing toolbar or the form controls toolbar and click the icon that looks like a standard mouse pointer arrow.
With a decent mouse (no touchpad) you can draw a rectangle around all your shapes (or as many as possible) in order to select all shapes completely included by that rectangle. Then you can anchor them to their top-left cells in one go.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hide image with cell

Post by bartjeman »

Hi Villeroy
If should try to "exchange the actual object" then my old code to write the anchor address to a cell should instead be write the object data for .Anchor

But where can I store that for later retrieval, I'm pretty sure I can't save object data to sheet cells ;)
OpenOffice 4.1.7 on Windows 10
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Hide image with cell

Post by bartjeman »

I'm going to mark this solved, even though I did not start it
I used the shape Position property

Code: Select all

REM  *****  BASIC  *****

Sub HideRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage

oRows = oSheet.getRows()

'write the position of all images to the sheet
For k = 0 To oDP.Count - 1
   oShape = oDP(k)
   oCell = oSheet.getCellByPosition(10, k)
   oCell.Value =  oShape.getPosition.x
   oCell = oSheet.getCellByPosition(11, k)
   oCell.Value =  oShape.getPosition.y
Next k

'check rows for zero quantuty
For i = 5 to 29
   oRow = oRows.getByIndex(i)
   oCell = oSheet.getCellByPosition(0,i) 'First row has index 0
   If oCell.Value = 0 Then
   
      'loop thru all the images to find the one in this row, then hide it
      For k = 0 To oDP.Count - 1
         oShape = oDP(k)
         oAnchor = oShape.Anchor
         AnchorRow = oAnchor.CellAddress.Row
         If AnchorRow = i Then
            oShape.Visible = False
            oShape.Printable = False
         End If
         
      Next k
   
      oRow.isVisible = False

   End If
Next i

End Sub

Sub ShowRows

oSheet = ThisComponent.CurrentController.ActiveSheet
oDP  = oSheet.DrawPage
Dim Position as New com.sun.star.awt.Point

oRows = oSheet.getRows()

' show hidden rows
For i = 5 to 29
   oRow = oRows.getByIndex(i)
   oRow.isVisible = True
Next i

'get the stored image positions and place the images where they belong
For k = 0 To oDP.Count - 1
   oShape = oDP(k)
   oShape.Visible = True
   oShape.Printable = True
   oCell = oSheet.getCellByPosition(10, k)
   Position.x = oCell.getValue
   oCell = oSheet.getCellByPosition(11, k)
   Position.y = oCell.getValue
   oShape.Position  = Position
Next k

End Sub
OpenOffice 4.1.7 on Windows 10
Post Reply