首页 > 解决方案 > 在 Google 脚本中,如何将 doc url 变量传递到 href 语句中?

问题描述

我在 Google 表格中有一个绑定的 Google 脚本,它创建一个菜单并使用一堆函数填充工作表的 onOpen 菜单。一个函数创建一个新的 Google Doc。然后我想打开那个文档,但我不知道如何将文档的 URL 传递到 href 语句中。请参见下面的代码:

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'); 

有人建议这个问题已经在这里得到了回答,虽然我可以理解为什么提出这个建议,但我有以下观察结果:

1)我对谷歌脚本编码太陌生了,不知道足够多的术语来在 Stack Overflow 上形成搜索查询来找到答案。事实上,如果我以某种方式偶然发现了那个“答案”,我不确定我是否会认出它,因为整体代码与我正在努力解决的代码是如此不同。

2) 在我提出问题之前,该解决方案并未作为建议的解决方案之一出现。

综合考虑这两点,尤其是对于新手编码人员,我认为在这个网站上提出我的问题将对符合我个人资料(而不是经验丰富的编码人员)的其他人有所帮助,因为这个问题和答案可能会出现在其他人试图做我曾经/正在尝试做的事情,而如何在字符串问答中使用变量则不会。

标签: google-apps-scriptgoogle-sheetsgoogle-docs

解决方案


桑迪·古德回答:

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

推荐阅读