下面的代码自动获取电子表格中某个列的最后一个链接。我需要创建一个网页应用程序的按钮,每次点击它运行脚本,并自动打开一个新窗口,在列的最后一个链接。我需要它的形式的网页应用程序,因为我需要嵌入的网址到一个按钮的外部网站。
提取列中最后一个链接的.gs代码如下所示:
function obtainurl(){
var summarySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary');
var lastRow = "AA" + summarySheet.getLastRow();
var new_url = summarySheet.getRange(lastRow).getValues();
var new_url1= [].concat.apply([], new_url)
return new_url1;
}上面的代码可以工作,并返回我与logger.log检查过的正确链接。然后,我有另一个.gs函数的组合:
function doGet(){
return HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50);
} 而对应的html代码:
<html>
<head>
<base target="_blank">
<script>
//Here is where i have the problem I don't know how to pass the url into the var above and get the correct link opened everytime.
var winRef = window.open(new_url);
winRef ? google.script.host.close() : window.alert('Allow popup to redirect you to '+url1) ;
window.onload=function(){document.getElementById('url').href = url1;}
</script>
</head>
<body>
If the download has not started please disable your ad-blocker! Or else contact us at -----
</body>
</html>总之,我不知道如何将传递给html文件。我已经检查过了,如果我将完整的url硬编码到html文件中,它就能工作。但是,这需要是动态的,总是打开电子表格列中的最后一个url。
提前谢谢你,我知道我是个新手。
发布于 2020-10-22 22:01:41
您需要使用.gs从客户机调用google.script.run函数,并在服务器返回值后指定要执行的函数。
// Calls server function obtainurl()
google.script.run.withSuccessHandler(showUrl).obtainurl();
// Function executed after server returns value
function showUrl(url) {
alert(url);
}这里有更多详细信息:https://developers.google.com/apps-script/guides/html/reference/run
https://stackoverflow.com/questions/64486648
复制相似问题