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