Get width and height of Slide

836 views
Skip to first unread message

Certify'em Support

unread,
Jul 16, 2020, 10:33:03 AM7/16/20
to google-apps-sc...@googlegroups.com
Hey All -

First post!

I am developing a script that inserts a textbox onto the first slide of a user's existing Google Presentation. I know how to get the slide, and how to insert a text box at a specific location using scaleX and scaleY in a relative transform request, thanks to sample code I've found from Google. It seems if I set scaleX and scaleY to 0, then the text box gets placed in the upper-left corner.

But what I cannot seem to find documented anywhere is how to obtain the dimensions (width and length in px) of the slide itself such that I can specify exactly where to place my textbox. I want it centered on the slide, and 20px from the bottom, ergo I need to make ScaleX = (width/2) and ScaleY = (height - 20). I hope that makes sense.

Any tips or pointers would be helpful. I can also do this directly via the Slides API if needed, via Advanced Services.

Thanks,
Dave

AliceKeeler Keeler

unread,
Jul 16, 2020, 11:22:23 AM7/16/20
to Google Apps Script Community
Morning Dave
Take a look at my code in this script

This rolls dice in a Google Slides and puts a rectangle on the slide to represent the die. 

Please do not share this with those outside the group, I freely give http://alicekeeler.com/diceslides but this one that puts the die on the slide is part of my premium membership benefits. 

Dave A

unread,
Jul 16, 2020, 12:05:40 PM7/16/20
to Google Apps Script Community
Thanks, Alice. It looks like your code inserts the die at an absolute position:

    var shape = slide.insertShape(SlidesApp.ShapeType.CUBE, 100, 70, 250, 250);

Whereas I want to insert mine at a relative position (i.e. centered on the slide, where the width of the slide can be different depending on how the user has created it). So I first need to know the slide's width so I can place my text box at x value width/2.

Michael O'Shaughnessy

unread,
Jul 16, 2020, 12:13:46 PM7/16/20
to Google Apps Script Community
Great work Alice!  Keep it up!

I created a SlidesApp solution that generates reports for student's NCE scores.  A place a diamond shape on a graph on the slide that matches their score.  Before I started this project I created 2 little scripts to help me figure out where things are placed.  I would change the page setup, the type of shape as well as the width and height of the shape.  I suggest you just create a blank presentation and add these 2 scripts and start "playing".  For what it is worth... slides uses "points" measurement when it comes to dimensions... So a page set up of 8.5 inches x 11 inches will give a page width of 612 points and a height of 792 points.

function getWidthAndHeight() {
  let pres = SlidesApp.getActivePresentation();
  let pw = pres.getPageWidth();
  let ph = pres.getPageHeight();
  
  let ui = SlidesApp.getUi();
  ui.alert(`The width is ${pw} and the height is ${ph}.`);
}

function insertAshape(){
  let pres = SlidesApp.getActivePresentation();
  let pw = pres.getPageWidth();
  let ph = pres.getPageHeight(); 
  let newSlide = pres.insertSlide(1);
  //inserts a cloud shape in the middle of the slide... no matter what the page setup is...
  newSlide.insertShape(SlidesApp.ShapeType.CLOUD, pw/2-10, ph/2-10, 20, 20);
}

Certify'em Support

unread,
Jul 16, 2020, 12:48:50 PM7/16/20
to google-apps-sc...@googlegroups.com
Thanks, Michael! getPageWidth() and getPageHeight() were exactly what I was looking for! Much appreciated!!

Best,
Dave

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/KcOU2FV5Dng/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3e5a22a2-3909-4921-b684-416c400d2a49n%40googlegroups.com.


--
Like Certify'em? Please consider rating and reviewing it.

Yasir Karam

unread,
Jul 16, 2020, 2:15:19 PM7/16/20
to google-apps-sc...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CA%2BGwAgHN1jFV7hOq7OOGPhC-bdhbKmd64LbRxjdEMnYKSpKOOA%40mail.gmail.com.

Certify'em Support

unread,
Jul 16, 2020, 2:39:20 PM7/16/20
to google-apps-sc...@googlegroups.com
Awesome, thanks Yasir!!

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/KcOU2FV5Dng/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CALiqV1X2GjFBbErj2%3Dt5PyxW6mWS7FK8hpMdmsdzL6i%2BkuiK%2Bg%40mail.gmail.com.

Michael O'Shaughnessy

unread,
Jul 16, 2020, 8:27:28 PM7/16/20
to google-apps-sc...@googlegroups.com
Yasir!!  Awesome page!!  I wish I would have found that a long time ago!!!

Reply all
Reply to author
Forward
0 new messages