【发布时间】: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 cell和doesn't seem to work。我能问你关于他们的事吗?我很抱歉我的英语不好。 -
@Tanaike 感谢您关注此问题。我基于here 的脚本在运行宏的工作表中有一个按钮,宏会将比率放在单元格 B2:B11 中。我不想按下按钮并将值放在 B2:B11 中,我希望函数只返回值。当我说
doesn't seem to work时,我的意思是我总是得到0.0的比率 -
感谢您的回复。我能理解你想做什么。所以我发布了一个答案。你能确认一下吗?
标签: google-apps-script google-sheets