【问题标题】:Google Sheets Apps Script > Finding Image RatioGoogle Sheets Apps 脚本 > 查找图像比例
【发布时间】:2018-08-07 22:50:58
【问题描述】:

我正在使用 google sheet 脚本语言,我想从网络链接中获取图像的比例,例如this flag.

我看到了it here 的一种方法,但我想从函数中返回比率,而不是将其放入随机单元格中。

到目前为止,我已经设法得到了这个,但这似乎不起作用。

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

【问题讨论】:

  • 您要检索图像的纵横比。我明白这一点。但我无法理解not put it in a random celldoesn't seem to work。我能问你关于他们的事吗?我很抱歉我的英语不好。
  • @Tanaike 感谢您关注此问题。我基于here 的脚本在运行宏的工作表中有一个按钮,宏会将比率放在单元格 B2:B11 中。我不想按下按钮并将值放在 B2:B11 中,我希望函数只返回值。当我说doesn't seem to work时,我的意思是我总是得到0.0的比率
  • 感谢您的回复。我能理解你想做什么。所以我发布了一个答案。你能确认一下吗?

标签: google-apps-script google-sheets


【解决方案1】:

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

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

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

在高级 Google 服务中启用 Drive API v2

  • 在脚本编辑器上
    • 资源 -> 高级 Google 服务
    • 开启 Drive API v2

Enable Drive API at API console

  • 在脚本编辑器上
    • 资源 -> 云平台项目
    • 查看 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

参考资料:

如果这不是你想要的,我很抱歉。

添加:

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

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

【讨论】:

  • 感谢@Tanaike!我决定使用该库,它运行良好:)
  • @Mark 感谢您的回复。我很高兴你的问题得到了解决。我为这种情况创建了这个库。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多