如何使用Google Doc API脚本在Google文档中的表(或数组)中添加超链接?

发布于 2025-01-23 02:51:21 字数 629 浏览 0 评论 0原文

我有以下代码在Google文档中附加表。

var sss = SpreadsheetApp.openById('id of spreadsheet'); 
var rawData =  sss.getDataRange().getValues()    

var data = []  
for (var i = 0; i< rawData.length; i++){
tempData=[]
tempData=[rawData[i][1],rawData[i][2],rawData[i][3]]
data.push(tempData)

}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();

body.appendTable(data).editAsText().setBold(false);

此代码正常。问题是rawdata [i] [3]中有URL。它在Google文档中显示为纯文本。如何将其转换为超链接?如果可以将其写入body.appendParagraph(“我的链接”).setlinkurl(“ http://www.google.com”),那就更好了。问题在于它在数组中,而不是在段落中。

I am having the following code for appending table in Google Docs.

var sss = SpreadsheetApp.openById('id of spreadsheet'); 
var rawData =  sss.getDataRange().getValues()    

var data = []  
for (var i = 0; i< rawData.length; i++){
tempData=[]
tempData=[rawData[i][1],rawData[i][2],rawData[i][3]]
data.push(tempData)

}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();

body.appendTable(data).editAsText().setBold(false);

This code works fine. The problem is that there is url in rawdata[i][3]. It gets displayed in Google doc as plain text. How can I convert it into hyperlink? It would be even better if it is possible to write it as body.appendParagraph("my link").setLinkUrl("http://www.google.com"). The problem is that it is in an array, not in paragraph.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2025-01-30 02:51:22

我相信您的目标如下。

  • 您想通过从Google电子表格中检索值来放置Google文档的表格。
  • 在您的电子表格中,列“ D”具有超链接。而且,您想将值设置为超链接。

在这种情况下,以下修改如何?

修改后的脚本:

var sss = SpreadsheetApp.openById('id of spreadsheet'); 
var rawData = sss.getDataRange().getValues();
var data = []
for (var i = 0; i < rawData.length; i++) {
  tempData = []
  tempData = [rawData[i][1], rawData[i][2], rawData[i][3]]
  data.push(tempData)
}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();

// I modified below script.
var table = body.appendTable(data);
for (var r = 0; r < table.getNumRows(); r++) {
  var obj = table.getCell(r, 2).editAsText();
  var text = obj.getText();
  if (/^https?:\/\//.test(text)) obj.setLinkUrl(text);
}
table.editAsText().setBold(false);
  • 运行此脚本时,使用从电子表格检索的值放置表。并且,关于表的“ C”列,文本已更改为超链接。

注意:

  • 此修改后的脚本认为您的“ D”列值就像https:// ###http:// ####。请小心。

  • 如果要使用超链接使用特定文本(例如,单击此处),请按以下方式修改。

    • 来自

        if(/^https?: \////.test(text))obj.setlinkurl(text);
       
    • to

        if(/^https?: \///.test(text))obj.setText(“单击此处”)。setLinkurl(text);
       

文献:

I believe your goal is as follows.

  • You want to put a table to Google Document by retrieving the values from Google Spreadsheet.
  • In your Spreadsheet, the column "D" has the hyperlinks. And, you want to set the value as the hyperlink.

In this case, how about the following modification?

Modified script:

var sss = SpreadsheetApp.openById('id of spreadsheet'); 
var rawData = sss.getDataRange().getValues();
var data = []
for (var i = 0; i < rawData.length; i++) {
  tempData = []
  tempData = [rawData[i][1], rawData[i][2], rawData[i][3]]
  data.push(tempData)
}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();

// I modified below script.
var table = body.appendTable(data);
for (var r = 0; r < table.getNumRows(); r++) {
  var obj = table.getCell(r, 2).editAsText();
  var text = obj.getText();
  if (/^https?:\/\//.test(text)) obj.setLinkUrl(text);
}
table.editAsText().setBold(false);
  • When this script is run, a table is put using the values retrieved from Spreadsheet. And, about the column "C" of the table, the text is changed to the hyperlink.

Note:

  • This modified script supposes that your values of column "D" are like https://### and http://###. Please be careful about this.

  • If you want to give the specific text (for example, click here) with the hyperlink, please modify as follows.

    • From

        if (/^https?:\/\//.test(text)) obj.setLinkUrl(text);
      
    • To

        if (/^https?:\/\//.test(text)) obj.setText("click here").setLinkUrl(text);
      

References:

和影子一齐双人舞 2025-01-30 02:51:22

您可以尝试以下内容,以在Google Doc可单击中制作所有URL:

function urlClickable() {
  var urlRegex = 'http[s]?:\/\/[^ ]+';
  var doc = DocumentApp.openById('yourDocId');
  var body = doc.getBody();
  var urlElement = body.findText(urlRegex);  

  while (urlElement != null) {    
    var text = urlElement.getElement().asText();

    var startOffset = urlElement.getStartOffset();
    var endOffset = urlElement.getEndOffsetInclusive();

    text.setLinkUrl(startOffset, endOffset, getUrl(text.getText()));

    urlElement = body.findText(urlRegex, urlElement);
  }
}

function getUrl(text) {
  var startOffset = text.indexOf('http');
  var endOffset = text.indexOf(' ', startOffset);

  if (endOffset === -1) {
    endOffset = text.length;
  }
  return text.substring(startOffset, endOffset);
}   

You can try the following to make all URLs in a Google Doc clickable:

function urlClickable() {
  var urlRegex = 'http[s]?:\/\/[^ ]+';
  var doc = DocumentApp.openById('yourDocId');
  var body = doc.getBody();
  var urlElement = body.findText(urlRegex);  

  while (urlElement != null) {    
    var text = urlElement.getElement().asText();

    var startOffset = urlElement.getStartOffset();
    var endOffset = urlElement.getEndOffsetInclusive();

    text.setLinkUrl(startOffset, endOffset, getUrl(text.getText()));

    urlElement = body.findText(urlRegex, urlElement);
  }
}

function getUrl(text) {
  var startOffset = text.indexOf('http');
  var endOffset = text.indexOf(' ', startOffset);

  if (endOffset === -1) {
    endOffset = text.length;
  }
  return text.substring(startOffset, endOffset);
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文