在Google Spreadsheets中进行修改后,调整所有图像的大小 [英] Resize all images after an edit in google Spreadsheets

查看:130
本文介绍了在Google Spreadsheets中进行修改后,调整所有图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个基于搜索栏导入不同图像的文档(它会自动生成带有信息的传单).

I'm working in a document that imports different images based on a search bar (it sort of automatically generates a flyer with information).

但是图像的缩放比例不好,因此我正在尝试使用Google脚本来解决此问题.在没有编程知识的情况下,我设法提出了以下脚本,但到目前为止,它什么也没做:)我希望有人可以给我一些有关我做错事情的指示.

The images however, do not scale nicely so I'm trying google scripts to solve this issue. Without prior knowledge of programming I've managed to come up with the following script but as of now it does exactly nothing :) I hope someone can give me some pointers as to what I'm doing wrong.

提前谢谢!

function onEdit() {
 
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet =ss.getActiveSheet();
  var images = activeSheet.getImages();
  
  for (var i = 0; i < images.length; i++) {
    
  var originW =  images.getWidth();
  var originH = images.getHeight();
  var newW = originW;
  var newH = originH;
  var ratio = originW/originH
    
  if(originW>maxWidth){
    newW = maxWidth;
    newH = parseInt(newW/ratio);
  }
  
  images.setWidth(newW).setHeight(newH).setAttributes(styleImage);
  var newWW = images.getWidth();
  var newHH = images.getHeight();
  var newRatio = newHH/newWW;
  Logger.log("image width = "+newWW);
  Logger.log("image height = "+newHH);

  if(newHH>maxWidth){
    newHH = maxHeight;
    newWW = parseInt(newHH/newRatio);
  }
  
  images.setWidth(newWW).setHeight(newHH);
  images.getParent().setAttributes(styleImage);
      
  }
}

推荐答案

所以 OverGridImage 对象. 这意味着您将必须遍历数组并修改每个对象,而这些对象几乎正在做.您有一个for循环,但是继续使用对象数组而不是选择其中的对象. images ==> images[i]在for循环中.

So .getImages() return an array of OverGridImage objects. This means that you will have to run through the array and modify each individual object, which you are almost doing. You have a for loop, but then go on to use the object array instead of selecting the objects within. images ==> images[i] within the for loop.

function onEdit() {

  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet =ss.getActiveSheet();
  var images = activeSheet.getImages();

  for (var i = 0; i < images.length; i++) {

  var originW =  images[i].getWidth();
  var originH = images[i].getHeight();
  var newW = originW;
  var newH = originH;
  var ratio = originW/originH

  if(originW>maxWidth){
    newW = maxWidth;
    newH = parseInt(newW/ratio);
  }

  images[i].setWidth(newW).setHeight(newH).setAttributes(styleImage);
  var newWW = images[i].getWidth();
  var newHH = images[i].getHeight();
  var newRatio = newHH/newWW;
  Logger.log("image width = "+newWW);
  Logger.log("image height = "+newHH);

  if(newHH>maxWidth){
    newHH = maxHeight;
    newWW = parseInt(newHH/newRatio);
  }

  images[i].setWidth(newWW).setHeight(newHH);
  images[i].getParent().setAttributes(styleImage);

  }
}

这篇关于在Google Spreadsheets中进行修改后,调整所有图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆