ddberlingo
ddberlingo

Reputation: 25

Google sites apps script to create HTML with hyperlink

I'm building HTML incrementally for use in a Google site, and I need to replace some content with a hyperlink. As per the documentation <a> tags are not permitted though.

So...how do I convert anchor in the following example to be a string of HTML that can be appended?

for(var ii=1; ii < strings.length; ii++) {
   html += "<h2>An item</h2>";
   html += "<div class='item'>";
   anchor = app.createAnchor("click for further info...", "http://x.com?id=y");
   html += strings[ii].replace(/<link>/g, anchor);
   html += "</div>";
}
app.add(app.createHTML(html));

I don't think .add(anchor) would work in this case since I'm needing to replace a placeholder string.

Upvotes: 1

Views: 1791

Answers (1)

Mogsdad
Mogsdad

Reputation: 45710

Class Html can only contain html text, it isn't a container for Class Anchor instances. You can put Anchors into any UiApp container element, though.

Example within an Apps Script gadget on a Google Site:

Screenshot

This is an example of an anchor in a FlowPanel. While any panel type would do, the flow panel uses a default arrangement of contained elements, which means that we can intersperse the Anchor elements with inlineLabels, and end up with the text appearing as a single line.

Code:

// Simulate html text + link by combining 
// inlineLabels & anchors in a flowPanel.
function doGet() {
  var app = UiApp.createApplication();

  var flow = app.createFlowPanel(); // To simulate text + link
  flow.add(app.createInlineLabel('If you want to see kittens, '));
  flow.add(app.createAnchor('click here', 'https://www.google.com/search?q=cute+kitten+pictures&tbm=isch'));
  flow.add(app.createInlineLabel('.'));
  app.add(flow);

  return app;
}

Upvotes: 3

Related Questions