on google script editor of ogle spreadsheets, I am trying to created a webpage which retrieves values from spreadsheet and displays. However, it is not displaying the items linked on the spreadsheet.It is displaying static values but not links. what am I doing wrong? Given below is my code.
Below code in my .gs sheet
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getData() {
return SpreadsheetApp
.openById('abcdef')
.getActiveSheet()
.getDataRange()
.getValues();
getURL();
}
function getURL() {
var range = SpreadsheetApp.getActiveSheet().getActiveCell();
var links = SpreadsheetApp.getActiveRange().getFormulas();
//logs - Google
var values = range.getValues();
//logs - =HYPERLINK("http://www.google.com", "Google")
var formula = range.links;
//simple regex to get first quoted string
var url = /"(.*?)"/.exec(formula);
//logs - http://www.google.com
return url;
}
My index.html has the following code
<? var data = getData(); ?>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td>
<a id="button" href=#><?= data[i][j] ?> </a>
</td>
<? } ?>
</tr>
<? } ?>
</table>
Playing with Sandy's code above I found that most urls were refused for many reasons (non https, same origin, etc) so I looked at the doc and ended with this version that includes a target="_blank"
parameter and that opens all the urls I tried.
<? var data = getData(); ?>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr >
<? for (var j = 0; j < data[i].length; j++) { ?>
<td > <a id="button" href=" <?= data[i][j] ?> " target="_blank"> <?= data[i][j] ?></a></td>
<? } ?>
</tr>
<? } ?>
</table>
note also that urls in the spreadsheet must be in the form http://www.google.com to work, a short form like www.google.com won't work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.