Spraying it on your skin just doesn't work as well as you'd think - magnesium ions don't really penetrate through your skin in any meaningful amounts, so it's basically just expensive placebo. If you actually have a magnesium deficiency, you need something that actually gets into your body, so tablets or powder you mix into a drink.
But here's the more important thing: before you just start popping magnesium, you should figure out where the cramps are actually coming from. Nighttime cramps often aren't from magnesium deficiency at all - way more often it's dehydration, awkward sleeping positions, or your muscles are just tense. If you sit a lot during the day or don't move around enough, cramps at night can get particularly annoying. Try drinking more and gently stretching your calves before bed.
If that doesn't help and the cramps really keep happening regularly, then a blood test makes sense - get your magnesium levels checked. And yeah, if there actually is a deficiency, tablets help. But most people have more of a movement or hydration problem than an actual deficiency.