Is there a way to remove a script from a doc (usin

2019-06-06 07:04发布

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

1条回答
叛逆
2楼-- · 2019-06-06 07:46

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);
  }
}
查看更多
登录 后发表回答