Home > Software design >  Automatically execute Google App Script on Creation of Google Doc or shortly thereafter
Automatically execute Google App Script on Creation of Google Doc or shortly thereafter

Time:08-06

We previously had success (from help we've received here!) adding an onOpen menu item that would trigger text formatting. The back story here is that we have google doc merge templates that each have this bound app script. Once the template is generated using data from another source, the user can click the menu item to then format the text. However, as we've grown this has become cumbersome with the number of documents that are generated daily. It requires a user to authorize the script and click the buttons to ultimately format the merged text. This also causes a stopping point in business process because the user running the script is not the ultimate user of the document.

I've pasted the working code below, but I was hopeful that someone might have some ideas on how to change this script so that it ran automatically, without user intervention, after the document is created. The document creation and the data merge happen simultaneously or nearly simultaneously.

function onOpen() {
  DocumentApp.getUi().createMenu('Butler')
      .addItem('Format Headings', 'FormatHeadings')
      
      .addToUi();
}

function FormatHeadings() {

  handle_tags(['<req>', '</req>'], "Roboto", 14, "Bold", "#4a5356");
  handle_tags(['<cit>', '</cit>'], "Roboto", 12, "Bold", "#4a5356");
  handle_tags(['<con>', '</con>'], "Roboto", 12, "Bold", "#B38F00");
  
}

function handle_tags(tags, family, size, style, color) {

  var body      = DocumentApp.getActiveDocument().getBody();
  var start_tag = tags[0];
  var end_tag   = tags[1];
  
  var found     = body.findText(start_tag);

  while (found) {
    var elem    = found.getElement();
    var start   = found.getEndOffsetInclusive();
    var end     = body.findText(end_tag, found).getStartOffset()-1;

    elem.setFontFamily(start, end, family);
    elem.setFontSize(start, end, size);
    elem.setForegroundColor(start, end, color);

    switch (style.toLowerCase()) {
      case 'bold': elem.setBold(start, end, true); break;
      case 'italic': elem.setItalic(start, end, true); break;
      case 'underline': elem.setUnderline(start, end, true); break;
    }

    found = body.findText(start_tag, found);
  }

  body.replaceText(start_tag, '');
  body.replaceText(end_tag, '');
}

Thank you!

CodePudding user response:

Just to set the right expectations, you can make Apps Script code run by triggering an event or manually running a function. We have those 2 options.

Now, you can't automatically run after the creation of a file because a file creation is not a supported event. You could however:

Create a form that requests the fileID of the new file (the file must be ready to be formatted), use the onSubmit event so when the form is submitted an Apps Script function is executed and the file is created. The onSubmit event is an installable trigger, for more details check the documentation at https://developers.google.com/apps-script/guides/triggers/events#google_forms_events.

You are using DocumentApp.getActiveDocument() you can replace that and use DocumentApp.openById() with the ID that was submitted in the form. This will automatically apply that same script to the file ID you submit in the form.

  • Related