Google Sites‎ > ‎Sample Scripts‎ > ‎

remove page function

Since at the moment there is no way to 'permanently' delete a page from scripts (see this thread http://www.google.com/support/forum/p/apps-script/thread?tid=31e4f73ae642c2b6&hl=en )  a mechanism is needed for those of us that wish to delete a page and then re-use that same url for a new page (as discussed above). I've written the function below and would appreciate any advice as to whether there is a more elegant way of achieving what I'm trying to do. I can see no (easy/efficient) way of finding out if a given page url has been used so I rely on the fact that renaming a page to a url that is 'deleted' but not 'permanently' throws an error. But this feels wrong - there ought to be a way of checking whether an operation will fail before actually doing it. Any suggestions for a better way of doing this ?

function removePage(page)
{
  // renames page before deleting it so thet the URL can be re-used
  // returns true if successful, false if not
  var pageSuffix 1;
  var pageRenamed false;
  var pageName page.getName();
  // pageSuffix check is get out in case error trap fails which would leave us in endless loop
  while (!pageRenamed && pageSuffix 100)
  {
    try
    {
      // rename the page before deleting it so that we can keep the original url
      // this will fail if the name has been used before - we catch this using the error trap
      var page page.setName(pageName+"-"+pageSuffix);
      // if we get here, page renaming succeeded , so set flag
      var pageRenamed true;
    }
    catch(err)
    {  
      // Tried to rename a page to one that has previously been deleted, so keep trying till we succeed      
      pageSuffix++;
    }
  }
  if (pageRenamed)
  
    page.deletePage();
  }
  return pageRenamed;
}

Comments