首页 > 解决方案 > Google Sheet To Doc 超链接显示

问题描述

上下文:我将 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(); 

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

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

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

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

标签: javascriptgoogle-apps-script

解决方案


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

修改点:

  • 添加单元格值后,它使用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();

笔记 :

  • theDate并且comment未在您的问题脚本中声明。
    • 在运行此示例脚本之前,请再次检查。
  • 在这个示例脚本中,它假设 检索到的值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();

推荐阅读