谷歌应用程序脚本新闻(公告)显示使用HTML服务

我正在尝试从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代码 . 我怎么能显示格式化的内容?请帮忙 . 谢谢 .

回答(1)

2 years ago

加载页面后,将其作为脚本加载,而不是预加载:

在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;