Puz*_*Boy 1 image add-on google-apps-script google-slides
如果图像位于页面框架之外,我想使用 Google App Script 裁剪图像,但据我检查 Google App Script 文档,我找不到裁剪图像的方法。
pageElements.asImage().replace (imgBlob, true);不允许将裁剪尺寸作为参数传递来.replace()裁剪图像。
我知道这可以使用自定义 API 来实现,传递图像 blob 和裁剪区域,以便调用另一台服务器上的裁剪方法。
但是如何使用 Google App Script,寻求专家建议。
这个答案怎么样?
我认为在现阶段,replace(blobSource, crop)是有局限性的。官方文档是这样说的。
cropBoolean:如果为 true,则裁剪图像以适合现有图像的大小。否则,图像将被缩放并居中。
我确认,当使用 裁剪图像时replace(blobSource, crop),图像的中心会留下。看来这是当前的规范。而且虽然Slides API中有“UpdateImagePropertiesRequest”的“cropProperties”,但不幸的是,在现阶段,这个仍然不能使用。这已经被报道过。参考号
replace(blobSource, crop)如果在当前规范下使用,下面的示例脚本怎么样?作为样本情况,在第一张幻灯片中准备了“image1”和“image2”这两个图像,并且使用“image2”裁剪了“image1”。
该脚本的流程如下。
function myFunction() {
// 1. Retrieve 2 images from a slide on Google Slides.
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var images = slide.getImages();
var image1 = images[0]; // Red image.
var image2 = images[1]; // Blue image.
// 2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
var replacedImage = image2.replace(image1.getBlob(), true);
// 3. Move the cropped image to "image1".
replacedImage.setTop(image1.getTop()).setLeft(image1.getLeft());
// 4. Remove the original "image1".
image1.remove();
}
Run Code Online (Sandbox Code Playgroud)
运行脚本时,“image1”将被裁剪。但发现在当前阶段,“image1”的中心被裁剪留下了。
在使用的附加示例脚本中replace(blobSource, crop),我想提出使用自我图像的方法。在此示例脚本中,当图像伸出时,页面外的图像将通过裁剪被删除。基本方法与上面的示例脚本相同。
function myFunction() {
var s = SlidesApp.getActivePresentation();
var slide = s.getSlides()[0];
var images = slide.getImages();
var image = images[0];
var pageWidth = s.getPageWidth();
var imagePosition = image.getLeft();
var imageWidth = image.getWidth();
var check = imagePosition + imageWidth - pageWidth;
if (check > 0 && check < imageWidth) {
image
.duplicate()
.setWidth(pageWidth - imagePosition)
.asImage()
.replace(image.getBlob(), true);
image.remove();
}
}
Run Code Online (Sandbox Code Playgroud)