Bring the pageElements to front or back using apps

2019-08-23 05:21发布

问题:

The stacking order is determined by the order by which they are inserted into the slide. However, some of the page elements in slides remain hidden.

Is there a way to change the Order of objects in Google slides using apps script?

回答1:

How about this workaround? I have experienced the same situation with you. At that time, I have moved the element using this workaround. I think that there are several workarounds for this situation. So please think of this as one of them. The flow of this workaround is as follows.

Principle:

In Google Slides, for example, when new shape is created to a slide, it is added to the last element to pageElements. This means that the last element to pageElements is the most front. This is used for the workaround.

Flow:

  1. Select shapes.
    • In this case, as a sample, the shapes which are brought to the front are selected by click.
  2. Add the selected shapes to the page using insertPageElement(). These are added to the page as new elements.
  3. Remove the selected shapes from pageElements.

By this, the selected shapes can be brought to the most front and back. When this flow is reflected to the script, it becomes as follows.

Sample script:

function move(page, spe) {
  var pe = page.getPageElements();
  spe.forEach(function(e) {
    page.insertPageElement(e);
  });
  pe.forEach(function(f, i) {
    if (spe.some(function(g) {return f.getObjectId() == g.getObjectId()})) {
      pe[i].remove();
    }
  });
}

// Bring to back
function back() {
  var slide = SlidesApp.getActivePresentation();
  var selected = slide.getSelection();
  var page = selected.getCurrentPage();
  var pageElements = page.getPageElements();
  var selectedPageElements = selected.getPageElementRange().getPageElements();
  var noSelectedPageElements = pageElements.filter(function(e) {return !selectedPageElements.some(function(f) {return e.getObjectId() == f.getObjectId()})});
  move(page, noSelectedPageElements);
}

// Bring to front
function front() {
  var slide = SlidesApp.getActivePresentation();
  var selected = slide.getSelection();
  var page = selected.getCurrentPage();
  var pageElements = page.getPageElements();
  var selectedPageElements = selected.getPageElementRange().getPageElements();
  move(page, selectedPageElements);
}

Result:

Note:

  • When you use this script, at first, please select the shapes on a slide. Then run front() or back(). When front() is run, the selected shape is moved to the most front. When back() is run, it is moved to the most back.
  • In this sample script, the selected shapes are brought to the most front and back. This is a simple sample script.
    • If you want to move the shapes every element, please modify the script.
  • In this case, the moved elements are added as new elements. So the moved object IDs are updated.

References:

  • insertPageElement(pageElement)
  • remove()

If this answer was not what you want, I'm sorry.

Updated at November 20, 2018:

The Slides service was updated at Google's update at November 14, 2018, and several methods were added for achieving this issue.

The Slides service has been extended with the following new methods that let you control the Z-positioning of page elements in Slides.

  • bringForward()
  • bringToFront()
  • sendBackward()
  • sendToBack()