使用html服务显示Google应用脚本新闻(公告)

时间:2015-03-26 13:39:00

标签: google-apps-script google-apps

我正在尝试从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 个答案:

答案 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;