0

In my never-ending quest to make my life easier, I've encountered something to make it more difficult.

Has anyone had any experience with why creating or editing objects in a Google Slide with Google Apps Script results in an inaccurate floating point version that's kinda close?

Example, creating an object:

SlidesApp
.getActivePresentation()
.getSlides()[0]
.insertShape(SlidesApp.ShapeType.RECTANGLE, 20, 20, 200, 200);

But the resulting rectangle has these properties:

Top: 20
Left: 20
Width: 200.00787401574803
Height: 200.00787401574803

And what's just as bad is the top and left 1px borders aren't pixel perfect as you'd expect given the correct top and left whole number value.

I discovered this because my first attempt at all this was to round the x/y,w/h to get shapes to at least be clean on the page, and that resulted in the same issues.

Arlo
  • 51
  • 7

1 Answers1

1

Some more digging shows 2 things.

1) These crazy floats (rounding errors) aren't the cause of the blurring of my objects on the slide. They can safely be ignored.

2) It seems the pixel values a slide has are a multiple of .375. So to move an object 1px to the right, you actually have to move it .375 to the right. If you want to round the values of a slide's objects, you need to round the result of the value * 2.66666, then multiply the result by .375.

Example:

pageElements[0].setTop(Math.round((pageElements[0].getTop() * 2.6666666666666))*.375);

So now the original goal of pixel-perfect editing of objects in a sidebar tool I create is now possible. Whew!

Arlo
  • 51
  • 7
  • 1
    Note that `getTop` and related methods all measure distance in points (1/72 of an inch), not pixels. This is intentional, since the mapping of points to pixels depends on the display device and zoom level, so keep in mind there may be some variation depending on where you're looking at the final preso. – Maurice Codik Dec 18 '17 at 16:31
  • @MauriceCodik - Do you know if there's a way to see what the current point to pixel ratio is, or what the inch-based dimension for the document is? I'm guessing my 2.66... and .375 values are derivable with some other piece data I can fetch, but I'm not seeing anything obvious pop in when looking at the Google Apps Script documentation. – Arlo Dec 18 '17 at 22:32
  • @Arlo-- I don't think so, sorry. – Maurice Codik Dec 20 '17 at 06:02