user2215892
user2215892

Reputation: 37

Merging Google Script gs files

I have recently adopted the mail merge scripts from googleapps developer blog.

Script 2 in http://googleappsdeveloper.blogspot.hk/2011/10/4-ways-to-do-mail-merge-using-google.html

There are 4 gs files in the script where one is unnecessary and I just comment the whole script and it runs perfectly fine.

Then I attempted to copy all the contents from "UI for Standard.gs" to "Code.gs". After doing so, I deleted "UI for Standard.gs".

The script then does not run. It can no longer send out mails.

Any ideas on what might go wrong when merging scripts?

Thanks!

Upvotes: 0

Views: 670

Answers (1)

Mogsdad
Mogsdad

Reputation: 45710

There is no reason that the script would stop running if all the code from separate .gs files were faithfully copied to a single .gs file. (Although there is no good reason to do this. In fact, it's a good practice to separate code as Romain did in this example.)

Did you know? When a script containing multiple 'files' is run, the entire script is 'loaded' and run. To the machine, the existence of multiple gs files is moot - they are simply a convenience for the author. All functions and global variables in all files are available to code in every individual file. All statements outside the scope of functions get executed. (So if you have something like var sheet = SpreadsheetService.getActiveSheet() at the top of every gs file, it will execute multiple times - you just need one.)

Your options at this point are:

  • Make sure your data is still valid. Maybe there's nothing wrong with the code.
  • Look at the Execution Transcript after a failed run, to see if you get clues of where things went wrong. At the very least, you'll know where it stopped.
  • Review your manual merge, comparing to the original code, to ensure that you've copied the code properly. If you have a second set of eyes for this, even better.
  • Use the debugger or Logger to carefully walk through processRow() and merge() - the former is the ONLY place that email is sent, the latter performs the main merge operation. Since the observed problem is that emails aren't sent, it makes sense to start debugging here.
  • Go back to the original and start over.

Any ideas on what might go wrong when merging scripts?

  • Misplaced braces ({ and }) can change data structures and functions.

  • Multiple copies of functions are allowed in gs, although only the bottom one will execute. You'll get no error message in this case, but things may not act the way you expect. (The original YAMM2 script had no duplicate function names, but I've seen other scripts in the gallery that do.)

    If, for instance, you copied functions from a companion gs file into Code.gs, then commented out the BODIES of the functions in the original file, a call to any of those functions would execute the empty version from the companion file, NOT the copy in Code.gs.

  • Operator error. We've all been there.

Upvotes: 3

Related Questions