Allow offset for OneCellAnchor

Issue #665 resolved
Claude Paroz created an issue

I'd like to link a drawing to a cell with the OneCellAnchor anchor, but with a top/left offset.
I've seen that the OneCellAnchor has a _from attribute with colOff/rowOff numbers to set any offset (http://officeopenxml.com/drwPicInSpread-oneCell.php).

Would it be a good idea to reuse the drawing.left/top attributes to set those offset numbers in the case of a OneCellAnchor? Here's what the code could do:

--- drawing/drawing_old.py  2016-08-15 18:21:17.162343165 +0200
+++ drawing/drawing.py  2016-08-15 18:13:13.958324478 +0200
@@ -95,7 +95,11 @@
         elif self.anchortype == "oneCell":
             anchor = OneCellAnchor()
             anchor._from.col = self.anchorcol
+            if self.left != 0:
+                anchor._from.colOff = pixels_to_EMU(self.left)
             anchor._from.row = self.anchorrow
+            if self.top != 0:
+                anchor._from.rowOff = pixels_to_EMU(self.top)

         anchor.ext.width = pixels_to_EMU(self._width)
         anchor.ext.height = pixels_to_EMU(self._height)

Comments (10)

  1. CharlieC

    That kind of overloading is not a good idea. Much better to pass in an Anchor with the appropriate values. NB positioning in Excel is a nightmare because it depends on the client's OS and screen. This is why I went with a single cell anchor without any kind of offset as the default.

  2. Claude Paroz reporter

    OK, sorry. But I'd like to get some sort of conceptual approval before diving in tests.

  3. CharlieC

    PR's are fine for discussion – they won't be merged without tests. Otherwise the mailing list is great for discussion and you can use it to describe the use case.

  4. CharlieC
    • changed status to resolved
    • edited description

    I think this is now resolved because client code can now create and manage anchors for images.

  5. Claude Paroz reporter

    Hey Charlie,

    Visting this issue again, I cannot see how we can manage anchors for images in latest code.
    I saw you deprecated img.anchor in a788c181a3d9bc917cff93d4e7e884cb1377f4c4, but you are still using that method in Worsheet.add_image. It seems that Drawing anchors are still dynamically created by the Drawing.anchor property.

  6. CharlieC

    The method now has the same semantics as when working with charts. The anchor is attached to the image when you attach it to a worksheet. You should have full control over the anchor, eg. you can pass either a simple coordinate or a fully-fledged anchor of your choice.

  7. Log in to comment