javascript - 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?
解决方案
添加单元格值时,我找不到直接提供链接的方法。那么这个改装怎么样呢?
修改点:
- 添加单元格值后,它使用
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();
推荐阅读
- c# - LibVLCSharp.WPF VideoView.Content(覆盖)滚动
- react-native - 我想要 2 个并排的按钮反应原生
- c - 如何验证输入日期是否在日期间隔内?
- oracle - Oracle PIVOT 选择性行到列
- android - 在 android.widget.Scroller 中检测滚动结束
- python - pip3 requirements.txt 充满了垃圾
- sql-server - 使用 SQL SERVER QUERY 查找字符串中所有出现的模式
- reactjs - 日期过滤器在 IE 中不起作用 - 引导表下一个编辑器
- python - Anaconda Navigator 无法打开 - 已尝试所有其他线程
- amazon-web-services - AWS VPC 对等连接破坏 RDP?