html - 如何使用 Google Apps 脚本在 Google 表格中的 htmloutput 中添加其他内容
问题描述
我找到了使用 HTML 输出创建弹出窗口的代码。当前设置为显示一个项目:在新选项卡中超链接到网站的标题。我有兴趣向此弹出窗口添加其他内容。
2个愿望:
- 在弹出窗口中显示多个链接
- 显示附加内容(例如,链接上方的介绍段落;链接描述
我尝试使用多个 showAnchor 函数调用提醒函数,结果只显示最后一个链接我尝试在提醒函数中使用数组作为 showAnchor 函数的“名称”和“url”。下面的代码以及在相关应用程序脚本的 notepad.gs 代码文件中包含此代码的示例表。
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('⚙️ Custom')
.addItem('Show Reminders','reminders')
.addToUi();
}
function reminders(){
showAnchor('Google','https://www.google.com/');
}
function showAnchor(name,url) {
var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
var ui = HtmlService.createHtmlOutput(html)
SpreadsheetApp.getUi().showModelessDialog(ui,'Reminders');
}
https://docs.google.com/spreadsheets/d/1Sqt7_Ev9iagbE9CZT1F2lAUJqc1fX4WfbyaOBGEfqyQ/edit?usp=sharing
解决方案
尝试这个:
function reminders() {
showAnchors([
{
description: 'Search the web without being tracked',
name: 'DuckDuckGo',
url: 'https://duckduckgo.com/',
},
{
description: 'A fairly popular search engine',
name: 'Google',
url: 'https://www.google.com/',
},
]);
}
function showAnchors(anchors) {
let html = '<html><body style="word-break:break-word; font-family:sans-serif;">';
html += '<p>Introduction paragraph above links.</p>'
anchors.forEach(anchor => {
html += '<h3>' + anchor.description + '</h3>'
+ '<a href="'
+ anchor.url
+ '" target="_blank" rel="noopener" onclick="google.script.host.close()">'
+ anchor.name
+ '</a>';
});
html += '</body></html>';
SpreadsheetApp.getUi()
.showModelessDialog(HtmlService.createHtmlOutput(html), 'Reminders');
}
推荐阅读
- performance - 随机微分方程的疯狂空间分配求解系统
- python - 正则表达式从字符串 Python 中找到这个特定的电话号码
- javascript - 类型错误:“ctx.cart 未定义”
- camunda - Camunda 自定义候选任务属性
- html - 如何使div大小固定?
- c - 无法获取字符串数组中的第一个字符
- python - 使用 Splinter lib (Python) 选择下拉菜单中的一项
- python - 如何在 python 中仅对特殊字符进行百分比编码?
- python - 为给定操作自动屏蔽 numpy 数组
- python - 从折线图中绘制matplotlib中的平均值?