如何使用 google 脚本在 google 文档中创建超链接数组(或一长串)

How to create an array (or one long string) of hyperlinks in google docs using google script

或者 google 文档的 hyperlink 在原始格式下的样子。

我尝试做下一件事:

var links;
var nameArr = ["1", "2", "3", "4", "5", "6"];
var tempArr= ["11", "12", "13", "14", "15", "16"];
for (i = 0; i < nameArr.length; i++) {
  nameArr[i].setUrlLink("https://en.wikipedia.org/wiki/" + tempArr[i] + "/detection"
  links = links + ", "+ nameArr[i];
}

我收到一个错误,因为我不能在字符串上使用 setLinkUrl,只能在文本对象上使用 - 没有找到将字符串转换为文本的方法。

虽然,如果我粘贴它 "as it","http..." 显示为常规字符串 - 而不是 link。

我想得到这样的东西: 1, 2, 3 ......并将其粘贴到 google docs 文档中。

链接是关联元素的 "rich" 特征(通常 Text). So to add a link to generic text, first you must get the associated Text element, and then invoke setLinkUrl 在上面。

与其他丰富的格式方法一样,附加元素继承前面同级元素的格式规范。因此,如果您格式化父元素的最后一个元素,则附加到父元素的下一个元素也可能以这种方式格式化。我为分隔符文本明确指定 nullstring URL 以避免 link 超出实际显示文本。 (这意味着如果您在调用此函数后以编程方式追加到 Paragraph,则追加的文本将与数组中最后显示的文本具有相同的 URL。)

这个简单的函数将 Paragraph 作为输入,连同显示文本数组和 URI,并将它们添加到末尾。

/**
 * Create links at the end of the given paragraph with the given text and the given urls.
 * @param {GoogleAppsScript.Document.Paragraph} pg The paragraph to hold the link array
 * @param {string[]} values The display text associated with the given links
 * @param {string[]} links The URI for the given link text
 * @param {string} [separator] text that should separate the given links. Default is comma + space, `", "`
 * @returns {GoogleAppsScript.Document.Paragraph} the input paragraph, for chaining
 */
function appendLinkArray(pg, values, links, separator) {
  if (!pg || !values || !links)
    return;
  if (!values.length || !links.length || values.length > links.length)
    throw new Error("Bad input arguments");
  if (separator === undefined)
    separator = ", ";

  // Add a space before the link array if there isn't one at the end of any existing text.
  if (pg.getText() && (!pg.getText().match(/ $/) || !pg.getText().match(/ $/).length))
    pg.appendText(" ").setLinkUrl("");
  // Add each link display text as a new `Text` object, and set its link url.
  links.forEach(function (url, i) {
    var text = values[i] || url;
    pg.appendText(text)
      .setLinkUrl(0, text.length - 1, url);
    if (separator && i < links.length - 1)
      pg.appendText(separator).setLinkUrl("");
  });
  return pg;
}