Google Sheets Apps脚本>查找图像比例 [英] Google Sheets Apps Script > Finding Image Ratio

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

问题描述

我使用的是Google表格脚本语言,并且我想从Web链接中获取图片的比例,例如此标记.

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

推荐答案

此解决方法如何?我认为您的情况有几个答案.因此,请将此视为其中之一.该脚本的流程如下.

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
    • On script editor
      • Resources -> Advanced Google Services
      • Turn on Drive API v2
      • 在脚本编辑器上
        • 资源-> Cloud Platform项目
        • View API控制台
        • 在入门"中,单击探索并启用API".
        • 在左侧,单击库.
        • 在搜索API和服务,输入驱动器".然后点击Drive API.
        • 单击启用"按钮.
        • 如果已启用API,请不要关闭.
        • On script editor
          • Resources -> Cloud Platform project
          • View API console
          • At Getting started, click "Explore and enable APIs".
          • At left side, click Library.
          • At Search for APIs & services, input "Drive". And click Drive API.
          • Click Enable button.
          • If API has already been enabled, please don't turn off.
          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.
              • Advanced Google Services
              • Drive API

              如果这不是您想要的,对不起.

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

              如果您不想在Google云端硬盘中创建文件,则可以使用此库.该库可以从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 Sheets Apps脚本&gt;查找图像比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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