如何将Google Sheets单元格数据中的链接转换为HTML锚标签?

huangapple go评论72阅读模式
英文:

How can I convert links in Google Sheets cell data to HTML anchor tags?

问题

I have some hyperlinked text.

转换为:

I have some <a href="https://www.google.com">hyperlinked text</a>.

英文:

I'm trying to convert something like this:

I have some hyperlinked text.

Into this:

I have some &lt;a href=&quot;https://www.google.com&quot;&gt;hyperlinked text&lt;/a&gt;.

However, it seems as though links created in Google Sheets using this method are considered part of the text's formatting, so I haven't been able to convert one to the other.

It's worth noting that I'm not using the HYPERLINK function here, just the "Insert Link (Ctrl+K)" method.

答案1

得分: 1

以下是翻译好的部分:

"如果 Google Sheets API 不提供捕获链接的方法,那会有点奇怪。您可以这样做(尽管有点巧妙,如果可能的话最好避免使用),使用一些正则表达式来捕获文本中的链接,并将它们包装在链接标签中。

^(http(s)?:\/\/.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&amp;//=]*)$

您是如何获取这些数据的?您是在使用 API,还是想直接在表格中进行操作?

如果您在表格中工作,JavaScript 支持宏。如果是这种情况,只需编写一个宏来循环处理您的数据并将超链接转换为标签。类似于以下示例:

/**
* 将富文本链接转换为 HTML 链接。
*
* @param {"A2:A42"} rangeA1 指定要将富文本链接转换为 HTML 链接的单元格范围的文本字符串。
* @param {A2:A42} dynamic_reference 可选。与普通范围引用相同的范围,以确保结果动态更新。
* @return {String[][]} 将富文本链接转换为 HTML 链接的文本。
* @customfunction
*/
function RichTextLinksToHtmlLinks(rangeA1, dynamic_reference) {
  // 版本 1.0,由 --Hyde 于 2022 年 3 月 30 日编写
  // - 初始版本,基于 RichTextLinks 1.1
  // - 许可证 https://www.gnu.org/licenses/gpl-3.0.html
  // - 请参阅 https://support.google.com/docs/thread/157878605
  if (typeof rangeA1 !== 'string') {
    throw new Error('RichTextLinksToHtmlLinks 期望 rangeA1 是一个文本字符串,如 "A2:A42",但实际得到的是 ' + Object.prototype.toString.call(rangeA1) + '。');
  }
  const _escapeHTML = (text) => text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  const range = SpreadsheetApp.getActiveSheet().getRange(rangeA1);
  const htmlArray = range.getRichTextValues()
    .map(row => row
      .map(value => value.getRuns()
        .map(run => {
          const link = run.getLinkUrl();
          const text = _escapeHTML(run.getText());
          return link ? `<a href="${link}" target="_blank">${text}</a>` : text;
        })
        .join('')
      )
    );
  return htmlArray.map(row => row.map(html => `<p>${html}</p>`));
}

我从这里获取的。

英文:

It's weird if the google sheets api doesn't provide methods to capture links. What you can do (although it's a bit hacky and I'd avoid it if I could) is use some regex like

^(http(s)?:\/\/.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&amp;//=]*)$

to capture the links in your text and wrap them in a link tag.

How are you getting this data? Are you using an API, or do you want to do this directly in sheets?

If you're working inside sheets, there is macros support in javascript. If that's the case simply write a macro to loop through your data and convert hyperlinks to tags. Something like

/**
* Converts rich text links to HTML links.
*
* @param {&quot;A2:A42&quot;} rangeA1 A text string that specifies the cell range where to convert rich text links to HTML links.
* @param {A2:A42} dynamic_reference Optional. The same range as a normal range reference, to ensure that the results update dynamically.
* @return {String[][]} The text with rich text links converted to HTML links.
* @customfunction
*/
function RichTextLinksToHtmlLinks(rangeA1, dynamic_reference) {
  // version 1.0, written by --Hyde, 30 March 2022
  //  - initial version, based on RichTextLinks 1.1
  //  - license https://www.gnu.org/licenses/gpl-3.0.html
  //  - see https://support.google.com/docs/thread/157878605
  if (typeof rangeA1 !== &#39;string&#39;) {
    throw new Error(&#39;RichTextLinksToHtmlLinks expected rangeA1 to be a text string like &quot;A2:A42&quot; but got &#39; + Object.prototype.toString.call(rangeA1) + &#39; instead.&#39;);
  }
  const _escapeHTML = (text) =&gt; text.replace(/&amp;/g, &#39;&amp;amp;&#39;).replace(/&lt;/g, &#39;&amp;lt;&#39;).replace(/&gt;/g, &#39;&amp;gt;&#39;);
  const range = SpreadsheetApp.getActiveSheet().getRange(rangeA1);
  const htmlArray = range.getRichTextValues()
    .map(row =&gt; row
      .map(value =&gt; value.getRuns()
        .map(run =&gt; {
          const link = run.getLinkUrl();
          const text = _escapeHTML(run.getText());
          return link ? `&lt;a href=&quot;${link}&quot; target=&quot;_blank&quot;&gt;${text}&lt;/a&gt;` : text;
        })
        .join(&#39;&#39;)
      )
    );
  return htmlArray.map(row =&gt; row.map(html =&gt; `&lt;p&gt;${html}&lt;/p&gt;`));
}

which I got from here

huangapple
  • 本文由 发表于 2023年8月4日 04:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定