我正在尝试从Google网站的公告页面检索帖子,并使用应用脚本小工具进行展示。
HTML页面
<html>
<body>
<div>
<?
var site = SitesApp.getSite("google.com", "smgeneral");
var page = site.getChildByName("news");
var announcements = page.getAnnouncements({ start: 0, max: 1, includeDrafts: false, includeDeleted: false});
for (var i = 0; i < announcements.length; i++) { ?>
<div><?= announcements[i].getHtmlContent(); ?></div>
<? } ?>
</div>
</body>
</html>
脚本:
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
输出:
<table class='sites-layout-name-one-column sites-layout-hbox' cellspacing='0'><tbody><tr>...........</td></tr></tbody></table>
当我在页面中嵌入脚本时,它显示的是html代码。我怎么能显示格式化的内容? 请帮忙。感谢。
答案 0 :(得分:0)
不是预加载,而是在加载页面后将其加载为脚本:
HTML中的:
<div id="contentHere"></div>
<script>
function getNews(){
google.script.run.withSuccessHandler(updateDiv).getUnreadEmails();
}
function updateDiv( content ){
document.getElementById('contentHere').innerHTML(content);
}
getNews();
</script>
在code.gs中:
var site = SitesApp.getSite("google.com", "smgeneral");
var page = site.getChildByName("news");
var announcements = page.getAnnouncements({ start: 0, max: 1, includeDrafts: false, includeDeleted: false});
var html = "";
for (var i = 0; i < announcements.length; i++) {
html += <div><?= announcements[i].getHtmlContent(); ?></div>;
}
return html;