If you have a fairly large Google Site, and you make mistakes like we all do, at some point you’ll need to do a mass update to all the pages. It happened to me yesterday. I host a gadget on most of my pages see (Displaying analytics data on site pages), which I had accidentally deleted a while ago. I host it on Drive, so when I deleted it, I actually only moved it to my trash.

When you host something on Drive, it takes the url https://googledrive.com/host/fileid , so it actually still continues to work from the trash bin. Yesterday I thought it was time to clean out my trash so I permanently deleted everything, including the hosted gadget – so every page on this site started showing an error where the missing gadget used to be.

Luckily I had the source for the gadget on GitHub, so I could easily recreate it, but the problem with using Drive as a host is that the fileid is a oneoff thing – meaning that I cannot just create another file with the same id.

So I was faced with either manually editing each of 700+ pages on this site and reinserting a new gadget, or patching up the existing gadget with the new fileid.

Here’s a snippet to do it.

DANGER and IMPORTANT – make sure you do lots of checking on what will be changed before actually doing the setHtmlContent() . This could mess up your site if you replace the wrong thing with the wrong thing. NOTE also that some deprecated gadgets will stop working when rewritten. For example , those old Adsense gadgets that Google deprecated on Sites. If your page gets rewritten and you still have some of them on your site – they will disappear forever. If you have gadgets on your site, test thoroughly that they will not be screwed up by rewriting the html before running.

All I had to do was set the old fileId

And the new one

Always worth giving it a trial run first by setting this to false

For this snippet you’ll need my cUseful library. You can find the details below or get it from github.

For more like this see Google Apps Scripts Snippets
Why not join our forum,follow the blog or follow me on Twitter to ensure you get updates when they are available.