如何从谷歌应用程序脚本中的数据单元格中检索超链接? [英] How can I retrieve the hyperlink from a data cell in google apps script?
问题描述
我正在使用 SpreadsheetApp.getActiveRange().getValues();获取一系列单元格的值,但它不返回与该单元格关联的超链接,只返回文本.有什么方法可以获取超链接,还是必须添加另一个包含 url 的字段?
I am using SpreadsheetApp.getActiveRange().getValues(); to get the value of a range of cells, but it doesn't return the hyperlink associated with that cell, only the text. Is there any way to get the hyperlink or do I have to add another field with the url in it?
谢谢.
更新:这就是我最终使用它来构建链接列表的方式.
UPDATE: This is how I ended up using it to build a link list.
<ul>
<?
var range = SpreadsheetApp.getActiveRange();
var data = range.getValues();
var links = range.getFormulas();
for(var i=1; i < data.length; i++){
if(data[i][0] !== ''){
?>
<li><a href="<?= links[i][0].split(""")[1]; ?>"><?= data[i][0]; ?></a></li>
<?
}
}
?>
</ul>
推荐答案
与单元格文本关联的超链接由公式表示.Google 电子表格会自动将任何网址转换为可点击的超链接,但在这些情况下,不会使用公式,并且文本和网址相同.
A hyperlink associated with a cell text is manifested by a formula. Google Spreadsheets does automagically converts any URL into a clickable hyperlink but in those cases formulas are not used and the text and the URL are the same.
下面是一个非常简单的解决方案(没有错误检查),说明如何通过脚本获得它.我怀疑你是从 Excel 或其他东西导入的,那是你不容易看到 HYPERLINK 公式.
Below is a very simple solution (no error checking) as to how you can get this through scripts. I suspect you imported this from Excel or something else and that's you don't readily see the HYPERLINK formula.
如果你有一个像这样的单元格 -
If you have a cell that looks like this -
然后这个脚本会让你读取与之关联的 URL -
Then this script will let you read the URL associated with it -
function getURL() {
var range = SpreadsheetApp.getActiveSheet().getActiveCell();
//logs - Google
Logger.log(range.getValue());
//logs - =HYPERLINK("http://www.google.com", "Google")
Logger.log(range.getFormulaR1C1());
//simple regex to get first quoted string
var url = /"(.*?)"/.exec(range.getFormulaR1C1())[1];
//logs - http://www.google.com
Logger.log(url);
}
这篇关于如何从谷歌应用程序脚本中的数据单元格中检索超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!