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