【问题标题】:Google Sheet To Doc Hyperlink DisplayGoogle Sheet To Doc 超链接显示
【发布时间】:2018-04-28 20:36:39
【问题描述】:

上下文:我将 G 表单数据插入到 G 表中,然后将新行附加到我在 G Doc 上的表上。 Google Doc Table 中的最后一列我想提供一个带有“编辑”一词的超链接。

我想实现 .link 的功能,但它插入了在文档中不起作用的 html。 https://www.w3schools.com/jsref/jsref_link.asp

这是我直接在 G 表中的脚本(提交表单时):

  // Grab the Table
  var body = DocumentApp.openById('theId').getBody(),
  searchElement = body.findElement(DocumentApp.ElementType.TABLE),
  element = searchElement.getElement(),
  table = element.asTable();

  // Wait for row insertion to finish, so that sheet.getLastRow() method gets the updated number of rows
  Utilities.sleep(1000); // 1 second  

  // Get the last row ID  
  var mySs = SpreadsheetApp.openById('sheetId').getSheets()[0];
  var lastRowId = mySs.getLastRow();
  var hyperlink = mySs.getRange('L' + lastRowId).getValue();

  // Insert the Row
  var cells = [lastRowId, hyperlink];
    var addRow = table.appendTableRow();
    cells.forEach(function(e, i){
    addRow.insertTableCell(i, e);
  });

  body.saveAndClose(); 

所以现在它直接将超链接添加到 google 表单。而我想将单元格数组中的当前超链接变量切换为实际的超链接,上面写着“编辑”,它具有超链接变量 URL。

我尝试直接从 G 表单中添加脚本数据示例

var hyperlink = '=HYPERLINK("www.google.com", "Google")';

它工作并将超链接插入到 G 表。问题是它无法将其带到 G Doc。我想可能是因为 G Sheet 公式无法提取到 G Doc Tables 中?

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    我找不到添加单元格值时直接提供链接的方法。那么这个修改怎么样呢?

    修改点:

    • 添加单元格值后,它使用setLinkUrl() 设置链接。
    • 我认为body.saveAndClose() 出现错误。请在文档中使用saveAndClose()

    修改后的脚本:

    // Grab the Table
    var doc = DocumentApp.openById('theId');
    var body = doc.getBody(),
    searchElement = body.findElement(DocumentApp.ElementType.TABLE),
    element = searchElement.getElement(),
    table = element.asTable();
    
    // Wait for row insertion to finish, so that sheet.getLastRow() method gets the updated number of rows
    Utilities.sleep(1000); // 1 second
    
    // Get the last row ID  
    var mySs = SpreadsheetApp.openById('sheetId').getSheets()[0];
    var lastRowId = mySs.getLastRow();
    var hyperlink = mySs.getRange('L' + lastRowId).getValue(); // Sample is '=HYPERLINK("www.google.com", "Google")'
    // var hyperlink = '=HYPERLINK("www.google.com", "Google")'; // If the error related to LINK occurs, please use this line instead of above hyperlink.
    var link = hyperlink.match(/"(.*?)"/g); // Added
    
    // Insert the Row
    var cells = [lastRowId, theDate, comment, link[1].replace(/"/g, "")]; // Modified
    var addRow = table.appendTableRow();
    cells.forEach(function(e, i){
      addRow.insertTableCell(i, e);
    });
    table.getCell(table.getNumRows() - 1, cells.length - 1).setLinkUrl(link[0].replace(/"/g, "")); // Added
    
    doc.saveAndClose();
    

    注意:

    • theDatecomment 未在您的问题脚本中声明。
      • 请在运行此示例脚本之前再次检查。
    • 在此示例脚本中,假设mySs.getRange('L' + lastRowId).getValue() 检索到的值是类似=HYPERLINK("www.google.com", "Google") 的字符串值。因此 URL 和链接字符串由 "(.*?)" 的正则表达式检索。
      • 如果mySs.getRange('L' + lastRowId).getValue()检索到的值与此不同,请修改正则表达式。
    • 如果要更改LINK在cells数组中的索引,请修改。

    参考:

    如果我误解了你的问题,我很抱歉。

    编辑:

    // Grab the Table
    var doc = DocumentApp.openById('theId');
    var body = doc.getBody(),
    searchElement = body.findElement(DocumentApp.ElementType.TABLE),
    element = searchElement.getElement(),
    table = element.asTable();
    
    // Wait for row insertion to finish, so that sheet.getLastRow() method gets the updated number of rows
    Utilities.sleep(1000); // 1 second
    
    // Get the last row ID  
    var mySs = SpreadsheetApp.openById('sheetId').getSheets()[0];
    var lastRowId = mySs.getLastRow();
    
    // --- Following part was modified ---
    var url = 'url.com'; // Added
    var linkText = 'Edit'; // Added
    // Insert the Row
    var cells = [lastRowId, theDate, comment, linkText]; // Modified
    var addRow = table.appendTableRow();
    cells.forEach(function(e, i){
      addRow.insertTableCell(i, e);
    });
    table.getCell(table.getNumRows() - 1, cells.length - 1).setLinkUrl(url); // Modified
    
    doc.saveAndClose();
    

    【讨论】:

    • var hyperlink '.getRange' 的当前单元格内容目前只是一个 URL。但是,我可以轻松地将格式更改为您的示例。有没有更简单的方法来实现这个解决方案?像这样:var url = 'url.com'; var linkText = '编辑'; var hyperlink = hyperlink(url, linkText);
    • @Bjaeg 很抱歉给您带来不便。我更新了我的答案。请证实。如果我误解了您的评论,我很抱歉。
    • @Bjaeg 欢迎。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多