假设我在单元格A1中有一个超链接:=hyperlink("stackexchange.com", "Stack Exchange")

在工作表的其他地方,我希望有一些公式分别从A1中获取链接文本和URL。我找到了一种只获取链接文本的方法:

=""&A1 


(用空字符串连接)。这将返回未链接的“堆栈交换”。

如何获取URL(stackexchange.com)?

评论

这是可以执行的脚本:productforums.google.com/forum/#!topic/docs/ymxKs_QVEbs

访客注意:如果您正在寻找一种从格式链接中提取URL的方法,而该格式链接不是= hyperlink()(已粘贴到工作表中的内容),那么抱歉:没有。最好不要将富文本粘贴到电子表格中。

复制了stackoverflow.com/questions/28474308/…?

访客注意2:如果您以html下载电子表格,则可以同时获得他们。或者更确切地说,它们很容易从html中提取出来。...不是理想的方法,但这是一种方法。

#1 楼

看到Rubén的答案后,我决定为此任务编写一个不同的自定义函数,该函数具有以下功能:


该参数以范围而不是字符串的形式提供:即=linkURL(C2)代替=linkURL("C2")。这与参数通常如何工作是一致的,并且使引用更健壮:如果有人在顶部添加新行,则将保留引用。
支持数组:=linkURL(B2:D5)返回在此范围内找到的所有hyperlink命令的URL(其他地方为空白单元格)。

要实现1,我不使用工作表(这将是目标单元格的文本内容),而是解析公式=linkURL(...)本身并从中提取范围符号。

/** 
 * Returns the URL of a hyperlinked cell, if it's entered with hyperlink command. 
 * Supports ranges
 * @param {A1}  reference Cell reference
 * @customfunction
 */
function linkURL(reference) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var formula = SpreadsheetApp.getActiveRange().getFormula();
  var args = formula.match(/=\w+\((.*)\)/i);
  try {
    var range = sheet.getRange(args[1]);
  }
  catch(e) {
    throw new Error(args[1] + ' is not a valid range');
  }
  var formulas = range.getFormulas();
  var output = [];
  for (var i = 0; i < formulas.length; i++) {
    var row = [];
    for (var j = 0; j < formulas[0].length; j++) {
      var url = formulas[i][j].match(/=hyperlink\("([^"]+)"/i);
      row.push(url ? url[1] : '');
    }
    output.push(row);
  }
  return output
}


评论


效果很好,尽管有点慢。

–丹妮德
16-2-26在23:32

从技术上讲这是可行的,但是我想知道是否有可能基于linkURL()结果创建一个新的超链接。例如= HYPERLINK(linkURL(C2),“新标签”)对我似乎不起作用。

–skube
16年4月8日在17:52

@skube这是我对该函数进行编码的一个副作用:它只能单独使用,不能与其他函数一起使用。您仍然可以使用= hyperlink(D2,“ new label”)创建新的超链接,其中D2具有linkURL公式。或者,使用Rubén的自定义功能。

–user79865
16年4月8日在17:58

似乎Google表格已更改,超链接不再存储为= HYPERLINK公式,因此上述解决方案不再起作用... take.ms/9bhpN

– Pavel'Strajk'Dolecek
20年9月1日于13:12

#2 楼

简短答案

使用自定义函数在单元格公式中获取带引号的字符串。

代码

Yisroel在评论中分享了此外部帖子Tech包含一个脚本,该脚本用相应公式中的第一个带引号的字符串替换有效范围内的每个公式。以下是该脚本的自定义功能的改编。



/** 
 * Extracts the first text string in double quotes in the formula
 * of the referred cell
 * @param {"A1"}  address Cell address.
 * @customfunction
 */
function FirstQuotedTextStringInFormula(address) {
  // Checks if the cell address contains a formula, and if so, returns the first
  // text  string in double quotes in the formula.
  // Adapted from https://productforums.google.com/d/msg/docs/ymxKs_QVEbs/pSYrElA0yBQJ

  // These regular expressions match the __"__ prefix and the
  // __"__ suffix. The search is case-insensitive ("i").
  // The backslash has to be doubled so it reaches RegExp correctly.
  // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp

  if(address && typeof(address) == 'string'){

    var prefix = '\"';
    var suffix = '\"';
    var prefixToSearchFor = new RegExp(prefix, "i");
    var suffixToSearchFor = new RegExp(suffix, "i");
    var prefixLength = 1; // counting just the double quote character (")

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var cell, cellValue, cellFormula, prefixFoundAt, suffixFoundAt, extractedTextString;

    cell = ss.getRange(address);
    cellFormula = cell.getFormula();

    // only proceed if the cell contains a formula
    // if the leftmost character is "=", it contains a formula
    // otherwise, the cell contains a constant and is ignored
    // does not work correctly with cells that start with '=
    if (cellFormula[0] == "=") {

      // find the prefix
      prefixFoundAt = cellFormula.search(prefixToSearchFor);
      if (prefixFoundAt >= 0) { // yes, this cell contains the prefix
        // remove everything up to and including the prefix
        extractedTextString = cellFormula.slice(prefixFoundAt + prefixLength);
        // find the suffix
        suffixFoundAt = extractedTextString.search(suffixToSearchFor);
        if (suffixFoundAt >= 0) { // yes, this cell contains the suffix
          // remove all text from and including the suffix
          extractedTextString = extractedTextString.slice(0, suffixFoundAt).trim();

          // store the plain hyperlink string in the cell, replacing the formula
          //cell.setValue(extractedTextString);
          return extractedTextString;
        }
      }
    } else {
      throw new Error('The cell in ' + address + ' does not contain a formula');
    }
  } else {
    throw new Error('The address must be a cell address');
  }
}


评论


此函数对我来说更好,因为它可以在其他表达式中使用。顺便说一下,它使用{“ A1”}表示法来寻址单元。

– vatavale
17年8月20日在20:26



#3 楼

假设单元格具有超链接功能;只需找到并将=hyperlink替换为“ hyperlink”或“ xyz”

,那么您只需要进行一些数据清理即可将它们分开。
尝试将拆分文本用于列或=split函数。
两者都将,作为分隔符。

再次将" [双引号]替换为[nothing]

似乎更简单..