简体   繁体   中英

In Google scripts, how do you pass a doc url variable into an href statement?

I have a bound Google script in a Google Sheets that creates a menu and populates the menu onOpen of the sheet with a bunch of functions. One function creates a new Google Doc. I then want to open that doc, but I can't figure out how to pass the doc's URL into an href statement. See code below:

function createLandscapeLyricDoc() {
  var doc = DocumentApp.create('Rename');
  var title = "replace with song title and then link this text to song title cell in Catalog Spreadsheet"
  var url = doc.getUrl();
  var body = doc.getBody();
  var paragraph = body.insertParagraph(0, "");
  var text1 = paragraph.appendText("© replace with writer(s)");
  text1.setFontSize(8);
  var rowsData = [['PUT FIRST VERSE/CHORUS HERE.  (SUGGEST USE ALL CAPS.)', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
  var style = {};
  body.insertParagraph(0, title)
  .setHeading(DocumentApp.ParagraphHeading.HEADING3);
  table = body.appendTable(rowsData);
  style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
  table.setAttributes(style);
  /*line below is the one that does not work as the variable "url" is not being read correctly (as I don't know how to use it or if this can even be done)*/
  var html = "<a href= 'url'; target='_blank'>Open 1-Column Lyric Template</a>";
  var selection = SpreadsheetApp.getActiveSheet();
  var userInterface = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Landscape New Lyric Doc'); 

It has been suggested that this question has already been answered here and while I can see why that suggestion was made, I have the following observations:

1) I am way too much of a newbie to Google script coding to know enough terminology to form a search query at Stack Overflow to find that answer. And in fact, had I somehow stumbled across that "answer," I am not sure I would have recognized it as such as the overall code is so different from the code I was struggling with.

2) That solution did not appear as one of the suggested ones for me to review before I asked my question.

Considering these two points together, especially for newbie coders, I think having my question on this site will be helpful to others that fit my profile (and not that of an experienced coder) because this question and answer probably will show up for others trying to do what I was/am trying to do whereas How to use a variable inside a string Q&A will not.

桑迪·古德(Sandy Good)回答:

var html = '<a href= "' + url + '"; target="_blank">Open 1-Column Lyric Template</a>;'

I find that doing it this way in complex strings is somewhat easier.

var html = Utilities.formatString('<a href="%s" target="_blank">Open 1-Column Lyric Template</a>',url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM