1

I developed a script extension that uses a Google doc as template AND as script holder.

It gives me a very nice environment to implement a mail merge application (see below). At some point I use the DocsList class makeCopy(new Name) to generate all the docs that will be modified and sent. It goes simply like that :

var docId=docById.makeCopy('doc_'+Utilities.formatString("%03d",d)).getId();

Everything works quite nicely but (of course) each copy of the template doc contains a copy of the script which is obviously not necessary ! It is also a bit annoying since each time I open a copy to check if data are right I get the sidebar menu that opens automatically which is a time consuming process ...

My question is (are) :

  • is there any way to remove the embedded script from the copy ? (that would be simple)
  • or should I copy all the doc elements from the template to an empty document ? (which is also a possible way to go but I didn't try and I don't know what will be in this doc in real life use... Shall I get a perfect clone in any case ?) I've read the doc and didn't find any relevant clue but who knows ? maybe I missed something obvious ;-)

below is a reduced screen capture to show the context of this question :

enter image description here

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • What? No code!? C'mon Serge, you're slipping! I've tried a variety of `.getAs()`, `.getBlob()` and setting mime types to get `DocsList.createFile()` to work. So far, I can get a gdoc that won't open, or a pdf, or "invalid Mime Type" exceptions. Visit [issue 585](https://code.google.com/p/google-apps-script-issues/issues/detail?id=585) and star it - maybe we'll get the ability to have `getAs()` return something more than PDFs. – Mogsdad May 29 '13 at 04:14
  • 1
    As a workaround you could "detect" that there's no placeholders on the file and simply do not show the side pane. – Henrique G. Abreu May 30 '13 at 14:13
  • @Henrique :that was a good suggestion :-) @ Mogsdad : starting from an empty doc could be achieved by using makeCopy on an empty doc but after that I still should iterate through the whole doc to duplicate every element one by one by identifying their type... not very efficient I guess. btw : I can live without code ! yes yes... I 'm positive, I can ! (at least I try sometimes :-) – Serge insas May 31 '13 at 20:40

1 Answers1

1

Following Henrique's suggestion I used a workaround that prevents the UI to load on newly created documents... (thanks Henrique, that was smart ;-)

The function that is called by onOpen now goes like that :

function showFields() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var find = body.findText('#'); // the new docs have no field markers anymore. 
  if(find != null){  // show the UI only if markers are present in the document.
  var html = HtmlService.createHtmlOutputFromFile('index')
      .setTitle("Outils de l'option Publipostage").setWidth(370);
  ui.showSidebar(html);
  }
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131