It's all explained in the "Secrets of the spellbook" tutorial in the tutorialsection.
Basically, you create an EMPTY spellbook with a certain OrderID A.
Next, for each spell you create a new spellbook (containing your spell) with the SAME orderId A. At map initialization you disable all spellbooks except for the EMPTY one. Then you add the empty spellbook to a unit (in the object editor). Now, whenever a unit gains one of the disabled spellbooks, the spell INSIDE the disabled book will appear in the empty spellbook.