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

拥有回忆 提交于 2019-12-12 01:13:16

问题


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

I tried to do the next thing:

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];
}

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.

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

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


回答1:


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.

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.)

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


来源:https://stackoverflow.com/questions/54689124/how-to-create-an-array-or-one-long-string-of-hyperlinks-in-google-docs-using-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!