How to convert elements to the same size as the first selected element?

北战南征 提交于 2020-08-08 05:16:07

问题


I want to set the size of all elements as the size of the first selected element. but seems something wrong with my code , somehow first element size not working for others.

Please see attached script

/*
 * Make all elements same size
 */
function sameSizeElements() {
  var selection = SlidesApp.getActivePresentation().getSelection();
  var selectionType = selection.getSelectionType();
  var pageElements = selection.getPageElementRange().getPageElements();
  
  //iterate the selected page elements to grab the values of each positiion
  for (var i = 0; i < pageElements.length; i++) {
  
    if(i != 0){
      pageElements[i].setWidth(pageElements[0].getWidth());
      pageElements[i].setHeight(pageElements[0].getHeight());
    }
  }   
}

Here is the full code you can put in ScriptEditor > code.js default file and refresh slide. it will work as you want to debug.

Updated

We are in the discussion with Google App Script issue tracking team - https://issuetracker.google.com/issues/162545277

set this protity and check it works


回答1:


As the current workaround, how about this method?

Issue and workaround:

In the current stage, unfortunately, it seems that this bug is still not resolved. By this, in your case, the 1st selected image cannot be retrieved. I think that this is the current reason of your issue.

In order to achieve your goal, as the current workaround, I would like to propose the following flow.

  1. Select an image which is used as the basic size and script is run.
    • The object ID of image is saved to the PropertiesService.
  2. Select the images you want to resize and the script is run.

When your script is modified for this flow, it becomes as follows.

Modified script:

function allMenu(){
  var slideUi = SlidesApp.getUi();
  slideUi.createMenu('LAK')
      .addSeparator()
      .addSubMenu(slideUi.createMenu('Sizes')
      .addItem('Select base image', 'selectBaseImage')  // Added
      .addItem('Same Size', 'sameSizeElements'))
      .addToUi();
}

// 1. At first, it saves an image which is used as the base image.
function selectBaseImage() {
  var selection = SlidesApp.getActivePresentation().getSelection();
  var pageElements = selection.getPageElementRange().getPageElements();
  if (pageElements.length == 1) {
    PropertiesService.getScriptProperties().setProperty("baseImage", pageElements[0].getObjectId());
  } else {
    throw new Error("Select one image.");
  }
}

// 2. As the next step, the selected images are resized using the saved image.
function sameSizeElements() {
  var prop = PropertiesService.getScriptProperties();
  var objectId = prop.getProperty("baseImage");
  if (objectId != "") {
    var selection = SlidesApp.getActivePresentation().getSelection();
    var baseImage = selection.getCurrentPage().getPageElementById(objectId);
    var pageElements = selection.getPageElementRange().getPageElements();
    for (var i = 0; i < pageElements.length; i++) {
      pageElements[i].setWidth(baseImage.getWidth());
      pageElements[i].setHeight(baseImage.getHeight());
    }
    prop.deleteProperty("baseImage");
  } else {
    throw new Error("Base image was not found.");
  }
}

Result:

References:

  • getSelection()
  • Properties Service



回答2:


Additional context to the issue:

when a server-side function is invoked, a ctx object is sent to the /invoke endpoint that, among other info (session id ssid, document id docId, revision number rev, and application type app), contains data about currently selected PageElements in property value with key sel.

The value is an array of arrays of strings and numbers, of which only one of the elements contains strings - if you look closely, these strings are object Ids (you can confirm with getObjectId method) in order of selection.

At least this confirms that the order of selection can be respected, but the way how the abovementioned list is managed server-side changes that order. Unfortunately, I could not confirm any particular order of how the elements (apart from grouping of Ids of form g8edc625556_0_0) are returned by .getSelection().getPageElementRange().getPageElements() chain of methods:



来源:https://stackoverflow.com/questions/63172078/how-to-convert-elements-to-the-same-size-as-the-first-selected-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!