问题
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
回答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.
- Select an image which is used as the basic size and script is run.
- The object ID of image is saved to the PropertiesService.
- 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 PageElement
s 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