如何使用Google脚本在Google文档中创建超链接数组(或一个长字符串) [英] How to create an array (or one long string) of hyperlinks in google docs using google script

查看:112
本文介绍了如何使用Google脚本在Google文档中创建超链接数组(或一个长字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或原始格式的google文档超链接.

Or how hyperlink of google docs looks like in raw format.

我试图做下一件事:

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,只能在文本对象上使用-没有找到将字符串转换为文本的方法.

I get an error, as i can't use setLinkUrl on string, only on text object - didn't find a way to cast string into text.

尽管,如果我将其按原样"粘贴,则"http ..."显示为常规字符串-而非链接.

Although, if i paste it "as it", the "http..." shows as a regular string - not a link.

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

I Want to get something like this: 1, 2, 3 ...... and paste it into google docs document.

推荐答案

链接是关联元素的丰富"功能(通常 Text ).因此,要添加指向通用文本的链接,首先必须获取关联的Text元素,然后调用

Links are "rich" features of the associated element (usually Text). So to add a link to generic text, first you must get the associated Text element, and then invoke setLinkUrl on it.

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

As with other rich format methods, appended elements inherit the formatting specification of the preceding sibling element. Thus, if you format the last element of a parent, the next element appended to the parent will likely also be formatted in that manner. I explicitly specify a nullstring URL for the separator text to avoid the link extending beyond the actual display text. (This means that if you programmatically append to the Paragraph after calling this function, that appended text will have the same URL as the last display text from your array.)

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

This simple function takes a Paragraph as input, along with the array of display text and the URIs, and adds them to the end.

/**
 * 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;
}

这篇关于如何使用Google脚本在Google文档中创建超链接数组(或一个长字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆