Apps Script .getActivePage() only returning “home”

2019-05-10 04:07发布

问题:

Is anyone else having trouble with SiteApps.getActivePage()? for me it's only returning the home page, not the active page.

回答1:

getActivePage is meant to work when you embed the published gadget in Google Sites page. It will return the page in which the GAS Gadget is embedded, otherwise it will return null

Here is a sample code which works perfect for me in Google Sites Apps Script Gadget.

function doGet(e) {
  var app = UiApp.createApplication();
  app.add(app.createLabel('Page name: '+SitesApp.getActivePage().getName()));
  app.add(app.createLabel('Url: '+SitesApp.getActivePage().getUrl()));
  return app;
}


回答2:

When directly embedding a gadget into a sites page, this seems to work as expected; however, when copying a page, it appears to fail -- perhaps this fits your scenario?

There's a pending apps-script issue (572) for pages copied from other pages which mentions:

getActivePage() called inside gadget returns the home page of the site



回答3:

The problem occurs whenever you setHtmlContent() on the page, the original embedding HTML code gets modified and the app stops behaving normally. It then returns the home page of the site whenever you getActiveSite() it...

It's really annoying and I'm quite disappointed about how long google is taking to fix this...