Google 表格应用程序脚本 >寻找图像比率 [英] Google Sheets Apps Script > Finding Image Ratio

查看:21
本文介绍了Google 表格应用程序脚本 >寻找图像比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用谷歌表格脚本语言,我想从网络链接中获取图像的比例,例如这个标志.

I'm using the google sheets scripting language, and I want to get the ratio of an image from a web link, e.g. this flag.

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

I have seen a way to do it here, but I want to return the ratio from the function, not put it in a random cell.

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

So far I have managed to get this, which doesn't seem to work.

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


  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;
}

推荐答案

这个解决方案怎么样?我认为对于您的情况有几种答案.因此,请将此视为其中之一.这个脚本的流程如下.

How about this workaround? I think that there are several answers for your situation. So please think of this as one of them. The flow of this script is as follows.

  1. 将图像下载为文件.
  2. 使用 Drive API 从下载的文件中检索 imageMediaMetadata.
  3. 删除下载的文件.
  4. 从 imageMediaMetadata 中检索纵横比.

要使用此示例脚本,请按如下方式在高级 Google 服务和 API 控制台中启用 Drive API.

In order to use this sample script, please enable Drive API at Advanced Google Services and API console as follows.

  • 在脚本编辑器上
    • 资源 -> 高级 Google 服务
    • 开启 Drive API v2
    • 在脚本编辑器上
      • 资源 -> 云平台项目
      • 查看 API 控制台
      • 在入门中,点击探索并启用 API".
      • 在左侧,点击图书馆.
      • 在搜索 API 和服务,输入驱动器".然后点击 Drive API.
      • 点击启用按钮.
      • 如果 API 已经启用,请不要关闭.
      function myFunction(url) {
        var blob = UrlFetchApp.fetch(url).getBlob();
        var fileId = DriveApp.createFile(blob).getId();
        var imageMediaMetadata = Drive.Files.get(fileId).imageMediaMetadata;
        var aspectRatio = imageMediaMetadata.width / imageMediaMetadata.height;
        Drive.Files.remove(fileId);
        return aspectRatio;
      }
      

      注意:

      • 当您问题中的 https://www.theodora.com/flags/e_timor.gif 用于此脚本时,将检索 1.5.
      • Note :

        • When https://www.theodora.com/flags/e_timor.gif in your question is used for this script, 1.5 is retrieved.
        • 如果这不是您想要的,我很抱歉.

          If this was not what you want, I'm sorry.

          如果您不想在 Google Drive 中创建文件,您可以使用这个库检索纵横比.这个库可以从 blob 中检索图像大小,因为图像的二进制数据被解析.当这个库用于你的情况时,脚本变成如下.

          If you don't want to create files in Google Drive, you can retrieve the aspect ratio using this library. This library can retrieve the image size from a blob, because the binary data of the image is parsed. When this library is used for your situation, the script becomes as follows.

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

          这篇关于Google 表格应用程序脚本 &gt;寻找图像比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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