Google apps script Google Sheets应用程序脚本>;查找图像比率

Google apps script Google Sheets应用程序脚本>;查找图像比率,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我使用的是GoogleSheets脚本语言,我想从web链接中获取图像的比例,例如 我已经看到了一种方法,但是我想从函数中返回比率,而不是把它放在一个随机的单元格中 到目前为止,我已经设法做到了这一点,这似乎不起作用 function imageSize(imgUrl) { if (typeof imgUrl == 'undefined') { imgUrl = 'http://www.google.com/intl/en_ALL/images/logo.gif'; } P

我使用的是GoogleSheets脚本语言,我想从web链接中获取图像的比例,例如

我已经看到了一种方法,但是我想从函数中返回比率,而不是把它放在一个随机的单元格中

到目前为止,我已经设法做到了这一点,这似乎不起作用

function imageSize(imgUrl) {
  if  (typeof imgUrl == 'undefined') {
    imgUrl = 'http://www.google.com/intl/en_ALL/images/logo.gif';
  }
  PropertiesService.getScriptProperties().setProperty('ratio', 0);

  var t = '';     
  t += '<script>\n';
  t += 'function hd(){\n';
  t += 'var img = new Image();\n';
  t += 'img.onload = function() {\n';
  t += "google.script.run.returnVal(this.width / this.height);\n";
  t += '}\n';
  t += "img.src = '" + imgUrl + "';\n";
  t += '}\n';
  t += 'google.script.run.withSuccessHandler(google.script.host.close)\n';
  t += '.returnVal(hd());\n';
  t += '</script>\n';  


  var output = HtmlService.createHtmlOutput(t);
  // output.setSandboxMode(HtmlService.SandboxMode.IFRAME); 
  // SpreadsheetApp.getUi().showModalDialog(output,'please, wait...');
  // output.clear();
  Utilities.sleep(2000);
  return PropertiesService.getScriptProperties().getProperty('ratio');;
}


function returnVal(h) {
  Logger.log(h);
  PropertiesService.getScriptProperties().setProperty('ratio', h);
  return h;
}
函数图像大小(imgUrl){
如果(imgUrl的类型=='undefined'){
伊姆古尔http://www.google.com/intl/en_ALL/images/logo.gif';
}
PropertiesService.getScriptProperties().setProperty('ratio',0);
var t='';
t+='\n';
t+='函数hd(){\n';
t+='var img=new Image();\n';
t+='img.onload=function(){\n';
t+=“google.script.run.returnVal(this.width/this.height);\n”;
t+='}\n';
t+=“img.src=”+imgUrl+”;\n”;
t+='}\n';
t+=“google.script.run.withSuccessHandler(google.script.host.close)\n”;
t+='.returnVal(hd());\n';
t+='\n';
var output=HtmlService.createHtmlOutput(t);
//setSandboxMode(HtmlService.SandboxMode.IFRAME);
//SpreadsheetApp.getUi().showModalDialog(输出,'please,wait…');
//output.clear();
公用事业.睡眠(2000年);
返回PropertiesService.getScriptProperties().getProperty('ratio');;
}
函数returnVal(h){
Logger.log(h);
PropertiesService.getScriptProperties().setProperty('ratio',h);
返回h;
}

这个变通方法怎么样?我认为对你的情况有几个答案。所以,请把这看作是其中之一。此脚本的流程如下所示

function myFunction(url) {
  var blob = UrlFetchApp.fetch(url).getBlob();
  var res = ImgApp.getSize(blob);
  var aspectRatio = res.width / res.height;
  return aspectRatio;
}
  • 将图像作为文件下载
  • 使用驱动器API从下载的文件中检索imageMediaMetadata
  • 删除下载的文件
  • 从imageMediaMetadata检索纵横比
  • 为了使用此示例脚本,请在高级Google服务和API控制台中启用驱动API,如下所示

    function myFunction(url) {
      var blob = UrlFetchApp.fetch(url).getBlob();
      var res = ImgApp.getSize(blob);
      var aspectRatio = res.width / res.height;
      return aspectRatio;
    }
    
    在高级Google服务中启用驱动器API v2
    • 关于脚本编辑器
      • 资源->高级谷歌服务
      • 打开驱动器API v2
    • 关于脚本编辑器
      • 资源->云平台项目
      • 查看API控制台
      • 开始时,单击“浏览并启用API”
      • 在左侧,单击“库”
      • 在搜索API和服务时,输入“驱动器”。然后单击驱动API
      • 单击启用按钮
      • 如果API已经启用,请不要关闭
    示例脚本 注:
    • https://www.theodora.com/flags/e_timor.gif
      在您的问题中,此脚本使用了
      1.5
    参考资料:
    如果这不是你想要的,我很抱歉

    补充: 如果不想在Google Drive中创建文件,可以使用检索纵横比。这个库可以从blob中检索图像大小,因为图像的二进制数据是经过解析的。当此库用于您的情况时,脚本如下所示

    function myFunction(url) {
      var blob = UrlFetchApp.fetch(url).getBlob();
      var res = ImgApp.getSize(blob);
      var aspectRatio = res.width / res.height;
      return aspectRatio;
    }
    

    您希望检索图像的纵横比。我明白这一点。但是我不能理解
    不把它放在一个随机的单元格中,而且
    似乎不起作用。我能问一下他们吗?很抱歉我的英语水平很差。@Tanaike谢谢你关注这个。基于此的脚本在运行宏的工作表中有一个按钮,宏将把比率放在单元格B2:B11中。我不想按下按钮并将值放入B2:B11,我希望函数只返回值。当我说
    似乎不起作用时
    我的意思是我总是得到一个0.0的比率谢谢你的回复。我能理解你想做什么。所以我贴了一个答案。你能确认一下吗?谢谢@Tanaike!我决定使用这个图书馆,它工作得很好:)@Mark谢谢你的回复。我很高兴你的问题解决了。我为这种情况创建了这个库。也谢谢你。